避免歧义匹配异常

编程入门 行业动态 更新时间:2024-10-24 12:32:23
本文介绍了避免歧义匹配异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通过反射对类型调用静态方法 Parse ,因为我在编译时不知道对象的类型(但是我知道,它具有 Parse 方法,使用字符串)。

I am invoking a static method Parse on a type via reflection because I do not know the type of the object at compile-time (I do know, however, it has a Parse method, taking a string).

但是,我收到了一个模棱两可的匹配异常,大概是因为有很多重载的 Parse 每个方法都带有一个对象(字符串,int,double等)。

However, I am getting an ambiguous match exception, presumably because there are a lot of overloaded Parse methods each taking a single object (string, int, double etc.).

如何在方法调用中更具体地说明以确保达到正确的方法( Parse(string s)),并且不会引发异常。

How can I be more specific in my method invocation to ensure I reach the correct method (Parse(string s)) and the exception is not thrown.

我的代码如下:

Type returnType = p.PropertyType; object value = returnType.GetMethod("Parse").Invoke(null, new string[] { "1" });

推荐答案

使用此重载并使用

returnType.GetMethod("Parse", new [] {typeof(string)})

更多推荐

避免歧义匹配异常

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

发布评论

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

>www.elefans.com

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