Browse DevX
Sign up for e-mail newsletters from DevX


Reflection Part I: Discovery and Execution : Page 3

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 System.Type class also provides a number of methods for returning a filtered set of types contained inside a class or another type. The methods GetConstructors, GetMethods, GetProperties, and GetEvents allow you to either return all of the given types as an array or supply filter criteria to only return a specific set of types.

A typical filter involves setting binding flags. Binding flags represent search criteria. You use values of the BindingFlags enumeration to represent things such as public or non-public types. You can also indicate flags or static members. You can even combine flags to further narrow your search. Consider the following basic class:

Public Class SomeClass Public Sub SomeMethod() End Sub Public Shared Sub SomeSharedMethod() End Sub Public Shared Sub SomeOtherSharedMethod() End Sub End Class

You can see that there are three public methods, two of which are static (or shared). Suppose that through reflection you want to find all public static methods for the SomeClass Type. You would call GetMethods and pass BindingFlag enumeration values such as the bindingAttr parameter. The following code retrieves an array of all methods in SomeClass that are both public and static:

Dim mi As MethodInfo() = _ GetType(SomeClass).GetMethods( _ BindingFlags.Public Or BindingFlags.Static)

You can use this same technique to return private types (provided you have the correct permission). To do so you would use the BindingFlag enumeration value BindingFlags.NonPublic. This can be combined with BindingFlags.Instance to return all instance methods that are private. This same pattern can be applied to return constructors, properties, and events.

As you may have guessed, searching is a very similar process to filtering. The only real difference is that searching is done via a more abstract method of System.Type: FindMembers. Rather than call a specific filter such as GetEvents you might use FindMembers and pass the value MemberTypes.Events as the memberType parameter. If you need a custom filter that doesn't involve a specific type, you can use the parameters of FindMembers to satisfy a number of different searching requirements. Here's an example.

Notice that the following Visual Basic class defines three fields: two private and one public.

Public Class SomeClass Private myPrvField1 As Int32 = 15 Private myPrvField2 As String = _ "Some private field" Public myPubField1 As Decimal = 1.03 End Class

Suppose that you need to use the FindMembers method of the Type class to return the private fields on an instance of SomeClass and display their values. You would do so by indicating the value Field of the enumeration MemberTypes for the memberTypes parameter of FindMembers. You then can indicate your BindingFlags, and FindMembers will return an array of MemberInfo objects that match your search criteria. The following code snippet provides an example:

Dim memInfo As MemberInfo() Dim fi As FieldInfo Dim sc As New SomeClass() memInfo = sc.GetType.FindMembers( _ MemberTypes.Field, BindingFlags.NonPublic Or _ BindingFlags.Instance, Nothing, Nothing) Dim i As Int16 For i = 0 To memInfo.GetUpperBound(0) fi = CType(memInfo(i), FieldInfo) Console.WriteLine(fi.GetValue(sc)) Next

Once you find the target members, the code converts them into actual FieldInfo objects and is able to query them for their values.

Custom Searching
Even with all the aforementioned type searching capabilities you may find that you need to define a custom type search. Custom searching involves the method from the previous example, FindMembers. You may have noticed that in the previous example we set two of FindMembers' parameters (filter and filterCriteria) to Nothing. The filter parameter takes an instance of the MemberFilter delegate. Defining a delegate allows you to define custom search logic. This delegate simply receives a MemberInfo object that represents a member that meets all the other search criteria.

The filterCriteria parameter can be any .NET object. You can use this parameter to define your custom search criteria. This can be as simple as a string or as involved as a custom object. Here is an example.

Image you have a class called SomeClass and that it defines three properties: Name, Id, and Type. Now suppose that you need a filter that searches classes to find only the properties Name and Id. We've provided a very simple example and of course you could return all properties, loop through them, and filter out those not named Name or Id. Take a look at the console application in Listing 1.

This application defines the delegate, MySearchDelegate, to customize the search. It creates the custom object, filterObject, with two fields that help define our search criteria. The application then calls FindMembers and indicates that you want all Property types. When a Property type is found the application raises the MySearchDelegate and passes the filterCriteria instance. The delegate then simply makes a decision based on the member name and returns True or False indicating whether or not the search passed the custom test. (See Sidebar: Custom Attributes)

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