Dynamic Method Invocation in C# .Net (Static Method and Non Static Method)

Create a solution in visual studio and add two project. First project name is  “FirstProject” and second one is “DynamicInvoke”. In the first project add Class1.cs class and compile.

Class1.cs

using System;
public class Class1{
public static String method1()
{
return "I am Static method (method1) in class1";
}
public String method2()
{
return "I am a Instance Method (method2) in Class1";
}
public String method3(String s)
{
return "Hello " + s;
}
}

DynaInvoke.cs

using System;
using System.Reflection;
class DynamicInvoke
{
public static void Main(String[] args)
{
Assembly MyAssembly = Assembly.Load("FirstProject");
//For getting the assembly click the right mouse on the class1 oject then go to the property
Type MyType = MyAssembly.GetType("FirstProject.Class1");
//class name should be added with assembly like (assemblyName.className) (NameSpace with classname)
object MyObj = Activator.CreateInstance(MyType);

//Invoking a static method (How to invoke a static method??)
String str = (String)MyType.InvokeMember("method1", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] { });
Console.WriteLine(str);

//Invoking a non-static method (How to invoke a non static method??)
str = (String)MyType.InvokeMember("method2", BindingFlags.Default | BindingFlags.InvokeMethod, null, MyObj, new object[] { });
Console.WriteLine(str);

//Invoking a non-static method with parameters (How to invoke a non static method with parametres??)
object[] MyParameter = new object[] { "Sadi" };
str = (String)MyType.InvokeMember("method3", BindingFlags.Default | BindingFlags.InvokeMethod, null, MyObj, MyParameter);
Console.WriteLine(str);
}
}

In the second project add this code and run..

Advertisements

13 thoughts on “Dynamic Method Invocation in C# .Net (Static Method and Non Static Method)

  1. Hi All

    When you post a tutorial, Please at least make sure that it works. If you search about dynamic classes in google you will get complaints after complaints that none is working. Why you people are posting programmes like this.
    Also when you post Post for all types(Console, web and windows)

    Thank you

  2. Hi Dear,
    when you read a tutorial plz at least make sure that you read it from the begining to the end. Why you people read tutorials from the middle?? or you should execute the code properly. you should not execute any portion of the code.

    you should understand the code at first then execute …
    all of my code is executed properly …

    i will try to make you easy…

  3. Hey Thanks for nice site,
    I have read some articles which are so nice and on the target..anyway i want your help how you show sample code in this format…pls guide me i also want to use it..Thanks

  4. i am very sorry ..
    i was very busy so i could not reply your comment ..
    it is too simple to show code like this format.
    You need to write your code in the following tag.

    [sourcecode=’csharp’] (your code)[\sourcecode]
    /author
    http:\\sadi02.wordpress.com

  5. Great post. One point needed for clarification though: members must be public in order to be Invoced this way. Though your solution had public method delcarations so it works perfectly on that occasion, it should be denoted that when the assembly called has private members, probably there should be a change on the BindingFlags operators to fix that.

    Also, in order to call from within the running assembly (same project) it is nice and neet to do simply this:

    this.GetType().InvokeMember(“MethodName”, BindingFlags.Default | BindingFlags.InvokeMethod, null, Activator.CreateInstance(this.GetType()), new object[] { });

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s