为使用Xml文档成员的方法获取MethodInfo的通用方法.姓名



我正在尝试获取系统。反射使用Xml文档文件项中的成员名称的方法的MethodInfo元数据。

这是基本代码。为了简洁起见,我省略了从Member.name字符串中获取值的样板。

// The following variables are all parsed out of the 'name' string from the 
// XML documentation for each entry (from 1st example below):
// Member: "ToInt"
// Namespace: "Library.Extensions"
// ClassName: "ConvertExtensions"    
// AssemblyToDocument: Library 
// ParameterArray: "System.String,System.Int32"
// Uses the GetMethod() method with the parameter 'types' overload
// as there can be multiple signatures for a particular Method name.  
string typeName = $"{Namespace}.{ClassName}, {AssemblyToDocument}";        
var types = ToTypeArray(ParameterArray);
MethodInfo methodInfo =  typeName.GetMethod(Member, types);

private Type[] ToTypeArray(string[] typeNames)
{
var types = new List<Type>();
foreach (string name in typeNames)
{
types.Add(Type.GetType(name));
}
return types.ToArray();
}

这适用于只有一个签名或像下面这样有几个简单签名的方法。

<member name="M:Library.Extensions.ConvertExtensions.ToInt(System.String,System.Int32)"></member>
<member name="M:Library.Extensions.ConvertExtensions.ToBool(System.String)"></member>

但是对于更复杂的示例进行了分解,例如方法重载。这个对于更复杂的类型,文档XML中的字符串无法按类型进行解析。GetType(字符串(。

<member name="M:Library.Extensions.ConvertExtensions.ToBool(System.String,System.String,System.Boolean)"></member>
<member name="M:Library.Extensions.IEnumerableExtensions.ElementInOrDefault``1(System.Collections.Generic.IEnumerable{``0},System.Int32)"></member>

目前,我可以获得xml文档文件中大约一半条目的MemberInfo。

是否有一种可靠、通用的方法可以使用XML文档文件中的member.name获取MethodInfo对象?

作为How to get MethodInfo with many overloading functions and complicated parameter,这个主题可能更清晰。

我假设您成功地解析了XML文档,并得到了类似的字符串

NetCoreScripts.StackOverFlow.ReflectionTopic.MyObject(System.String,System.String,System.Boolean)

在那里,您可以使用任何分离器方法将其拆分为2个元素:

第一个是:NetCoreScripts.StackOverFlow.ReflectionTopic.MyObject

第二个是数组:System.StringSystem.StringSystem.Boolean(3个参数的顺序必须相同(

private static Type[] ToTypeArray(string[] typeNames)
{
var types = new List<Type>();
foreach (string name in typeNames)
{
types.Add(Type.GetType(name));
}
return types.ToArray();
}

我重用你的ToTypeArray并创建一个模型

namespace NetCoreScripts.StackOverFlow.ReflectionTopic
{
public class MyObject
{
public void DoMore(int parameter) { }
public void DoMore(string str1, string str2, bool bool1) { }
public void DoMore(string str, int num) { }
public void DoMore(List<string> parameter) { }
public void DoMore(List<string> parameter, int param) { }
public void DoMore(List<List<string>> parameter, bool? nullableBool, bool isTrue) { }
}
}

主要调用方法为

public static void GetMyMethod(Type type, string[] arr)
{
var parammeters = ToTypeArray(arr);
var method = type.GetMethod("DoMore", parammeters);
Console.WriteLine(method.Name);
}
public static void MainFunc()
{
var type = Type.GetType("NetCoreScripts.StackOverFlow.ReflectionTopic.MyObject");
GetMyMethod(type, new string[] { "System.Int32" });
GetMyMethod(type, new string[] { "System.String", "System.String", "System.Boolean" });
GetMyMethod(type, new string[] { "System.String", "System.Int32" });
GetMyMethod(type, new string[] { "System.Collections.Generic.List`1[[System.String]]" });
GetMyMethod(type, new string[] { "System.Collections.Generic.List`1[[System.String]]", "System.Int32" });
GetMyMethod(type, new string[] { "System.Collections.Generic.List`1[[System.Collections.Generic.List`1[[System.String]]]]", "System.Nullable`1[[System.Boolean]]", "System.Boolean" });
}

为了知道确切的名称,我建议在配对测试中使用全名,写和读Xml会更容易

Console.WriteLine(typeof(bool?).FullName); 
//System.Nullable`1[[System.Boolean, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
Console.WriteLine(Type.GetType("System.Nullable`1[[System.Boolean]]").FullName);
//For shorter

希望它能帮助

最新更新