2016-05-07 19 views
2

我得到IL與擴展方法方法:如何使用roslyn查找MethodDeclaractionSyntax參數的system.Type?

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

因爲我的解決方案具有過載(同名)的方法我得到異常(有不止一個)。

所以我需要使用下面的方法,需要類型[]。

//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 
} 

回答

2

你會需要符號這裏。

從你ParameterSyntax P,使用SemanticModel.GetDeclaredSymbol得到IParameterSymbol,然後看它的Type得到ITypeSymbol你感興趣的內容。

+0

我需要的System.Type發送的getMethod。如何從得到的System.Type ITypeSymbol? – mohsen

+4

你不能 - 反射和羅斯林是不同的世界。您可以使用Type.MetadataName找到解決方法,然後通過反射來找到正確的類型。 –

相關問題