BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


.NET and XML
Using XML classes in .Net
Pass node sets to XSLT stylesheets using .NET
Output a directory as XML using .NET
.Net, COM Interoperability and Word Object Model
WS-Routing for .NET endpoints
Using XML and XSLT in VB.NET Jeopardy
Symmetric Encryption/Decryption with .NET
XInclude.NET 1.0
EXSLT.NET 1.0
How to read a XML file using XmlTextReader?
How to compare a XML element with XmlTextReader?
How to verify if an element is empty with XmlTextReader and IsEmptyElement?
How to check if the XML elements contains any attributes with XmlTextReader
How to avoid to read the XML declaration with XmlTextReader?
How to create a XML file with XmlTextWriter?
Is it possible to add attributes with XmlTextWriter?
How to Write a Comment to a XML File with XmlTextWriter and WriteComment?
How to add the XML Declaration with XmlTextWriter and WriteStartDocument?
How to use Indentation in XmlTextWriter?
What is the usage of WriteFullEndElement and how do I use?


 
 

<< XSLT 


By Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First Posted 03/17/2002
Times viewed 3463

Dynamic Method Invocation in .NET using Reflection API


This post contains attachments
v20020317185530.zip 

Summary This article explains how to create a class and how to dynamically invoke its methods using System.Reflection Namespace.

 Reflection provides objects that encapsulate assemblies, modules, and types. Reflection can be used to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can invoke the type’s methods or access its fields and properties.

Reflection can be used to create applications called type browsers, which enable users to select types and then view the information about those types.

It can at times even be used to emit Intermediate Language Code on the fly so that the generated code can be executed directly.

The Reflection API uses the System.Reflection namespace, with the Type class to identify the Type of the class being reflected, fields of a struct or enum represented by the FieldInfo class, members of the reflected class represented by the MemberInfo class, methods of a reflected class represented by the MethodInfo class, and parameters to methods represented by the ParameterInfo class.

The Activator class's CreateInstance() method is used to dynamically invoke instances on the fly. Dynamic Invocation is very useful in providing a very late-binding architecture, where one component's runtime can be integrated with other component runtimes.

 To explain the concept of dynamic method invocation, let me explain first by creating a class Client as follows:

 Using C#:

 

using System;

class Client

{

 

              private static String sFirstName = Pandu;

              private static String sMiddleName = ;

              private static String sLastName = Rao;

              private String sAddress1;

              private String sAddress2;

              private String sCity;

              private String sState;

              private String sZip5;

              private String sZip4;

             

              public static String GetName()

    {

                            return(sFirstName + sMiddleName + sLastName);

              }

             

              public String GetAddress()

              {

                            sAddress1 = One New York Plaza;

                            sAddress2 = Water Street;

                            sCity = New York City;

                            sState = New York;

                            sZip5 = 10004;

                            sZip4 = 1234;

                            string sRetAddress = sAddress1 + , +  sAddress2 + , + sCity + ,  + sState + , + sZip5 + , + sZip4;

                            return sRetAddress;

              }

              public String Greetings(String sName)

    {

                            return(Hello + sName + . Welcome to the world to .NET);

              }

}

 

Save the above code with filename Client.cs

Compile it as follows:

              Csc /t:library Client.cs

This command will build a library called Client.dll

Refer Client.vb for VB.NET Code.

Compile it as follows:

              C:\pandu\asp\reflection>vbc /t:library Client.vb

              This command will build a library called Client.dll

 Here I am giving the example with both C# .NET and VB.NET. While running this class, you need to select one language to run the application.

 Now let us create a console-based application, which invokes the methods of the Client class dynamically by using System.Reflection namespace.

In the example given below,

              Assembly oAssemby = Assembly.Load(path);

              Loads an assembly given its AssemblyName “Client”

 Type oType = oAssemby.GetType(Client);

              Gets the Type object that represents the specified type “Client”

 String sOutput = (String) oType.InvokeMember (GetName,BindingFlags.Default | BindingFlags.InvokeMethod,null,null,new Object [] {});

Invokes the specified member using specified binding constraints and matching the specified matching constraints. Here “GetName” is the method name of type string, Binding.Flags.Default | BindingFlags.InvokeMethod is of type BindingFlags, Binder is null, object to instantiate is null and new Object[] {} for argument list.

Refer Code in ClientInvoke.cs

Compile this code using the following command:

              C:\pandu\asp\reflection>Csc ClientInvoke.cs

Refer Code of ClientInvoke.vb

 Compile this code using the following command:

              C:\pandu\asp\reflection>vbc ClientInvoke.vb

 From the above code, you will know the differences in writing VB.NET and C#.NET code.        

 When you compile the above code, ClientInvoke.exe is created.

Then call the application by using the following command at command prompt:

              C:\pandu\asp\reflection>ClientInvoke

 

System Requirements: .NET SDK

 

Additional information


Rate this article on a scale of 1 to 10 (1 votes, average 5)

Your vote :  

<< XSLT 





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML