Possible Duplicate:
C# - Programmatic equivalent of default(Type)
Is there a method or property in the Type class (or elsewhere) that I can use to find the value of default(T)?
Possible Duplicate:
C# - Programmatic equivalent of default(Type)
Is there a method or property in the Type class (or elsewhere) that I can use to find the value of default(T)?
Just check type.IsValueType
; if that is true use Activator.CreateInstance(type)
- otherwise it is null.
It also helps that you can pass null to SetValue on a PropertyInfo or FieldInfo and it will work for value-types with even for int, float etc.
object obj = type.IsValueType ? Activator.CreateInstance(type) : null;