i have a small doubt in the concept of late binding..
how to invoke the overloaded methods in the class library.
here the details
the code for class library will be
i build and classlibrary.dll was generatedCode:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace classlibrary { public class summation { public static void sum() { int a = 2; int b = 3; int i; i = a + b; Console.WriteLine("sum() is " + i); } public static void sum(int a) { int b = 3; int i; i = a + b; Console.WriteLine("sum(int a) is " + i); } public static void sum(int a,int b) { int i; i = a + b; Console.WriteLine("sum(int a,int b) is " + i); } public static void sum(int a,int b,int c) { int i; i = a + b + c; Console.WriteLine("sum(int a,int b,int c) is " + i); } } }
now in the class that refer classlibrary.dll
the code
MethodInfo mi=t.GetMethod("sum");
gives Ambiguous error because there are more than one method with the same sum.
how to invoke all the "sum" methods using the concept of late binding..
i tried to found the solution. I wrote a code. of course,it works
but i dont know whether this approach is right or wrong.. so go through the code and inform me
the output isCode:using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.IO; namespace mustdo { class mustdo_class { public static void Main() { Assembly a = null; try { a = Assembly.Load("classlibrary"); } catch (FileNotFoundException e) { Console.WriteLine(e.Message); } Type t = a.GetType("classlibrary.summation"); object o=Activator.CreateInstance(t); MethodInfo[] mi = t.GetMethods(); foreach (MethodInfo m in mi) { //Console.WriteLine(m.Name); if(m.Name=="sum") { Console.WriteLine("method name:" + m.Name); object[] p = null; ParameterInfo[] pi = m.GetParameters(); if ((pi.Length == 0)) { m.Invoke(o, null); } else if ((pi.Length == 1)) { p=new object[1]; p[0]=2; m.Invoke(0,p); } else if ((pi.Length == 2)) { p = new object[2]; p[0] = 2; p[1] = 3; m.Invoke(0, p); } else if ((pi.Length == 3)) { p = new object[3]; p[0] = 2; p[1] = 3; p[2] = 4; m.Invoke(0, p); } } } } } }
method name:sum
sum() is 5
method name:sum
sum(int a) is 5
method name:sum
sum(int a, int b) is 5
method name:sum
sum(int a,int b,int c) is 9
is there any other way to achieve this..
thanks in Advance




Reply With Quote