Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi All,
I have a problem. My problem is that I have a class with 3 overloaded functions. Example:
public class clsOverload
{
    public void OverloadedFunction(Int32 param1, String param2,     Int32 param3)
    {

    }
    public void OverloadedFunction(String param1, Decimal param2, String param3, String param4)
    {

    }
    public void OverloadedFunction(String param1, Int32 param2, String param3, Int32 param4, String param5)
    {

    }
}


Now I am accessing these function from another class. Now what I want to do is to count how many overloaded function are there?(In this case they are 3) and also I wanna get the position of the parameter Which is unique in all functions.(In this case Decimal is the parameter and its position is 2nd). I tried a lot but didn't get any solution. I hope you can help.

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 24-Dec-10 1:29am    
Do you have prior knowledge of the method name? Anyway, whatever you want, it's easy via Reflection.
astrovirgin 24-Dec-10 1:30am    
Yes, I know the method name. Can you please give me some idea?
Sergey Alexandrovich Kryukov 24-Dec-10 1:32am    
Do you have prior knowledge of the parameter name you need to find position? Or you need to know all the parameters? Then the ultimate structure which holds it is just a reflected MethodInfo...
astrovirgin 24-Dec-10 1:36am    
Yes I do. I know which parameter to find. actually I have no idea about Reflection.
astrovirgin 24-Dec-10 2:34am    
Thanks for your help SAKryukov. Its done by using Reflection...

It is indeed Reflection you need - you seem to have worked this out on your own, but here is the code (mostly just to move this from the unanswered list)

C#
namespace DirtyLittleConsoleApp
    {
    class Program
        {
        static void Main(string[] args)
            {
            MethodInfo[] methods = typeof(A).GetMethods();
            foreach (MethodInfo mi in methods)
                {
                Console.WriteLine(mi.Name);
                foreach (ParameterInfo pi in mi.GetParameters())
                    {
                    Console.WriteLine(pi.ParameterType.Name);
                    }
                }
            Console.ReadLine();
            }
        }
    public class A
        {
        public void M(string s) { }
        public void M(string s, int i) { }
        public void M(string s, int i, params int[] ints) { }
        }
    }
 
Share this answer
 
Comments
Dalek Dave 24-Dec-10 16:19pm    
Good Call Griff.
Manfred Rudolf Bihy 24-Dec-10 17:07pm    
See above! +5
Hi astrovirgin,

There are couple of delicate moments you need to know as well.
First, let me give you a bit more universal code I meant when I saw your question:

C#
using System;
using System.Reflection;
using MethodList =
    System.Collections.Generic.List<System.Reflection.MethodInfo>;
using OverloadingDictionary =
    System.Collections.Generic.Dictionary<
        string,
        System.Collections.Generic.List<System.Reflection.MethodInfo>>;

public class TypeReflector {

    public TypeReflector(Type type) { this.Type = type; }

    public OverloadingDictionary ClassifyMethodsByName(
           BindingFlags flags) {
        OverloadingDictionary dict = new OverloadingDictionary();
        MethodInfo[] methods = this.Type.GetMethods(flags);
        foreach (MethodInfo method in methods) {
            MethodList methodList;
            if (!dict.TryGetValue(method.Name, out methodList))
                methodList = new MethodList();
            methodList.Add(method);
        } //loop
        return dict;
    } //ClassifyMethodsByName

    //...

    Type Type;

} //class TypeReflector


In this code, if we call ClassifyMethodsByName, we can obtain all methods classified in a dictionary by the name key, and the value is a list of methods overloaded under this name. So, the number of overloaded method is simply a list's Count:

C#
public static int OverloadingCount(OverloadingDictionary dictionary, string name) {
    MethodList methodList;
    if (!dictionary.TryGetValue(name, out methodList))
        return 0;
    else
        return methodList.Count;
} //OverloadingCount


All the information on parameters (and a lot more) can be taken from MethodInfo (see Microsoft help -- very clear).

Now, one delicate moment is the parameter BindingFlags flags. Many developers was struggling about missing members (not
only methods, but any other) simply because Type.GetMethods() without BindingFlags parameter returns only subset of a methods (only public and non-static), not all methods. Same story with other kinds of members.

If you need all methods, you need the following:

C#
var dictionary = ClassifyMethodsByName(
    BindingFlags.Instance | BindingFlags.Static |
    BindingFlags.Public | BindingFlags.NonPublic);


If you don't use BindingFlags.Instance, only static members are returned; if you don't use BindingFlags.Static, only non-static members are returned; BindingFlags.NonPublic adds protected and private ones, etc. There are more different flags, but they are used in different Reflection methods.

Another delicate aspect is inherited members. The Reflection methods we are discussing never list inherited methods (nor any other inherited members).
(Nobody remembered about those, huh?)
To obtain inherited members, one need to collect them recursively.
First, we need a base type:

Type baseType = this.Type.BaseType;


Now, baseType should call recursively call some method collecting members. The recursion must end when Type.BaseType returns null.

As I understand, in your problem inherited methods should be counted exactly as any others, because you say you will use this data to call methods (Invoke). So, you will need to take care about those.

Thank you. Best wishes in New Year!
 
Share this answer
 
v2
Comments
Dalek Dave 24-Dec-10 16:19pm    
Great Answer.
Manfred Rudolf Bihy 24-Dec-10 17:08pm    
Very detailed! 5+
Sandeep Mewara 25-Dec-10 0:28am    
Well, I came to this answer to notify you about my reply here: http://www.codeproject.com/Answers/139584/Help-solving-Knapsack-Algorithm.aspx?cmt=35901#answer1

But couldn't leave without a 5! to this answer. Great answer.
Espen Harlinn 26-Feb-11 11:21am    
Amazing good effort, my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900