code_junkie

Dynamically getting default of a parameter type

Nov 13th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. object blankObject = null;
  2. foreach (var paramInfo in _methods[methodName].Key.GetParameters())
  3. {
  4. if (paramInfo.Name == paramName)
  5. {
  6. Type paramType = paramInfo.ParameterType;
  7. blankObject = (object)default(paramType);
  8. }
  9. }
  10. parameters[i] = blankObject;
  11.  
  12. public object GetDefault(Type type)
  13. {
  14. return type.IsValueType ? Activator.CreateInstance(type) : null;
  15. }
  16.  
  17. public void MyMethod<T>(T obj)
  18. {
  19. T myvar = default(T);
  20. }
Add Comment
Please, Sign In to add comment