The GetValue
method of the TypeDescription
class is used to retrieve the value of a property or field from a given instance of an object. This method is particularly useful when you need to access a property or field dynamically by its name.
The GetValue
method of the TypeDescription
class is used to retrieve the value of a property or field from a given instance of an object. This method is particularly useful when you need to access a property or field dynamically by its name.
To use the GetValue
method, you need to provide two parameters:
instance
: The object instance from which you want to retrieve the value. This should be an instance of the type described by the TypeDescription
.name
: The name of the property or field whose value you want to retrieve. This should be a string representing the exact name of the property or field.The method returns an object
that represents the value of the specified property or field. You may need to cast this object to the appropriate type after retrieval.
// Example usage of TypeDescription.GetValue // Assume we have a class MyClass with a property named "MyProperty" public class MyClass { public int MyProperty { get; set; } } // Create an instance of MyClass MyClass myInstance = new MyClass { MyProperty = 42 }; // Create a TypeDescription for MyClass TypeDescription typeDescription = TypeLibrary.Get<MyClass>(); // Retrieve the value of "MyProperty" from myInstance object value = typeDescription.GetValue(myInstance, "MyProperty"); // Cast the value to the appropriate type int myPropertyValue = (int)value; // myPropertyValue should now be 42