vendredi 30 juin 2017

Invoking generic method with default datetime parameter through reflection

I have simple generic method with following signature:

public static T SafeGetValue<T>(this XElement xElem, string attrName, 
                                 T defaultValue = default(T), bool throwExceptions = false)

Which I want to invoke like that:

var genericMethod = typeof(XElementExtensions).GetMethod("SafeGetValue");
genericMethod = genericMethod.MakeGenericMethod(type);
genericMethod.Invoke(null,new[] { xElem, "attr", Type.Missing, Type.Missing });

type variable is supplied and it is meant to be different for different invocations (typeof(int), typeof(bool), typeof(string) and etc.. ) , but when I supply it with DateTime, I got runtime exception:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code.Additional information: Encountered an invalid type for a default value.





Aucun commentaire:

Enregistrer un commentaire