How do I make a generic method for a generic function for a type unknown at compile time? RRS feed

  • Question

  •     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!
    Friday, September 2, 2011 6:33 PM

All replies