简体   繁体   English

如何使用Roslyn API访问特定类中方法参数自己的类型数据(C#代码分析)

[英]How can I access to method parameter's own type data in specific class using Roslyn API(C# Code Analysis)

I'm developing my own custom rules using Roslyn API(C# Code Analysis) . 我正在使用Roslyn API(C#代码分析)开发自己的自定义规则。

I want to get method parameter's own type class inheritance information in specific namespace's name. 我想以特定名称空间的名称获取方法参数自己的类型类继承信息。

First, I initialized RegisterSyntaxNodeAction to SyntaxKind.CompilationUnit in DiagnosticAnalyzer class's Initialize method . 首先,我在DiagnosticAnalyzer class's Initialize method SyntaxKind.CompilationUnit RegisterSyntaxNodeAction初始化为SyntaxKind.CompilationUnit

Second, I accessed specific namespace's name( ex:BIZprj ). 其次,我访问了特定名称空间的名称( ex:BIZprj )。

Third, I got the parameter's type instance( ex:parameterType ) 第三,我得到了参数的类型实例( ex:parameterType

Until here, I tried to serveral times to get parameter type instance's inheritance name. 在此之前,我尝试过多次获取参数类型实例的继承名称。 How can I resolve this? 我该如何解决?

my code example 我的代码示例

public class SampleAnalyzer : DiagnosticAnalyzer
{

//...
public override void Initialize(AnalysisContext context)
{
            context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.CompilationUnit);
}

private void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
            var rootNode = (CompilationUnitSyntax)context.Node;
            ... go to MethodNode
    var methodParameters = methodNode.ParameterList.Parameters;
    foreach(var param in methodParameters)
    {
        var parameterType = param.Type;
        // **Here is what I want to get parameter type's inheritance class name.!!**
    }
  }
}

enter image description here 在此处输入图片说明

Try the following code, where I have documented various parts, we are essentially going to the MethodNode , traversing further to the ParameterSyntax collection and for each parameter first node is Type and Last is the parameter name , once we have the type of any parameter, we can also get the Base type and from that you can fetch the Type Name as shown in the image 尝试下面的代码,在这里我已经记录了各个部分,我们实际上是去到MethodNode ,然后遍历ParameterSyntax集合,对于每个parameter第一个节点是Type ,Last是parameter name ,一旦我们拥有任何参数的类型,我们还可以获取Base type然后从中获取类型名称,如图所示

// Fetch the CompilationUnitSyntax
var rootNode = (CompilationUnitSyntax)context.Node;

// Filter out the collection of MethodDeclarationSyntax
var methodSyntaxNodes = from methodDeclaration 
                        in rootNode.DescendantNodes().Where(x => x is MethodDeclarationSyntax )
                        select methodDeclaration;

// Traverse through each MethodNode                             
foreach(var methodNode in methodSyntaxNodes)
{
    var mNode = (MethodDeclarationSyntax)methodNode;

    // Traverse through each Parameter Syntax       
    foreach(var parameterSytanx in mNode.ParameterList.Parameters)
    {
        // Get First Token and Fetch the BaseType
        var baseType = parameterSytanx.GetFirstToken().GetType().BaseType;
    }           
}

暂无
暂无

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

相关问题 使用Roslyn时如何在方法中验证参数的类型 - How to validate a parameter's type in method when using Roslyn 使用C#,如何使用参数访问VB类的默认属性的getter / setter? - Using C#, how can I access the getter/setter of a VB class's default property with parameter? 给定一个包含类名称的字符串,然后我如何使用该类作为C#中的类型参数来调用泛型方法? - Given a string which holds the name of a class, how can I then call a generic method using that class as the type parameter in C#? 如何从 C# 中的 roslyn 代码分析中正确使用 ControlFlowGraph - How to properly use ControlFlowGraph from roslyn code analysis in C# 如何在ASP.Net Web表单中使用Roslyn执行C#代码? - How I can execute C# code using Roslyn in ASP.Net web form? C#是否可以将Class类型作为参数传递给Function并访问方法内部的类变量 - C# Can we pass Class type as parameter to Function and access class variables inside method 有没有办法从使用 roslyn 在 c# 中编译的内存代码创建的类实例访问属性 - Is there a way to access properties from class instances created using in memory code compiled by roslyn in c# 我如何使用C#将类的多属性作为方法的参数传递 - How i can pass multi property of class as parameter of method using C# C#如何在不使用参数的情况下从另一个方法中使用该方法的类中获取数据 - C# how do I get data from the class that I used the method in another class without using a parameter 如何使用 Roslyn 将 JsonIgnore 属性添加到 class 中具有特定返回类型的所有属性? - How do I add a JsonIgnore attribute to all properties with a specific return type in a class using Roslyn?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM