public class MyClass { private int myNumber = 10; } // Accessing the field 'myNumber' from MyClass var field = typeof(MyClass).GetField("myNumber", BindingFlags.NonPublic | BindingFlags.Instance); int value = (int)field.GetValue(myClassInstance); // returns the value 10
public class Person { public string Name { get; set; } public int Age { get; set; } } var person = new Person() { Name = "John", Age = 30 }; // Updating the value of the 'Age' field using Mono.CSharp Field var field = typeof(Person).GetField("Age"); field.SetValue(person, 40); // sets the value of 'Age' to 40In this example, we create a Person object with a Name and Age property. We use Mono.CSharp Field to access the Age field and set it to a new value of 40. This demonstrates how the library can be used to update values in fields of an object.