本文介绍了使用Type变量调用正确的泛型方法,使用out和ref的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 静态类示例
{
public static string方法< T>(ref List< string> p2,out string p3,string p4)
{
...
}
public static string方法< T>(ref List< string> p2,out string p3,int p4)
{
.. 。
}
}

以下显然不起作用,想法:
$ b $ pre $ public static string MethodCaller(Type theType,ref List< string> p2,out string p3,string p4)
{
方法< theType>(ref p2,out p3,p4);
}

使用GetMethod?它如何知道使用哪两种重载方法?
应该使用Expression.Call来代替吗?我们如何处理ref和out参数?

请帮忙:)

解决方案

这可以通过反射来完成,尽管找到正确的重载是有点麻烦的:

  class Program 
{
static void Main(string [] args)
{
List< string> p2 =新列表< string>();
字符串p3;
string p4 =输入字符串;
string result = MethodCaller(typeof(DateTime),ref p2,out p3,p4);

$ b $公共静态字符串MethodCaller(类型theType,ref List< string> p2,out string p3,string p4)
{
MethodInfo method =(from m in typeof(Example).GetMethods()
let p = m.GetParameters()
其中m.Name ==Method
&& p.Length == 3
&& p [0] .ParameterType.IsByRef
&& p [0] .ParameterType.HasElementType
&& p [0] .ParameterType.GetElementType()== typeof(List< string>)
&& p [1] .ParameterType.IsByRef
&& p [1] .ParameterType.HasElementType
&& p [1 ] .ParameterType.GetElementType()== typeof(string)
&& p [2] .ParameterType == typeof(string)
选择m).Single();
MethodInfo genericMethod = method.MakeGenericMethod(theType);
object [] parameters = new object [] {null,null,p4};
string returnValue =(string)genericMethod.Invoke(null,parameters);
p2 =(List< string>)参数[0];
p3 =(字符串)参数[1];
返回returnValue;


$ b静态类示例
{
public static string方法< T>(ref List< string> p2,out string p3,string p4)
{
p2 = new List< string>();
p2.Add(typeof(T).FullName);
p2.Add(p4);
p3 =输出字符串;
返回返回值;

$ b $ public static string Method< T>(ref List< string> p2,out string p3,int p4)
{
p2 = new List< string> ;();
p2.Add(typeof(T).FullName);
p2.Add(p4.ToString());
p3 =输出字符串;
返回返回值;
}
}


static class Example
{
    public static string Method<T>(ref List<string> p2, out string p3, string p4)
    {
      ...
    }
    public static string Method<T>(ref List<string> p2, out string p3, int p4)
    {
      ...
    }
}

The following obviously doesn't work, but that's the idea:

public static string MethodCaller(Type theType, ref List<string> p2, out string p3, string p4)
{
    Method<theType>(ref p2, out p3, p4);
}

Using GetMethod? how does it know which of the two overloaded methods to use?should we Use Expression.Call instead? how do we deal with the ref and out parameters?

Please help :)

解决方案

This can be done through reflection, although finding the correct overload is a bit messy:

class Program
{
    static void Main(string[] args)
    {
        List<string> p2 = new List<string>();
        string p3;
        string p4 = "input string";
        string result = MethodCaller(typeof(DateTime), ref p2, out p3, p4);
    }

    public static string MethodCaller(Type theType, ref List<string> p2, out string p3, string p4)
    {
        MethodInfo method = (from m in typeof(Example).GetMethods()
                             let p = m.GetParameters()
                             where m.Name == "Method"
                             && p.Length == 3
                             && p[0].ParameterType.IsByRef
                             && p[0].ParameterType.HasElementType
                             && p[0].ParameterType.GetElementType() == typeof(List<string>)
                             && p[1].ParameterType.IsByRef
                             && p[1].ParameterType.HasElementType
                             && p[1].ParameterType.GetElementType() == typeof(string)
                             && p[2].ParameterType == typeof(string)
                             select m).Single();
        MethodInfo genericMethod = method.MakeGenericMethod(theType);
        object[] parameters = new object[] { null, null, p4 };
        string returnValue = (string)genericMethod.Invoke(null, parameters);
        p2 = (List<string>)parameters[0];
        p3 = (string)parameters[1];
        return returnValue;
    }
}

static class Example
{
    public static string Method<T>(ref List<string> p2, out string p3, string p4)
    {
        p2 = new List<string>();
        p2.Add(typeof(T).FullName);
        p2.Add(p4);
        p3 = "output string";
        return "return value";
    }

    public static string Method<T>(ref List<string> p2, out string p3, int p4)
    {
        p2 = new List<string>();
        p2.Add(typeof(T).FullName);
        p2.Add(p4.ToString());
        p3 = "output string";
        return "return value";
    }
}

这篇关于使用Type变量调用正确的泛型方法,使用out和ref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 17:54