Logo
programming4us
programming4us
programming4us
programming4us
Windows XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
 
programming4us
Windows 7

Visual Basic 2010 : Reflection - Invoking Code Dynamically

6/20/2011 2:52:34 PM
Reflection also enables executing dynamic code, meaning that you can pick up types defined within an assembly, creating instances and invoking types from Visual Basic code without having a reference to that assembly. For example, imagine you want to load the People.dll assembly and create and populate an instance of the Person class, as shown in Listing 1.
Listing 1. Creating and Running Dynamic Code
Imports System.Reflection
Module DynamicCode

Sub DynCode()
Dim asm = Assembly.LoadFrom("People.dll")

'Gets the type definition
Dim personType = asm.GetType("People.Person")

'Gets the LastName property definition
Dim lastNameProperty As PropertyInfo = personType.
GetProperty("LastName")
'Gets a reference to the property setter
Dim lastNamePropSet As MethodInfo = lastNameProperty.
GetSetMethod

Dim firstNameProperty As PropertyInfo = personType.
GetProperty("FirstName")
Dim firstNamePropSet As MethodInfo = firstNameProperty.
GetSetMethod

Dim ageProperty As PropertyInfo = personType.GetProperty("Age")
Dim agePropSet As MethodInfo = ageProperty.GetSetMethod

'Creates an instance of the Person class
Dim newPerson As Object = _
Activator.CreateInstance(personType)

'Each method is invoked upon the new type instance
lastNamePropSet.Invoke(newPerson, New Object() {"Del Sole"})
firstNamePropSet.Invoke(newPerson, New Object() {"Alessandro"})
agePropSet.Invoke(newPerson, New Object() {32})

'Gets the BuildFullName method from the Person class
Dim buildFullNameMethod = personType.GetMethod("BuildFullName")

'The method returns String but Invoke returns Object, so
'a conversion is required
Dim result As String = CStr(buildFullNameMethod.
Invoke(newPerson, Nothing))

Console.WriteLine(result)
Console.ReadLine()
End Sub
End Module


When you have the type instance, you invoke the GetProperty method to get a reference of the desired property. This returns a PropertyInfo object. To set the property value, you need a reference to the setter method that is obtained via the GetSetMethod and that returns a MethodInfo object. (If you also want the ability to get a property value, you need to invoke instead GetGetMethod the same way.) When you have all properties, you need an instance of the class. This can be obtained by calling the Activator.CreateInstance method, which takes the type instance as the argument. The System.Activator class contains members for creating code locally or retrieving code from a remote location. Having an instance of the class is required before you set properties, because it is against the instance that property setters will be invoked. To actually run the property setter, you call the MethodInfo.Invoke instance method; the first argument is the type instance, whereas the second argument is an array of items of type Object, each to be used as a property value. In our case each property in the Person class accepts just one value, so each array can store just one item. Similarly you can get reference to methods invoking GetMethod on the type instance, as it happens in Listing 1, to get a reference to the Person.BuildFullName method. When you call Invoke to run the method, you can pass Nothing as the second argument if the original method does not require parameters. The code simply produces the following result:

Del Sole Alessandro, Male of Age: 32

After seeing how you can call dynamic code provided by an existing assembly, let’s now see how to create code at runtime.

Security Note

In many cases you notice that you can also invoke members marked as private or with limited visibility. Although this can seem exciting, take care. If you invoke a private member but you are not completely sure about its purpose, you expose your code to potential uncontrollable dangers.

Other -----------------
- Visual Basic 2010 : Reflection - Reflecting Types
- Administering Internet Explorer : Troubleshooting Internet Explorer Issues
- Administering Internet Explorer : Understanding Advanced Settings (part 2) - Branding Internet Explorer & Group Policy Settings
- Administering Internet Explorer : Understanding Advanced Settings (part 1) - Certificate Settings
- Administering Internet Explorer : Managing Windows Internet Explorer Settings (part 2)
- Administering Internet Explorer : Managing Windows Internet Explorer Settings (part 1) - Managing Cache
- Visual Basic 2010 : Reflection - Understanding Assemblies Metadata & Getting Assembly Information
- Visual Basic 2010 : Hosting WCF Services in Internet Information Services & Configuring Services with the Configuration Editor
- Supporting Mobile Windows 7 Users : Understanding DirectAccess & Using BranchCache
- Microsoft Excel 2010 - Creating and Modifying Charts - Changing Chart Titles
 
 
Video tutorials
- How To Install Windows 8 On VMware Workstation 9

- How To Install Windows 8

- How To Install Windows Server 2012

- How To Disable Windows 8 Metro UI

- How To Change Account Picture In Windows 8

- How To Unlock Administrator Account in Windows 8

- How To Restart, Log Off And Shutdown Windows 8

- How To Login To Skype Using A Microsoft Account

- How To Enable Aero Glass Effect In Windows 8

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
programming4us programming4us
 
programming4us
Girls
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone