annamarine.blogg.se

Ambiguous reflection definition
Ambiguous reflection definition















In case of my latest example, where each interface has different implementation, you have the ability to invoke reflective any of the implementations, by choosing the correct interface. "Id", BindingFlags.Instance | BindingFlags.Public)) ĭepending which implementation you want to retrieve.

ambiguous reflection definition

In our example, if we use the interface types instead when reflecting the Id property, we will solve our case, as each of the interfaces has only one entry in its method table for the requested signature. To prevent the framework from confusion, you should use a type high enough in the inheritance hierarchy, so that it has only one entry in its method table for the member you want to reflect. When no different implementation is provided for each interface (as in your case), the only implementation you have is called by both entries in the method table for that signature, but still there are two entries pointing to the same property.

AMBIGUOUS REFLECTION DEFINITION CODE

NET framework's way to prevent you from executing code with possibly unknown/unintended behavior. The AmbiguousMatchException you received is the. See - the two properties can have different implementations, and at that point the runtime cannot know whether their implementations are unified (reflection happens at runtime now, not at compile time when unification was performed). This is because you might have implemented your class like that: public class EntityBase : IEntityBase, IObjectWithId Will look into the method table of the EntityBase class and will see two property entries for that signature and will not know which one to pick. "ID", BindingFlags.Instance | BindingFlags.Public) The following code: typeof(EntityBase).GetProperty( The two properties are automatically assigned to the same implementation in the EntityBase class by the compiler (the process is called unification). However, the EntityBase class still has two ID properties in its method table (one coming form each interface). Will compile, because the ID property satisfies both interfaces. So this class: public class EntityBase : IEntityBase, IObjectWithId Now, back to your code - you used object instead of TId which creates a rare but interesting case - the two ID properties unify because of their identical signature.

ambiguous reflection definition

Abstract class BusinessObject : IObjectWithIdĬlass EntityBase : BusinessObject, IEntityBase















Ambiguous reflection definition