在静态重载的方法使用反射out参数 [英] Reflection on a static overloaded method using an out parameter

查看:189
本文介绍了在静态重载的方法使用反射out参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与通过反射与out参数调用一个重载的静态方法的一些问题,并希望得到一些指点。

I'm having some issues with invoking an overloaded static method with an out parameter via reflection and would appreciate some pointers.

我在寻找动态创建喜欢的类型 System.Int32 System.Decimal ,然后调用静态的TryParse(字符串,出x)在其

I'm looking to dynamically create a type like System.Int32 or System.Decimal, and then invoke the static TryParse(string, out x) method on it.

下面的代码有两个问题:

The below code has two issues:


  • t.GetMethod(的TryParse,新类型[] {typeof运算(字符串),T})未能归还MethodInfo的我希望

  • t.GetMethod("TryParse", new Type[] { typeof(string), t } ) fails to return the MethodInfo I expect

mi.Invoke(空,新的对象[] {value.ToString(),concreteInstance})看似成功,但出参数 concreteInstance 不设置为解析值

mi.Invoke(null, new object[] { value.ToString(), concreteInstance }) appears to succeed but doesn't set the out param concreteInstance to the parsed value

交织成这个功能,你可以看到一些临时代码演示应该发生什么,如果在键入参数设置为 System.Decimal

Interwoven into this function you can see some temporary code demonstrating what should happen if the type parameter was set to System.Decimal.

public static object Cast(object value, string type)
{
    Type t = Type.GetType(type);
    if (t != null)
    {
        object concreteInstance = Activator.CreateInstance(t);
        decimal tempInstance = 0;

        List<MethodInfo> l = new List<MethodInfo>(t.GetMethods(BindingFlags.Static | BindingFlags.Public));

        MethodInfo mi;
        mi = t.GetMethod("TryParse", new Type[] { typeof(string), t } );  //this FAILS to get the method, returns null
        mi = l.FirstOrDefault(x => x.Name == "TryParse" && x.GetParameters().Length == 2);  //ugly hack required because the previous line failed
        if (mi != null)
        {
            try
            {
                bool retVal = decimal.TryParse(value.ToString(), out tempInstance);
                Console.WriteLine(retVal.ToString());       //retVal is true, tempInstance is correctly set
                object z = mi.Invoke(null, new object[] { value.ToString(), concreteInstance });
                Console.WriteLine(z.ToString());            //z is true, but concreteInstance is NOT set
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        return concreteInstance;
    }

    return value;
}



什么我需要做什么来确保我的 t.GetMethod()调用返回正确的MethodInfo?什么我需要做的有 concreteInstance 在正确设置我的 mi.Invoke()打电话?

What do I need to do to ensure that my t.GetMethod() call returns the correct MethodInfo? What do I need to do to have concreteInstance correctly set in my mi.Invoke() call?

我知道有一堆关于这个主题的问题,但其中大部分都涉及静态泛型方法,或者是不超载的静态方法。 这个问题是类似的,但不是重复

I know there are a bunch of questions on this topic, but most of them involve static generic methods or static methods that are not overloaded. This question is similar but not a duplicate.

推荐答案

您需要使用正确的的BindingFlags ,并使用 Type.MakeByRefType 退出 REF 参数。一秒钟,我就对你的代码示例。

You need to use the right BindingFlags and use Type.MakeByRefType for out and ref parameters. One second, and I'll have a code sample for you.

例如,

MethodInfo methodInfo = typeof(int).GetMethod(
    "TryParse",
    BindingFlags.Public | BindingFlags.Static,
    Type.DefaultBinder,
    new[] { typeof(string), typeof(int).MakeByRefType() },
    null
);



我要指出的是调用,这是一个有点棘手了。这里是你如何做到这一点。

I should point out that invoking this is a little tricky too. Here's how you do it.

string s = "123";
var inputParameters = new object[] { "123", null };
methodInfo.Invoke(null, inputParameters);
Console.WriteLine((int)inputParameters[1]);



第一个是因为我们正在调用一个静态方法(有没有对象接受这个调用)。在 inputParameters 将由的TryParse 通过解析的结果(它的退出参数)。

The first null is because we are invoking a static method (there is no object "receiving" this invocation). The null in inputParameters will be "filled" for us by TryParse with the result of the parse (it's the out parameter).

这篇关于在静态重载的方法使用反射out参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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