fork(1) download
  1. using System;
  2. using System.Reflection;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. // your code goes here
  9. var myType = new MyType();
  10. Console.WriteLine("before");
  11. Console.WriteLine(myType.one + " " + myType.two);
  12. myType = SetAllFieldsAsDefault(myType) as MyType;
  13. Console.WriteLine("after");
  14. Console.WriteLine(myType.one + " " + myType.two);
  15. }
  16.  
  17.  
  18. class MyType {
  19. internal double one = -1.1;
  20. internal string test = "hi";
  21. internal double two = 3.3;
  22. }
  23.  
  24. private static object SetAllFieldsAsDefault(object _register)
  25. {
  26. Type register = _register.GetType();
  27. var fields = register.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  28. foreach (var field in fields)
  29. {
  30. if (field.FieldType != typeof(double)) continue;
  31. field.SetValue(_register, 0);
  32. }
  33. return _register;
  34. }
  35. }
Success #stdin #stdout 0s 131648KB
stdin
Standard input is empty
stdout
before
-1.1 3.3
after
0 0