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

Зміст курсу

Advanced C# with .NET

Basic ReflectionBasic 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.cs

It is important to note that we cannot not use typeof function with objects, rather we use it with class names:

cs

index.cs

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.cs

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.cs

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

cs

index.cs

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.cs

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.cs

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#?

Виберіть правильну відповідь

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

Виберіть правильну відповідь

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

Виберіть правильну відповідь

Все було зрозуміло?

Секція 4. Розділ 7
course content

Зміст курсу

Advanced C# with .NET

Basic ReflectionBasic 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.cs

It is important to note that we cannot not use typeof function with objects, rather we use it with class names:

cs

index.cs

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.cs

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.cs

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

cs

index.cs

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.cs

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.cs

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#?

Виберіть правильну відповідь

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

Виберіть правильну відповідь

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

Виберіть правильну відповідь

Все було зрозуміло?

Секція 4. Розділ 7
some-alt