简体   繁体   English

C#类型GetMethod(),其中参数顺序是随机的

[英]C# Type GetMethod() where parameter order is random

I am trying to get a method of an object, and it works fine if the parameters of the method match the order of the list of parameters I provide. 我正在尝试获取一个对象的方法,并且如果该方法的参数与我提供的参数列表的顺序匹配,则可以正常工作。 I am trying to avoid this, so I do not have to worry about the order of parameter types in methods across different files. 我试图避免这种情况,所以我不必担心跨不同文件的方法中参数类型的顺序。 Here is what I have 这是我所拥有的

MethodInfo mi = stateType.GetMethod("Entrance", typesToUse.ToArray());

In my test case, typesToUse only contains two instances of unique interfaces, IClass1 and IClass2 in that order. 在我的测试案例中,typesToUse仅包含两个唯一接口实例,即IClass1和IClass2(按此顺序)。

If the Entrance method is : Entrance(IClass1 c1, IClass2 c2), it picks this method up. 如果Entrance方法为:Entrance(IClass1 c1,IClass2 c2),它将采用此方法。 Although, if its Entrance(IClass2 c2, IClass1 c1), it will not and mi will then be null. 虽然,如果它的Entrance(IClass2 c2,IClass1 c1)不会,则mi将为null。

Is there a way around this? 有没有解决的办法? Perhaps a way to tell GetMethod to ignore parameter order? 也许一种告诉GetMethod忽略参数顺序的方法?

Any help is appreciated and thank you. 任何帮助表示赞赏,谢谢。

It is not sensible to implement a method that will ignore parameter order. 实现忽略参数顺序的方法是不明智的。 Parameter order is critical to determining that you have found the correct method. 参数顺序对于确定找到正确的方法至关重要

Consider this simple class: 考虑这个简单的类:

public class A 
{
    public void Foo(int a, string b)
    {
        PrintAString();
    }

    public void Foo(string b, int a)
    {
        FormatHardDrive();
    }

} }

If you're method ignored the parameter order...bad things might happen! 如果您的方法忽略了参数顺序...可能会发生坏事!

Having said all that, it is possible of course. 说了这么多,当然是可能的。 Simply get all the methods with a given name, eliminate all those that do not contain parameters for all the types in typesToUse , and then ensure you only have one. 只需获取具有给定名称的所有方法,消除所有不包含typesToUse所有类型的参数的typesToUse ,然后确保只有一个。

The following code demonstrates this: 以下代码演示了这一点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        var typesToUse = new Type[] { typeof(int), typeof(string) };
        var methods = typeof(A).GetMethods().Where(m => m.Name == "Foo");

        var matchingMethods = methods.Where(m => ContainsAllParameters(m, typesToUse));

        Console.WriteLine(matchingMethods.Single());
    }

    private static bool ContainsAllParameters(MethodInfo method, Type[] typesToUse) 
    {
        var methodTypes = method.GetParameters().Select(p => p.ParameterType).ToList();

        foreach(var typeToUse in typesToUse)
        {
            if (methodTypes.Contains(typeToUse))
            {
                methodTypes.Remove(typeToUse);
            }
            else 
            {
                return false;       
            }
        }

        return !methodTypes.Any();
    }

}

public class A
{
    public void Foo(string a, int b) 
    {
        Console.WriteLine("Hello World");
    }
}

You could create an overload of the method, that takes the parameters in different order. 您可以创建方法的重载,该重载采用不同顺序的参数。

The recommended solution would probably just be to make sure they always are passed in the correct order. 推荐的解决方案可能只是确保始终以正确的顺序传递它们。

If you can't ensure this, you might solve the problem by creating an overload of the method, or by performing some checks as the first thing in the Entrance-method. 如果不能确保这一点,则可以通过创建方法的重载或通过执行某些检查作为入口方法中的第一件事来解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM