使用Roslyn时如何在方法中验证参数的类型 [英] How to validate a parameter's type in method when using Roslyn

查看:59
本文介绍了使用Roslyn时如何在方法中验证参数的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Roslyn进行代码分析,以验证即使我具有以下签名

  public void MyMethod (对象anObject,MyCustomObject customObject); 

我只想从MyCustomObject接收一个字符串(第一个)和一个孩子作为参数(第二个)。我对签名无能为力,不能更改。



这是我评估方法的内容(以下是代码段)

  public void OnMethodInvocation(SyntaxNodeAnalysisContext context)
{
var invocation = context.Node作为InvocationExpressionSyntax;
var symbol = context.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;

if(symbol?.Name.ToString()== MyMethod)
{
var parameterList = invocation.Parameters;
}

到目前为止,我可以通过属性Parameters(符号)来操作IParameterSymbol对象。参数)。我没有得到以下内容:我已经遍历了包含两个参数的IEnumerable结果,但是由于方法签名,它希望接收一个对象和一个MyCustomObject实例。我目前无法确定第一个参数确实是对象而不是字符串(仅是示例,可能是其他任何东西),并且当我期望MyCustomObject的孩子时,如果我给它一个null,我想知道它是一个null参数。



我将感谢任何能够将我从这种棘手的情况中解脱出来的人! / p>

更新



当我进入时,这是给我什么样的信息ArgumentSyntax对象:

  ArgumentSyntax参数异常
ContainsAnnotations:否
ContainsDiagnostics:否
ContainsDirectives :false
ContainsSkippedText:false
表达式:IdentifierNameSyntax IdentifierName异常
FullSpan:{[550..559)}
HasLeadingTrivia:错误
HasStructuredTrivia:错误
HasTrailingTrivia:假
IsMissing:假
IsStructuredTrivia:假
KindText: Argument
语言: C#
NameColon:空
父级(Microsoft.CodeAnalysis.SyntaxNode):ArgumentListSyntax ArgumentList(异常,异常)
ParentTrivia:SyntaxTrivia None
RawKind:8638
RefOrOutKeyword:语法令牌无
跨度:{[550..559)}
SpanStart:550


解决方案

您应该做的是获取参数(不是参数-它们是不同的东西!)并调用 SeguticModel.GetTypeInfo()上的ArgumentSyntax。这将为您提供要传递的表达式的类型。从那里您可以进行所需的任何检查。


I'm doing code analysis with Roslyn in order to validate that even though I have the following signature

public void MyMethod(object anObject, MyCustomObject customObject);

I only want to receive, as parameters, a string (1st) and a child from MyCustomObject (2nd). I have no power over the signature, it cannot be changed.

Here's what I did to evaluate my method (Here's a snippet)

    public void OnMethodInvocation(SyntaxNodeAnalysisContext context)
    {
        var invocation= context.Node as InvocationExpressionSyntax;
        var symbol = context.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;

        if (symbol?.Name.ToString()== "MyMethod")
        {
            var parameterList = invocation.Parameters;
        }

As of now, I can manipulate my IParameterSymbol objects from the property Parameters (symbol.Parameters). What I don't get is the following : I've gone through my result IEnumerable containing both my parameters, but because of the method signature, it expects to receive an object and a MyCustomObject instances. I'm not in a position (at the moment) to be certain that the first parameter is indeed an object and not a string (merely an example, could have been anything else) and that when I'm expecting a child of MyCustomObject, if I give it a null, I want to know it's a null parameter.

I'll be grateful to anyone who can un-stuck me from this sticky situation !

UPDATES

Here's what kind of information is given to me when I get into an ArgumentSyntax object :

ArgumentSyntax Argument exception
    ContainsAnnotations: false
    ContainsDiagnostics: false
    ContainsDirectives: false
    ContainsSkippedText: false
    Expression: IdentifierNameSyntax IdentifierName exception
    FullSpan: {[550..559)}
    HasLeadingTrivia: false
    HasStructuredTrivia: false
    HasTrailingTrivia: false
    IsMissing: false
    IsStructuredTrivia: false
    KindText: "Argument"
    Language: "C#"
    NameColon: null
    Parent (Microsoft.CodeAnalysis.SyntaxNode): ArgumentListSyntax ArgumentList (exception,exception)
    ParentTrivia: SyntaxTrivia None 
    RawKind: 8638
    RefOrOutKeyword: SyntaxToken None 
    Span: {[550..559)}
    SpanStart: 550

解决方案

What you should be doing is getting the arguments (not parameters -- they are different things!) and calling SemanticModel.GetTypeInfo() on the ArgumentSyntax. That'll give you the type of the expression being passed. From there you can do whatever checks you need.

这篇关于使用Roslyn时如何在方法中验证参数的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆