简体   繁体   English

模糊输入系统.反射

[英]Ambiguous Entry System.Reflection

Im getting a error when im parsing a method trough system.reflection:通过 system.reflection 解析方法时出现错误:

System.Reflection.AmbiguousMatchException System.Reflection.AmbiguousMatchException

typeof(Graphics).GetMethod("DrawRectangle").Invoke(g, new object[] {
     Pens.Red, new Rectangle(200, 200, 100, 50)
});

however it works nice when im parsing it trough the compiler但是当我通过编译器解析它时它工作得很好

g.DrawRectangle(Pens.Red, new Rectangle(200,200,100,50));

is there a way to specify which method i want to call?有没有办法指定我要调用的方法?

Graphics.DrawRectangle has 3 different overloads, so it is impossible to find concrete one via GetMethod just by name, you can specify parameter types for desired overload in GetMethod call: Graphics.DrawRectangle有 3 种不同的重载,因此仅通过名称无法通过GetMethod找到具体的重载,您可以在GetMethod调用中指定所需重载的参数类型:

typeof(Graphics).GetMethod("DrawRectangle", new[] {typeof(Pen), typeof(Rectangle)})

There are different overload for DrawRectangle. DrawRectangle 有不同的重载。

Try something like this.尝试这样的事情。

var args = new object[] { Pens.Red, new Rectangle(200, 200, 100, 50) };
 var r = 
 typeof(Graphics).GetMethod("DrawRectangle",System.Type.GetTypeArray(args));
 r.Invoke(g, args);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM