I'am writing source generator that makes a copy (with some little changes) of simple DTO classes. I have copied all contained properties and all works fine. But some of my classes inherited from another like this:
public class MyClass : MyBaseClass
{
public string MyProperty { get; set; }
}
I can't just copy inheritance declaration, because my copied classes locaded in different namespace. So I need to add using to namespace of MyBaseClass, so I need to find than namespace. I've already done this for my property types using semantic model, like this:
Compilation
.GetSemanticModel(propertyNode.Type.SyntaxTree)
.GetSymbolInfo(propertyNode.Type)
.Symbol
.ContainingNamespace
But I have unexpected issue with gettting namespace from base type. What i've tried for now:
// list of all base types
var typeList = classNode.BaseList;
var semanticModel = Compilation.GetSemanticModel(typeList.SyntaxTree);
var symbol = semanticModel.GetSymbolInfo(typeList); // symbol.Symbol is null
var type = semanticModel.GetTypeInfo(typeList); // type.Type is null
// list of all base types
var firstType = classNode.BaseList.Types[0];
var semanticModel = Compilation.GetSemanticModel(firstType.SyntaxTree);
var symbol = semanticModel.GetSymbolInfo(firstType); // symbol.Symbol is null
var type = semanticModel.GetTypeInfo(firstType); // type.Type is null
// list of all base types
var typeList = classNode.BaseList;
var semanticModel = Compilation.GetSemanticModel(typeList.SyntaxTree);
var symbol = semanticModel.GetSymbolInfo(typeList.Types[0]); // symbol.Symbol is null
var type = semanticModel.GetTypeInfo(typeList.Types[0]); // type.Type is null
I dont know what went wrong. I've tried to call different methods with different parameters, but did not get proper result. May be there is another way to do this?