public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); // Get the Type and FieldInfo. Type MyTypea = typeof(Myfielda); FieldInfo Myfieldinfoa = MyTypea.GetField("SomeField", BindingFlags.NonPublic | BindingFlags.Instance); Type MyTypeb = typeof(Myfieldb); FieldInfo Myfieldinfob = MyTypeb.GetField("SomeField"); // Get and display the IsPublic and IsPrivate property values. Console.Write("\n{0}.", MyTypea.FullName); Console.Write("{0} - ", Myfieldinfoa.Name); Console.Write("{0}", Myfielda.Field); Console.Write("\n IsPublic = {0}", Myfieldinfoa.IsPublic); Console.Write("\n IsPrivate = {0}", Myfieldinfoa.IsPrivate); Console.Write("\n{0}.", MyTypeb.FullName); Console.Write("{0} - ", Myfieldinfob.Name); Console.Write("{0};", Myfieldb.SomeField); Console.Write("\n IsPublic = {0}", Myfieldinfob.IsPublic); Console.Write("\n IsPrivate = {0}", Myfieldinfob.IsPrivate); return(0); }
public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); //Get the Type and FieldInfo. Type MyTypea = typeof(Myfielda); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.Public | BindingFlags.Instance); Type MyTypeb = typeof(Myfieldb); FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.Public | BindingFlags.Instance); //Modify the fields. //Note that Myfieldb is not modified, as it is //read-only (IsInitOnly is True). Myfielda.field = "A - modified"; //Myfieldb.field = "B - modified"; //For the first field, get and display the name, field, and IsInitOnly state. Console.Write("\n{0} - {1}, IsInitOnly = {2} ", MyTypea.FullName, Myfieldinfoa.GetValue(Myfielda), Myfieldinfoa.IsInitOnly); //For the second field get and display the name, field, and IsInitOnly state. Console.Write("\n{0} - {1}, IsInitOnly = {2} ", MyTypeb.FullName, Myfieldinfob.GetValue(Myfieldb), Myfieldinfob.IsInitOnly); return(0); }
public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); // Get the Type and FieldInfo. Type MyTypea = typeof(Myfielda); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic | BindingFlags.Instance); Type MyTypeb = typeof(Myfieldb); FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic | BindingFlags.Static); // For the first field, get and display the name, field, and IsStatic property value. Console.Write("\n{0} - ", MyTypea.FullName); Console.Write("{0}; ", Myfieldinfoa.GetValue(Myfielda)); Console.Write("IsStatic - {0}", Myfieldinfoa.IsStatic); // For the second field get and display the name, field, and IsStatic property value. Console.Write("\n{0} - ", MyTypeb.FullName); Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldb)); Console.Write("IsStatic - {0}", Myfieldinfob.IsStatic); return(0); }