使用Type变量调用正确的泛型方法,使用out和ref

编程入门 行业动态 更新时间:2024-10-28 14:23:42
本文介绍了使用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

本文发布于:2023-11-10 10:24:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575097.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   正确   方法   Type   ref

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!