CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    3

    late binding and overloaded methods

    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

    Code:
    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);
    
            }
        }
    }
    i build and classlibrary.dll was generated

    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

    Code:
    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);
                    }
                    }
                    
    
    
                }
    
            }
        }
    }
    the output is

    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

  2. #2
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: late binding and overloaded methods

    Your calling code has to know about the parameter types to use if you plan to reflect them.

    Your solution is a little ugly, but this link has the proper solution for calling overloaded methods through reflection:

    http://stackoverflow.com/questions/2...-method-in-net

  3. #3
    Join Date
    Mar 2010
    Posts
    3

    Re: late binding and overloaded methods

    thank you

  4. #4
    Join Date
    Mar 2010
    Posts
    3

    Re: late binding and overloaded methods

    but the only constraint is i have to use the concept of ParameterInfo in the program.. thats why i preferred the method i posted.. actually this is a kind of assignment

  5. #5
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: late binding and overloaded methods

    ah, well if you're being forced to do it that way since it's an assignment for class, then I suppose you have no other choice.

    As long as you know what's wrong and right

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured