[英]C# - Investigating "Method' information using Reflection?
我的意圖是使用反射研究類型的“方法”,以驗證以下內容:
方法應該是實例方法並且是公共的。
本質上采用參數“ params”和void。
該方法不進行遞歸調用。
我開始是:
static void ProcessMethodInfo(Type t)
{
MethodInfo[] info = t.GetMethods();
foreach (MethodInfo mi in info)
{
// How to check the conditions here ?
}
}
但是我不知道該如何進一步。 需要幫助。
好吧,如果您的評分為3,則表示所檢查的方法應該是非遞歸的; 那就很痛苦-您需要解析IL。 但是對於其他人;
Type type = ...
var qry = from method in type.GetMethods(
BindingFlags.Instance | BindingFlags.Public)
where method.ReturnType == typeof(void)
let parameters = method.GetParameters()
where parameters.Length == 1
&& parameters[0].ParameterType.IsArray
&& Attribute.IsDefined(parameters[0], typeof(ParamArrayAttribute))
select method;
foreach (var method in qry)
{
Console.WriteLine(method.Name);
}
全部:使用google;)
我認為您無法使用反射檢測到項目3。
檢查MethodInfo類的以下成員:
為了能夠檢查該方法是否是遞歸的,我認為您需要的不僅僅是簡單的反射。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.