如何在编译时为未知类型的泛型函数创建泛型方法?

如何在编译时为未知类型的泛型函数创建泛型方法?

问题描述:

    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(thisnew 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