Advertisement
elena1234

Reflection - get type and fields

May 28th, 2021 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1.            
  2.            Type type = typeof(Car);
  3.            // Type type = Type.GetType("Namespace.ClassName");
  4.            // string fullName = typeOf(SomeClass).FullName;
  5.            // string name = typeOf(SomeClass).Name;
  6.            // Type baseType = typeof(SomeClass).BaseType;
  7.  
  8.           //  var field = type.GetField("fieldName"); // without binding flags given only public or return null
  9.  
  10.             var fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);        
  11.             foreach (var field in fields)
  12.             {
  13.                 Console.WriteLine(field); // field.Name or field.FieldType
  14.                 var fieldValue = field.GetValue(instance);
  15.             }          
  16.  
  17.  
  18. // var field = type.GetField($"{fieldAsString}", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
  19.  
  20. // var fieldValue = field.GetValue(instance);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement