Invoking methods and using properties

In this section, we'll take a look at how we can access the properties and methods of a custom class at runtime using reflection.

This example serves to give you an idea of how we can access methods and properties using reflection at runtime. However, based on your requirements, you can dynamically access properties, their types and methods, and their parameters.

We created a new custom class where we defined two integer type properties: Number1 and Number2. Then, we defined a public method that accepts a parameter and returns a number to be added or subtracted

internal class CustomClass1
{
public int Number1 { get; set; }
public int Number2 { get; set; }

public int Getresult(string action)
{
int result = 0;
switch (action)
{
case "Add":
result = Number1 + Number2;
Console.WriteLine($"Sum of numbers {Number1} and {Number2} is : {result}");
break;

case "Subtract":
result = Number1 - Number2;
Console.WriteLine($"Difference of numbers {Number1} and {Number2} is : {result}");
break;
}
return result;
}
}

Then, we created a simple method where we could access the properties and methods of the custom class that we created previously. In the first line, we retrieved the type information of the custom class. Using this type, we created an instance of the class using the Activator.CreateInstance method. Now, using the Getproperties method of the type we retrieved, we accessed all the properties and set a value to each of them based on the property name.

In the next line, using the Type information of the object, we retrieve MethodInfo using the GetMethod method. Then, we invoked the public method of the custom class twice with two different actions called Add and Subtract:

public static void GetResults()
{
Type objType = typeof(CustomClass1);
object obj = Activator.CreateInstance(objType);
foreach (PropertyInfo prop in objType.GetProperties())
{
if(prop.Name =="Number1")
prop.SetValue(obj, 100);
if (prop.Name == "Number2")
prop.SetValue(obj, 50);
}

MethodInfo mInfo = objType.GetMethod("Getresult");
mInfo.Invoke(obj, new string[] { "Add" });
mInfo.Invoke(obj, new string[] { "Subtract" });
}

If you run the program and debug every line, you will see that each property has been retrieved and values have been set. The following is the output of the program:

Sum of numbers 100 and 50 is : 150
Difference of numbers 100 and 50 is : 50
Press any key to exit.

This sample is a simple one, as we created two properties, both of the integer type. However, in real time, such simple scenarios may not exist. Therefore, at runtime, you need to use the GetType method in order to understand the type of property retrieved.

Additionally, in the example we were able to get the type of the Custom class where we hardcoded it. Using generics, we can even pass the class at runtime and get the info type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.44.229