gpt4 book ai didi

c# - 如何使用 roslyn 查找 MethodDeclactionSyntax 参数的 system.Type?

转载 作者:行者123 更新时间:2023-11-30 23:25:51 26 4
gpt4 key购买 nike

我得到带有扩展方法的方法的 IL:

public static byte[] getIL(this Type t,string nameOfMethod)
{
MethodBody mb=t.GetMethods(BindingFalgs...).Where(m=>m.Name ==nameOfMethod ).Single();
return mb.GetMethodBody().GetIlAsByteArray();
}

因为我的解决方案有 Overload(同名)方法,所以我得到了异常(有多个)。

所以我需要使用下面需要 Type[] 的方法。

//this will replace in above method
m.GetMethod("NameOfMethod",ArrayTypeOfParameter);

但是我怎样才能从 TypeSyntax 得到 Type 呢?

public override SyntaxNode VisitMethodDeclarationSyntax(MethodDeclarationSyntax m)
{
foreach(ParameterSyntax p in m.ParameterList.Parameters)
{
TypeSyntax t=p.Type;
//how get system.Type here
}

最佳答案

这里你需要符号

从您的ParameterSyntax p,使用SemanticModel.GetDeclaredSymbol 获取IParameterSymbol,然后查看其Type 获取您感兴趣的 ITypeSymbol

关于c# - 如何使用 roslyn 查找 MethodDeclactionSyntax 参数的 system.Type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37088879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com