Conteúdo do Curso
Advanced C# with .NET
Advanced C# with .NET
Basic Reflection
In this chapter, we will cover the basic and important features of reflection which might be useful in Web and UI Development with C#.
Getting the Types of Objects at Runtime
The first feature is the one that we had already explored in the last chapter. It is the GetType
method. The GetType
method gives us the type of any object. This can be useful when we want to make decisions based on the types of objects:
index
static float ExampleMethod(object value) { if(value.GetType() == typeof(int)) { return (float) value * 2; } return (float) value; }
It is important to note that we cannot not use typeof
function with objects, rather we use it with class names:
index
int value = 10; typeof(value) // Incorrect typeof(int) // Correct, returns "System.Int32" typeof(long) // Correct, returns "System.Int64"
There is a class called Type
which is defined in the System.Reflection
namespace. We can create variables of type Type
and store the result of GetType
or typeof
into those variables:
index
Type exampleVar1 = value.GetType(); Type exampleVar2 = typeof(int);
The typeof
function is not exactly the same as the GetType()
method. Both return a Type
instance, however, typeof
returns the Type
instance of a class at compile time, while the GetType()
method returns the Type
instance of an object at the runtime. This is because when using typeof(int)
, the program already essentially knows that the data type is int
, it simply has to translate it to a Type
instance. While in case of value.GetType()
the program has to figure out the type of the value
object.
Getting a List of all the Properties of a Type
The Type
objects have a method called GetProperties
which can retrieve the list of all the public properties in the target type.
The property references can be stored into an array of type PropertyInfo
. The PropertyInfo
class is defined in the System.Reflection
namespace and has a variety of useful attributes and methods.
Reference to a single specific property can also be retrieved, by using the GetProperty
method.
The GetProperty
method will return the property which matches the propertyName
. Otherwise it returns null
.
Getting a List of all the Methods of a Type
We can get a list of all the methods of a specific type by calling the GetMethods
method of its Type
instance.
The method references can be stored in an array of type MethodInfo
which is also defined in the System.Reflection
namespace.
index
MethodInfo[] methods = exampleType.GetMethods();
We can also retrieve a single method from a class using GetMethod
:
index
MethodInfo method = exampleType.GetMethod("methodName");
The GetMethod
returns the first method which matches the name methodName
. If there is no such method, it returns null
.
MethodInfo
class has a method called Invoke
which takes the following arguments:
index
Invoke(objectReference, arguments);
Here objectReference needs to be an object of the type from which the method is from. The arguments
can be represented by either null
in case of no arguments, or an array of type object
. For-example:
index
method.Invoke(exampleObject, null); method.Invoke(exampleObject, new object[] { "first", "second", 1, 2, 3.4f, 5.67, true });
Obrigado pelo seu feedback!