Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Reflection | Generics & Reflection
Advanced C# with .NET
course content

Course Content

Advanced C# with .NET

Advanced C# with .NET

1. Introduction to Desktop Development with .NET
2. Interactive GUI
3. Threading
4. Generics & Reflection

bookBasic 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:

cs

index

copy
12345678910
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:

cs

index

copy
1234
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:

cs

index

copy
12
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.

cs

index

copy
1
MethodInfo[] methods = exampleType.GetMethods();

We can also retrieve a single method from a class using GetMethod:

cs

index

copy
1
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:

cs

index

copy
1
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:

cs

index

copy
12
method.Invoke(exampleObject, null); method.Invoke(exampleObject, new object[] { "first", "second", 1, 2, 3.4f, 5.67, true });
1. What does the GetType method do in C#?
2. How can you get a list of all properties of a type using reflection?
3. What is the correct method of invoking a method which has two arguments of type `string` and `int` respectively?
What does the GetType method do in C#?

What does the GetType method do in C#?

Select the correct answer

How can you get a list of all properties of a type using reflection?

How can you get a list of all properties of a type using reflection?

Select the correct answer

What is the correct method of invoking a method which has two arguments of type `string` and `int` respectively?

What is the correct method of invoking a method which has two arguments of type string and int respectively?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 7
We're sorry to hear that something went wrong. What happened?
some-alt