简体   繁体   English

使用Roslyn获取方法参数的TYPE的内部详细信息

[英]Get inner details of the TYPE of a method parameter using Roslyn

I'm trying to find out if there is a way to get the inner details of the TYPE of a method parameter. 我试图找出是否有一种方法来获取方法参数的类型的内部细节。

Say a method has the below definition 说一种方法具有以下定义

public void method1(Type1 param1, Type2 param2)
{
   ...
   ...
}

And Type1 has the below definition 并且Type1具有以下定义

public class Type1
{
    public int prop1 {get; set;}
    public string prop2 {get; set;}
    public double prop3 {get; set;}
}

Then what I'm looking to get out of my sample tool is the list of properties of the class Type1 . 然后,我希望从示例工具中脱颖而出的是Type1类的属性列表。 That is, I'm expecting my output to be 也就是说,我期望我的输出是

prop1 
prop2 
prop3

I could get a reference to the parameter by doing a param.Type but it is of type IdentifierNameSyntax . 我可以通过执行param.Type来获取对参数的引用,但是它的类型为IdentifierNameSyntax I'm able to get the name from it ( Type1 in this case) but not able to dig deeper INTO Type1 to get the properties. 我可以从中获取名称(在这种情况下为Type1 ),但无法深入研究INTO Type1以获取属性。

Is there any simple way to get that which I'm not yet aware of? 有什么简单的方法可以得到我还不知道的东西? Or do I have to do a search in the entire solution again using the name of the type that I got? 还是我必须使用得到的类型名称再次在整个解决方案中进行搜索?

Thanks a lot! 非常感谢!

PS: I did get a thought of using Reflection but all I have is a string ( Type1 ) and not the actual TYPE. PS:我确实想到过使用Reflection但是我所拥有的只是一个字符串( Type1 ),而不是实际的TYPE。 Not sure if I can use this or not. 不知道我是否可以使用此功能。

Update-1: This question seems to be mildly close to what I'm expecting but from what I understand, the user only wants the name of the parameter type, not it's inner details. Update-1: 这个问题似乎与我期望的差不多,但是据我了解,用户只需要参数类型的名称,而不是内部细节。

Update-2: Adding sample code from my tool below. 更新2:从下面的工具中添加示例代码。 Unfortunately, I cannot post the actual code but the below sample is basically what I'm trying to do. 不幸的是,我无法发布实际的代码,但是下面的示例基本上是我想做的。 Update-2: Adding sample code from my tool below. 更新2:从下面的工具中添加示例代码。 Unfortunately, I cannot post the actual code but the below sample is basically what I'm trying to do. 不幸的是,我无法发布实际的代码,但是下面的示例基本上是我想做的。

var methodNode = (MethodDeclarationSyntax)node;
string paramClassName = string.Empty;

foreach (var param in methodNode.ParameterList.Parameters)
{
    paramClassName = param.Type.ToFullString();
    //GET Class details from the above class name
    GetInnerDetailsOfClassFromClassName(paramClassName);   //any way to do this?
}

一旦找到ParameterSyntax ,请使用SemanticModel.GetDeclaredSymbol获取IParameterSymbol ,然后查看其Type以获取您感兴趣的ITypeSymbol

Thanks to @Serj-Tm, @KevinPilch-Bisson and @JonSkeet. 感谢@ Serj-Tm,@ KevinPilch-Bisson和@JonSkeet。 Your suggestions of using Symbols worked. 您使用符号的建议起作用了。 This is my code below that returned the result I was expecting. 这是我下面的代码,返回了我期望的结果。

var methodNode = (MethodDeclarationSyntax)node;
string modelClassName = string.Empty;

foreach (var param in methodNode.ParameterList.Parameters)
{
    var metaDataName = document.GetSemanticModelAsync().Result.GetDeclaredSymbol(param).ToDisplayString();
//'document' is the current 'Microsoft.CodeAnalysis.Document' object
    var members = document.Project.GetCompilationAsync().Result.GetTypeByMetadataName(metaDataName).GetMembers();
    var props = (members.OfType<IPropertySymbol>());

    //now 'props' contains the list of properties from my type, 'Type1'
    foreach (var prop in props)
    {
        //some logic to do something on each proerty
    }

}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM