C# GetProperty 反射导致新属性上的“找到不明确的匹配”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11443707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GetProperty reflection results in "Ambiguous match found" on new property
提问by Valamas
How can I get my property? Currently an error is occuring of Ambiguous match found, see the comment line in code.
我怎样才能得到我的财产?当前正在发生错误Ambiguous match found,请参阅代码中的注释行。
public class MyBaseEntity
{
public MyBaseEntity MyEntity { get; set; }
}
public class MyDerivedEntity : MyBaseEntity
{
public new MyDerivedEntity MyEntity { get; set; }
}
private static void Main(string[] args)
{
MyDerivedEntity myDE = new MyDerivedEntity();
PropertyInfo propInfoSrcObj = myDE.GetType().GetProperty("MyEntity");
//-- ERROR: Ambiguous match found
}
采纳答案by Kevin Aenmey
Situations in which AmbiguousMatchException occurs ...
...derived type declares a property that hides an inherited property with the same name, by using the new modifier
发生 AmbiguousMatchException 的情况...
...派生类型声明了一个属性,该属性隐藏了具有相同名称的继承属性,通过使用 new 修饰符
If you run the following
如果你运行以下
var properties = myDE.GetType().GetProperties().Where(p => p.Name == "MyEntity");
you will see that two PropertyInfoobjects are returned. One for MyBaseEntityand one for MyDerivedEntity. That is why you are receiving the Ambiguous match founderror.
您将看到PropertyInfo返回了两个对象。一为MyBaseEntity一为MyDerivedEntity。这就是您收到Ambiguous match found错误的原因。
You can get the PropertyInfofor MyDerivedEntitylike this:
你可以得到这样的PropertyInfofor MyDerivedEntity:
PropertyInfo propInfoSrcObj = myDE.GetType().GetProperties().Single(p =>
p.Name == "MyEntity" && p.PropertyType == typeof(MyDerivedEntity));
回答by Lorenz Lo Sauer
Kevin already pointed out the issue, but you don't need complex statements, or LINQ for that:
凯文已经指出了这个问题,但你不需要复杂的语句,或者 LINQ:
PropertyInfo propInfoSrcObj = myDE.GetType().
GetProperty("MyEntity", typeof(MyDerivedEntity));
回答by AlphaOmega
For property:
对于财产:
MemberInfo property = myDE.GetProperty(
"MyEntity",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
For method:
对于方法:
MemberInfo method = typeof(String).GetMethod(
"ToString",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly,
null,
new Type[] { },// Method ToString() without parameters
null);
BindingFlags.DeclaredOnly - Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered.
BindingFlags.DeclaredOnly - 指定只应考虑在提供的类型层次结构级别声明的成员。不考虑继承成员。
回答by odyth
I was having this issue with MsgPack serialization of my LocationKey object. Ended up being the operators I had defined in my LocationKey class. Having both of these operators defined caused DefaultContext.GetSerializer(obj.GetType());to throw Ambiguous Match Found when trying to serialize. Removing one set of operators made the issue go away.
我的 LocationKey 对象的 MsgPack 序列化遇到了这个问题。最终成为我在 LocationKey 类中定义的运算符。定义这两个运算符会导致DefaultContext.GetSerializer(obj.GetType());在尝试序列化时抛出 Ambiguous Match Found。删除一组运算符使问题消失。
public static bool operator ==(int key1, LocationKey key2)
{
return key1 == key2.Value;
}
public static bool operator !=(int key1, LocationKey key2)
{
return key1 != key2.Value;
}
public static bool operator ==(LocationKey key1, int key2)
{
return key1.Value == key2;
}
public static bool operator !=(LocationKey key1, int key2)
{
return key1.Value != key2;
}
回答by Chad Kuehn
The ambiguity occurs because of the newdeclaration in MyDerivedEntity. To overcome this you can use LINQ:
由于 中的new声明而产生歧义MyDerivedEntity。为了克服这个问题,您可以使用 LINQ:
var type = myObject.GetType();
var colName = "MyEntity";
var all = type.GetProperties().Where(x => x.Name == colName);
var info = all.FirstOrDefault(x => x.DeclaringType == type) ?? all.First();
This will grab the property out of the derived type if it exists, otherwise the base. This can easily be flip-flopped if needed.
如果存在,这将从派生类型中获取属性,否则为基类。如果需要,这可以很容易地翻转。
回答by Saurabh Solanki
I got this error in browser console I search for it and I found this exception is for c# and answer is also for c# then I try to look at my code and I found the where the problem occurs :
我在浏览器控制台中收到此错误我搜索它,发现此异常适用于 c#,答案也适用于 c#,然后我尝试查看我的代码,发现问题发生的位置:
I have an ajax post method and when I post data got this error so the data I have passed will be collected by c# web method, so when I see that model I have 2 properties with the same name so I remove one and problem and exception got solved.
我有一个 ajax post 方法,当我发布数据时出现此错误,因此我传递的数据将由 c# web 方法收集,因此当我看到该模型时,我有 2 个同名的属性,因此我删除了一个,问题和异常解决了。
回答by DavidScherer
For me, in VB.Net, I encountered this super descriptive error when passing a JS object to a <WebMethod()>function.
对我来说,在 VB.Net 中,我在将 JS 对象传递给<WebMethod()>函数时遇到了这个超级描述性错误。
My Object was composed of EF entities which contained references to other entities. Setting those reference properties to nothing resolved this. No idea why this didn't cause a cyclical reference when serializing to send to the JS, but there it is.
我的对象由包含对其他实体的引用的 EF 实体组成。将这些参考属性设置为空可以解决这个问题。不知道为什么在序列化发送到 JS 时这不会导致循环引用,但确实如此。

