Here we will see how to invoke a member method by using reflection.
1. Create a class library with the name MathLib;
2. Add the following code to it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MathLib
{
public class Maths
{
public void Add(int a, int b)
{
int c = a + b;
Console.WriteLine("Result From Calculator after Add:" + c);
}
private void Sub(int a, int b)
{
int c = a - b;
Console.WriteLine("Result From Calculator after Add:" + c);
}
}
}
3. Compile the class library; the result will be a MathLib.dll file in "projectfolder\bin\release" folder
4. Now we want to invoke the library's methods from another application without adding the reference before compilation, instead accessing an assembly at run time and even without knowing it's name or any other details, call a method from that library - this sounds really dynamic.
To do so create a console application and use the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; //for reflection
using System.Globalization; //for culture-optional
namespace SimpleInvokeDemo
{
class Program
{

Nitin AnandPosted Jan 12, 2016, 6:39 AM
IS there any way to load assembly without knowing the actual path but just the file name like in this case MathLib.dll
Nitin AnandPosted Jan 12, 2016, 6:37 AM
good one. it works for me as Assembly.LoadFrom(path);