- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了这段代码:
MethodInfo method2 = typeof(IntPtr).GetMethod(
"op_Explicit",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
null,
new Type[]{
typeof(IntPtr),
},
null
);
如果我尝试运行我得到一个不明确的匹配异常,我该如何解决这个问题?谢谢
我想获取的方法是op_Explicit(intptr)返回值int32
最佳答案
没有用于在不同类型的方法之间进行选择的标准重载。你必须自己找到方法。您可以编写自己的扩展方法,如下所示:
public static class TypeExtensions {
public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingAttr, Type[] types, Type returnType ) {
var methods = type
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(mi => mi.Name == "op_Explicit")
.Where(mi => mi.ReturnType == typeof(int));
if (!methods.Any())
return null;
if (methods.Count() > 1)
throw new System.Reflection.AmbiguousMatchException();
return methods.First();
}
public static MethodInfo GetExplicitCastToMethod(this Type type, Type returnType )
{
return type.GetMethod("op_Explicit", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, new Type[] { type }, returnType);
}
}
然后使用它:
MethodInfo m = typeof(IntPtr).GetExplicitCastToMethod(typeof(int));
准确的说,在IntPtr类中定义了两个类型转换:
public static explicit operator IntPtr(long value)
public static explicit operator long(IntPtr value)
并且在 System.Int64 类中没有定义转换(long 是 Int64 的别名)。
您可以为此目的使用Convert.ChangeType
关于c# - 抛出 AmbiguousMatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17567666/
我写了这段代码: MethodInfo method2 = typeof(IntPtr).GetMethod( "op_Explicit", Bind
using System; using System.Reflection; namespace A { interface IObjectWithId { TId Id { get;
我得到异常(exception): AmbiguousMatchException: ambiguous match found 当打开我的窗口时,XAML被解析。我有一个基本的ViewModel类。
我有以下(简化的)类(class): public abstract class BaseSite { public int SiteId { get; set; } public s
我正在尝试在我的代码重载签名中测试(使用 Moq)重载 protected 泛型方法: protected void AutoMap(IList sources
我正在寻找一种解决方案来访问类的“展平”(最低)属性值及其通过属性名称的反射派生的值。 即从 ClassB 或 ClassC 类型访问 Property1 或 Property2 : publi
我有两个名称相同但方法签名不同的 Controller 操作。它们看起来像这样: // // GET: /Stationery/5?asHtml=true [AcceptVer
我正在使用反射创建一个 lambda 函数。它适用于我尝试过的大多数项目,但是在其中一个属性上它一直抛出不明确的匹配异常。 代码如下所示。当它命中 Expression.PropertyOrField
我有两个名称相同但大小写不同的属性 Title 和 TITLE: public class Product { [Key] public Guid Id { get; set; }
昨天我在开发 Web 部件时遇到了一个问题(这个问题不是关于 webpart 而是关于 C#)。关于这个问题的背景很少。我有一个使用反射加载 WebPart 的代码,其中我得到了 AmbiguousM
当我使用 JSON.NET 在 LINQPad 中运行此代码时: var x = JObject.Parse( @"{ ""data"" : [ { ""id"" : ""bbab529e
我正在尝试获取 MethodInfo来自方法 TableExists所以我可以用一个类型来调用它。 该方法在 OrmLiteSchemaApi 中声明类(class)。有2个重载: public st
我正在开发一个有点简单的 InventoryTracker MVC5 应用程序,其中我在将 LocalDatabase 设置为 Seed() 时遇到了一些问题. 当我运行 update-databas
我是一名优秀的程序员,十分优秀!