[英]Pass a class gotten from Reflection to a generic method
這是我要嘗試的一個虛擬示例:
var ass = Assembly.Load("Dummy.Class.FullName");
var yy =
from t in ass.GetTypes()
let attributes = t.GetCustomAttributes(typeof(MyTestAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = t, Attributes = attributes.Cast<MyTestAttribute>() };
foreach (var x in yy)
{
TestOpen<typeof(x.Type)>();
}
private void TestOpen<TEntity>() where TEntity : Entity, new()
{
}
我無法獲得類定義並以這種方式傳遞給通用方法,我嘗試了一切,我想我特別想念了一些方法,該方法正在等待某個編譯的類,並且從反思的角度來看,我不能得到它,對嗎?
干杯
您可以使用MakeGenericMethod生成正確的方法定義,然后通過反射進行調用。
Type thisType = this.GetType();
var mi = thisType.GetMethod("TestOpen");
foreach (var x in yy)
{
var gmi = mi.MakeGenericMethod(x.Type);
gmi.Invoke(this, null);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.