Is there a way to read the default namespace setting from the IProject interface or any other Roslyn interface? I know that I can parse the project\'s file but I think this shou
Unfortunately, Roslyn does not expose a way to do that at the moment, but I agree that it is something we will probably need eventually.
The library Microsoft.Build.Evaluation, which is, I believe, the successor of Roslyn does have this feature, but it is not easy to find.
I use the code below to obtain the default namespace.
My tests have shown that it matches the RootNamespace, stored in the .csproj file.
private string GetDefaultNamespace(Microsoft.Build.Evaluation.Project p)
{
string rtnVal = "UNKNOWN_NAMESPACE";
foreach (ProjectItemDefinition def in p.ItemDefinitions.Values)
{
if (def.ItemType == "ProjectReference")
{
foreach(ProjectProperty prop in def.Project.AllEvaluatedProperties){
if(prop.Name == "RootNamespace"){
rtnVal = prop.EvaluatedValue;
}
}
}
}
return rtnVal;
}