Here's a complete example showcasing reflection in .NET:
using System; using System.Reflection; public class Person { public string Name { get; set; } public int Age { get; set; } public void SayHello() { Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old."); } } public class Program { public static void Main() { // Create an instance of the Person class Person person = new Person { Name = "John", Age = 30 }; // Get the type of the Person class using reflection Type personType = typeof(Person); // Get the property information using reflection PropertyInfo nameProperty = personType.GetProperty("Name"); PropertyInfo ageProperty = personType.GetProperty("Age"); // Set the property values using reflection nameProperty.SetValue(person, "Jane"); ageProperty.SetValue(person, 25); // Get the method information using reflection MethodInfo sayHelloMethod = personType.GetMethod("SayHello"); // Invoke the method using reflection sayHelloMethod.Invoke(person, null); } } In this example, we have a Person class with properties Name and Age, as well as a SayHello method. We utilize reflection to access and manipulate these members.
We start by obtaining the Type object for the Person class using typeof(Person).
Using the Type object, we retrieve the PropertyInfo objects for the Name and Age properties using GetProperty. With these PropertyInfo objects, we can get and set their values using GetValue and SetValue.
Next, we use GetMethod to retrieve the MethodInfo object for the SayHello method. With this MethodInfo, we can invoke the method using Invoke passing the instance (person) on which the method should be invoked and an empty parameter array (null) since the SayHello method doesn't have any parameters.
Running this example will output: "Hello, my name is Jane and I am 25 years old." as the SayHello method is invoked on the modified instance of Person.
This example demonstrates the basic usage of reflection in .NET to access properties and invoke methods dynamically. Reflection is a powerful tool for working with types and members at runtime, but it should be used judiciously due to its performance implications and potential for errors if not used correctly.
"C# reflection get type information example"
// Example of getting type information using reflection in C# Type myType = typeof(MyClass); Console.WriteLine($"Type Name: {myType.Name}, Full Name: {myType.FullName}"); "C# reflection create instance example"
// Example of creating an instance using reflection in C# Type myType = typeof(MyClass); object instance = Activator.CreateInstance(myType);
"C# reflection get method information example"
// Example of getting method information using reflection in C# Type myType = typeof(MyClass); MethodInfo myMethod = myType.GetMethod("MyMethod"); Console.WriteLine($"Method Name: {myMethod.Name}, Return Type: {myMethod.ReturnType}"); "C# reflection invoke method example"
// Example of invoking a method using reflection in C# Type myType = typeof(MyClass); object instance = Activator.CreateInstance(myType); MethodInfo myMethod = myType.GetMethod("MyMethod"); object result = myMethod.Invoke(instance, null); "C# reflection get property information example"
// Example of getting property information using reflection in C# Type myType = typeof(MyClass); PropertyInfo myProperty = myType.GetProperty("MyProperty"); Console.WriteLine($"Property Name: {myProperty.Name}, Property Type: {myProperty.PropertyType}"); "C# reflection get field information example"
// Example of getting field information using reflection in C# Type myType = typeof(MyClass); FieldInfo myField = myType.GetField("MyField"); Console.WriteLine($"Field Name: {myField.Name}, Field Type: {myField.FieldType}"); "C# reflection enumerate assembly types example"
// Example of enumerating types in an assembly using reflection in C# Assembly myAssembly = Assembly.GetExecutingAssembly(); foreach (Type type in myAssembly.GetTypes()) { Console.WriteLine($"Type Name: {type.Name}"); } "C# reflection custom attribute example"
// Example of using custom attributes with reflection in C# [MyCustomAttribute("Example")] public class MyClass { } // Retrieve and use the custom attribute Type myType = typeof(MyClass); MyCustomAttribute attribute = (MyCustomAttribute)Attribute.GetCustomAttribute(myType, typeof(MyCustomAttribute)); Console.WriteLine($"Attribute Value: {attribute.Value}"); "C# reflection dynamic invocation example"
// Example of dynamically invoking methods using reflection in C# Type myType = typeof(MyClass); object instance = Activator.CreateInstance(myType); // Dynamic method invocation myType.InvokeMember("MyDynamicMethod", BindingFlags.InvokeMethod, null, instance, null); dotnet-httpclient exoplayer gis jelly voip datagridviewrow md5sum angular-builder catplot version