- Step 1: Add the reflection namspace
using System.Reflection;
Step 2: Load the assembly from the file
Assembly testAsm = Assembly.LoadFrom(@"C:\AddList.dll"); - Cheers
- Shyju Mohan
Step 3 :Create the type object
Type testType = testAsm.GetType("Blog.Sample.ListCreator");
Call the GetType function of the assembly class to create the Type.In above example there is a namspace called Blog.Sample and class called ListCreator.
Step 4:Create the instance of the Class
object testObj = Activator.CreateInstance(testType);
Use Activator.CreateInstance method to create the instance of the required class by passing the Type object which we created in the above step.
Step 5: Call the method
testType.InvokeMember("CreatList", BindingFlags.Default BindingFlags.InvokeMethod, null, testObj, new object[] { "BlogSample", "AssemblyBlog" });
Use the Type.InvokeMember to call the method in the class of loaded assembly.
Parameter Details:
1, Name of the Method (Ex: "CreateList")
2, Binding Flag(We are invoking a method so it will be BindingFlags.InvokeMethod)
3, Binding object - you can pass null
4, Target Object - which class's method need to be called (Object which is created by Activator.Creatinstance method - instance of the class)
5, Object Array of parameters for method (Above passed two string parameters)
No comments:
Post a Comment