Browse DevX
Sign up for e-mail newsletters from DevX


Reflection Part I: Discovery and Execution : Page 2

Many developers see reflection as something new and mysterious. In the first of this two-part article we explain reflection in the .NET Framework and demonstrate where and how you can use this new tool in your applications.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Namespaces and Classes
There are two namespaces that contain the Reflection classes, System.Reflection and System.Reflection.Emit. The Reflection namespace contains objects related to type discovery and execution while the Reflection.Emit namespace dynamically generates code at runtime (this is the focus of part 2 of this article). Table 1 provides a quick and easy reference to the key classes you'll use when working with reflection.

Reflection Discovery: Searching and Filtering
Earlier in this article you learned how to load an assembly and gain access to its types. In fact, for any given .NET assembly you can return all of its types using Assembly.GetTypes. This method returns all of the global or public types (depending on your .NET security model or context) stored in a given assembly. For example, the following code creates an Assembly instance based on itself and iterates through the assembly's types:

Imports System.Reflection Module Basics Sub Main() Dim mi As MethodInfo Dim myAssembly As [Assembly] myAssembly = GetType(Basics).Assembly Dim t As Type For Each t In myAssembly.GetTypes() Console.WriteLine("Type:=" & t.Name) Next Console.ReadLine() End Sub End Module

This code is useful for looping through all of the public types exposed on a given assembly, but what if you have a large assembly or are simply after specific types such as a constructor or a property? If you've spent any time at all looking at the .NET Framework Class Library, you know that one Assembly can contain a large number of types. You may not need to know every type; you might prefer to filter or search for a very specific type or types. Fortunately, the System.Type class exposes a number of methods for accessing, filtering, and searching for specific types inside a given assembly.

Direct Access
Direct access to a given type implies you've made a decision and are now looking for an exact type. Perhaps your application queries the user for the type they are interested in, or perhaps you knew from the start the specific type to look for. In any case, the System.Type class provides a number of methods that provide direct access to specific types. Methods like GetConstructor, GetMethod, GetProperty, and GetEvent allow you to target their specific types. For example, suppose you have the following class:

Public Class SomeClass Public Sub New() End Sub Public Sub New(ByVal someValue As Int32) End Sub Public Sub New(ByVal someValue As Int32, _ ByVal someOtherValue As Int32) End Sub Public Sub SomeMethod() End Sub End Class

Here is a class with three empty constructors and an empty method. Now assume that you want to access the constructor that takes one integer value as its only parameter. To do so you would use the GetConstructor method of the Type class. This method allows you to pass an array of objects that represent parameters for a given constructor. When executed, the method searches a type for any constructor that matches the signature defined by the array of parameters. GetConstructor then returns a ConstructorInfo object for your use (perhaps you want to invoke the constructor). For example, you would first create the array of parameters as follows:

Dim ts() As Type = {GetType(Int32)}

Finally, you would then call GetConstructor of your Type object as follows:

Dim ci As ConstructorInfo = _ GetType(SomeClass).GetConstructor(ts)

Similarly, Type.GetMethod provides you with direct access to the methods on a given object.

This method returns a MethodInfo instance for your use. It simply takes the name of the given method as a parameter. For example:

Dim mi As MethodInfo = _ GetType(SomeClass).GetMethod("SomeMethod")

You might wonder, "What if I have two methods with the same name but different signatures in the same class?" Well, in the previous case you would get an ambiguous matching error. However, there are a number of versions of all of these direct access methods that allow you to pinpoint specific types. For instance the GetConstructor method can filter methods based on an array of parameters; or you could filter based on calling conventions, return type, etc. You can apply this same pattern to directly access a specific property or an event contained by your type.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date