转换为嵌套 class 格式
Convert to nested class format
给出这样的东西:
namespace Mystuff
{
public class Foo
{
public class Bar
{
}
}
}
在 Rosyln 中,如果我想获得代表 Bar
的符号,我可以这样做:
var barSymbol = compilation.GetTypeByMetadataName("MyStuff.Foo+Bar");
但这行不通:
var barSymbol = compilation.GetTypeByMetadataName("MyStuff.Foo.Bar");
你必须有 +
来表示 Bar
嵌套在 Foo
中。但是,如果我只有字符串 MyStuff.Foo.Bar
(因为它来自其他地方,它的名称为 Bar
),是否有可靠的方法从 MyStuff.Foo.Bar
到 MyStuff.Foo+Bar
如果您不知道 先验 MyStuff.Foo.Bar
是 Foo
内部的嵌套 class 还是非嵌套 class 命名空间内 MyStuff.Foo
?
你可以自己查一下:
在 .
上拆分名称,然后从 compilation.SourceModule.GlobalNamespace
开始,循环遍历每个部分并在当前交易品种上调用 GetMembers(name)
以获取 ITypeOrNamespaceSymbol
名字.
给出这样的东西:
namespace Mystuff
{
public class Foo
{
public class Bar
{
}
}
}
在 Rosyln 中,如果我想获得代表 Bar
的符号,我可以这样做:
var barSymbol = compilation.GetTypeByMetadataName("MyStuff.Foo+Bar");
但这行不通:
var barSymbol = compilation.GetTypeByMetadataName("MyStuff.Foo.Bar");
你必须有 +
来表示 Bar
嵌套在 Foo
中。但是,如果我只有字符串 MyStuff.Foo.Bar
(因为它来自其他地方,它的名称为 Bar
),是否有可靠的方法从 MyStuff.Foo.Bar
到 MyStuff.Foo+Bar
如果您不知道 先验 MyStuff.Foo.Bar
是 Foo
内部的嵌套 class 还是非嵌套 class 命名空间内 MyStuff.Foo
?
你可以自己查一下:
在 .
上拆分名称,然后从 compilation.SourceModule.GlobalNamespace
开始,循环遍历每个部分并在当前交易品种上调用 GetMembers(name)
以获取 ITypeOrNamespaceSymbol
名字.