计数重载函数和参数位置 [英] Count Overloaded Functions and the position of parameter

查看:79
本文介绍了计数重载函数和参数位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有个问题.我的问题是我有一个带有3个重载函数的类.示例:

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)
    {

    }
}



现在,我正在从另一个类访问这些函数.现在我要做的是计算有多少个重载函数?(在这种情况下,它们是3),我也想获得所有函数中唯一的参数的位置.(在这种情况下,十进制是参数它的位置是2nd).我尝试了很多,但没有得到任何解决方案.希望您能提供帮助.

谢谢



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

推荐答案

这确实是您需要的反射-您似乎已经自己解决了此问题,但这是代码(主要是将其从未解答的内容中移除)列表)

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)

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) { }
        }
    }


您好,astrovirgin,

您还需要了解几个微妙的时刻.
首先,让我给您一些通用的代码,当我看到您的问题时,这就是我的意思:

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:

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



在此代码中,如果调用ClassifyMethodsByName,则可以获取通过名称键分类在字典中的所有方法,并且该值是在此名称下重载的方法的列表.因此,重载方法的数量只是一个列表的Count:



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:

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



有关参数(以及更多内容)的所有信息都可以从MethodInfo获取(请参阅Microsoft帮助-非常清楚).

现在,一个微妙的时刻是参数BindingFlags flags.许多开发人员都在为缺少成员而苦苦挣扎(不是
仅包含方法,但仅包含其他方法),这是因为不带BindingFlags参数的Type.GetMethods()仅返回方法的子集(仅公共和非静态),而不是所有方法.与其他成员的故事相同.

如果需要所有方法,则需要以下内容:



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:

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



如果不使用BindingFlags.Instance,则仅返回静态成员;否则,返回false.如果您不使用BindingFlags.Static,则仅返回非静态成员;否则,返回false. 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;



现在,baseType应该以递归方式调用一些方法收集成员.递归必须在Type.BaseType返回null时结束.
据我了解,在您的问题中,继承方法应与其他方法一样进行计数,因为您说您将使用此数据来调用方法(调用).因此,您需要注意这些.

谢谢你.新年祝福!



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!


这篇关于计数重载函数和参数位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆