如何在编译时为未知类型的泛型函数创建泛型方法? [英] How do I make a generic method for a generic function for a type unknown at compile time?
本文介绍了如何在编译时为未知类型的泛型函数创建泛型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class GenericAutoValidateControl
{
public GenericAutoValidateControl(Control ctrl)
{
Type t = ctrl.GetType(); //Get the control type
MethodInfo method = this.GetType().GetMethod("AddControlValidation", BindingFlags.NonPublic | BindingFlags.Static);
method.MakeGenericMethod(new Type[] { t });
method.Invoke(this, new object[] { ctrl });//this line throws error below
}
private static void AddControlValidation<T>(T ctrl)
{
//do stuff here...
}
}
Exception: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true
How can i do this? Thanks!
推荐答案
您好
我认为您正在寻找这个
http://www.codeproject.com/KB/dotnet/InvokeGenericMethods.aspx
这篇关于如何在编译时为未知类型的泛型函数创建泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文