Sadi02’s Weblog

May 14, 2008

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

Filed under: Computer Science — Tags: , , — Md. Shaik Sadi @ 8:26 am

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..

4 Comments »

  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

    Comment by Sen K. Mathew — September 2, 2008 @ 4:50 am

  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…

    Comment by Md. Shaik Sadi — September 15, 2008 @ 11:57 am

  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

    Comment by KP — October 14, 2008 @ 9:40 am

  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.

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

    Comment by Md. Shaik Sadi — November 2, 2008 @ 5:00 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.