【发布时间】:2019-02-05 13:58:12
【问题描述】:
using System;
using System.Reflection;
namespace A
{
interface IObjectWithId<TId>
{
TId Id { get; }
}
interface IEntityBase : IObjectWithId<object>
{
new object Id { get; }
}
abstract class BusinessObject<TId> : IObjectWithId<TId>
{
public abstract TId Id { get; }
}
class EntityBase : BusinessObject<object>, IEntityBase
{
public override object Id { get { return null; } }
}
public static class Program
{
public static void Main()
{
Console.WriteLine(typeof(EntityBase).GetProperty("Id", BindingFlags.Instance | BindingFlags.Public));
}
}
}
我得到了这个:
System.Reflection.AmbiguousMatchException was unhandled
Message="Ambiguous match found."
Source="mscorlib"
StackTrace:
at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
at System.Type.GetProperty(String name, BindingFlags bindingAttr)
at A.Program.Main() in C:\Home\work\A\Program.cs:line 26
InnerException:
Microsoft Visual Studio 2008
版本 9.0.30729.1 SP
Microsoft .NET 框架
版本 3.5 SP1
编辑:
奇怪的是,看起来其他人无法复制它。虽然它每次都会在我的机器上崩溃。我发现这段代码:
Console.WriteLine(typeof(EntityBase).GetProperty("Id", BindingFlags.Instance | BindingFlags.Public, null, typeof(object), Type.EmptyTypes, null));
工作正常,虽然应该是一样的。
【问题讨论】:
-
没有提供更多信息的异常细节吗?
-
我刚刚在 VS2008/.net 3.5 中运行了这段代码,没有遇到异常。您使用的是哪个版本的 .net?
-
我也不例外。 VS2008/.Net 3.5
-
这不是我发明的,伙计们。这真的发生在我身上。
-
我删除了关于信息,因为它在这里无关紧要。
标签: .net reflection