简体   繁体   English

如何从 Roslyn 的另一个方法中作为参数传递的方法获取返回类型?

[英]How can I obtain return type from method passed as an argument in another method with Roslyn?

Lets say that I have a code:假设我有一个代码:

public class Test {
    private readonly IFactory _factory;
    private readonly ISomeClass _someClass;

    public Test(IFactory factory, ISomeClass someClass)
    {
        _factory = factory;
        _someClass = someClass;
    }

    ....

    public void TestMethod() {
        _someClass.Do(_factory.CreateSomeObject());
    }
}

public class Factory {
    public SomeObject CreateSomeObject() {
        return new SomeObject();
    }
}

public class SomeClass {
    public void Do(SomeObject obj){
        ....
    }
}

I would like to get return type of CreateSomeObject from InvocationExpressionSyntax of someClass.Do(_factory.CreateSomeObject());我想获得的返回类型CreateSomeObjectInvocationExpressionSyntaxsomeClass.Do(_factory.CreateSomeObject()); Is it possible?是否可以?

I have a list of arguments (ArgumentSyntax) but I have no clue how to get method return type from ArgumentSyntax.我有一个参数列表(ArgumentSyntax),但我不知道如何从 ArgumentSyntax 获取方法返回类型。 Is there better and easier way to do it other then scanning a solution for Factory class and analyzing CreateSomeObject method?除了扫描Factory类的解决方案并分析CreateSomeObject方法之外,还有更好更简单的方法吗?

Yes, it is possible.对的,这是可能的。 You would need to use Microsoft.CodeAnalysis.SemanticModel for it.您需要为此使用Microsoft.CodeAnalysis.SemanticModel

I assume you have CSharpCompilation and SyntaxTree already available, so you would go in your case with something like this:我假设您已经有 CSharpCompilation 和 SyntaxTree 可用,因此您可以使用以下内容:

SemanticModel model = compilation.GetSemanticModel(tree);           
var methodSyntax = tree.GetRoot().DescendantNodes()
    .OfType<MethodDeclarationSyntax>()
    .FirstOrDefault(x => x.Identifier.Text == "TestMethod");
var memberAccessSyntax = methodSyntax.DescendantNodes()
    .OfType<MemberAccessExpressionSyntax>()
    .FirstOrDefault(x => x.Name.Identifier.Text == "CreateSomeObject");
var accessSymbol = model.GetSymbolInfo(memberAccessSyntax);
IMethodSymbol methodSymbol = (Microsoft.CodeAnalysis.IMethodSymbol)accessSymbol.Symbol;
ITypeSymbol returnType = methodSymbol.ReturnType;

Once you get the desired SyntaxNode out of the semantic model, you need to get its SymbolInfo and properly cast it, to get its ReturnType which does the trick.一旦您从语义模型中获得所需的SyntaxNode ,您需要获取它的SymbolInfo并正确地转换它,以获得它的ReturnType这一点。

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

相关问题 C#泛型如何获取没有对象类型作为参数的泛型方法的参数类型? - C# Generics How Can I get the type passed as an argument for a generic method with no object type as a parameter? 如何在Roslyn中检查方法参数类型/返回类型是否通用? - How to check if method parameter type/return type is generic, in Roslyn? 如何在Main()中使用方法(其参数已由ref传递)的返回值 - How can I use a method's (whose argument has been passed by ref) return value in Main() 从传递给方法的Type中设置返回类型 - Set return type from Type that passed into method 我怎样才能让这个方法从使用中推断类型参数? - How can I get this method to infer type argument from usage? 如何从具有通用返回类型的方法返回原语? - How can I return a primitive from a method with a generic return type? 犀牛如何模拟返回传入参数的方法? - How can I rhino mock a method that returns the passed in argument? 我如何创建ac#方法,该方法将返回与作为参数的具有相同元素类型的相同集合类型? - How can i create a c# method that will return the same collection type as as argument with a different element type? 如何从方法返回匿名类型? - How can I return an anonymous type from a method? 如何从另一个 class 调用这种类型的扩展方法 - How can I call this type of extension method from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM