Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:10:56+00:00 2026-05-23T04:10:56+00:00

using System; using System.Reflection; namespace A { interface IObjectWithId<TId> { TId Id { get;

  • 0
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));
    }
  }
}

I am getting this:

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
Version 9.0.30729.1 SP
Microsoft .NET Framework
Version 3.5 SP1

EDIT:

Strangely enough, looks like other folks fail to reproduce it. Though it does crash on my machine every time. I found out that this code:

Console.WriteLine(typeof(EntityBase).GetProperty("Id", BindingFlags.Instance | BindingFlags.Public, null, typeof(object), Type.EmptyTypes, null));

Does work OK, although it should be the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:10:57+00:00Added an answer on May 23, 2026 at 4:10 am

    To get your question answered, I’ll get you familiar with the term ‘Method Table’. This is part of the internal representation of types in the .NET framework, where every .NET type has its own method table. You can imagine it as a hash map (or a dictionary) containing all the methods and properties of the type. The key is the method/property signature (method name and parameter types, without the return type) and the value is a collection of matching methods/properties, alongside some reflection metadata information such as which type has declared the method/property.

    When class A derives from a base class – B, or implements an interface C, the items in the method table of B and C become directly available in the method table of A. If the method table of A already contains an item with certain signature, that item is added to the collection for the same signature, so now A will have 2 methods/properties that the signature points to. The only way to distinguish these duplicate entries is by comparing the metadata describing the type on which behalf the signature is declared.

    Lets take the interface IObjectWithId<TId>, which defines a property TId ID { get; set; }. The EntityBase class implements IObjectWithId<TId> so receives an TId ID { get; set; } property to its method table. At the same time this class implements the IEntityBase interface, which gives it the Object ID { get; set; } property. The EntityBase class then receives two properties under the same signature (because the return type does not participate in the signature), while it will still expose 2 different properties. The following declaration will result in a compile error:

       public class EntityBase : IEntityBase, IObjectWithId<int>
       {
            public int ID { get; set; }
       }
    

    because the IEntityBase is not implemented. Similarly, the follwing will also fail:

       public class EntityBase : IEntityBase, IObjectWithId<int>
       {
            public object ID { get; set; }
       }
    

    because this time IObjectWithId<int> is not satisfied. You may try to do this:

       public class EntityBase : IEntityBase, IObjectWithId<int>
       {
            public object ID { get; set; }
            public int ID { get; set; }
       }
    

    just to receive another compilation error for having 2 properties with the same signature.

    The way to work this around, is to implement at least one of the conflicting signatures explicitly:

       public class EntityBase : IEntityBase, IObjectWithId<int>
       {
            private object objID;
            private int intID;
    
            object IEntityBase.ID { get { return objID; } set { objID = value; } }     
            int IObjectWithId<int>.ID { get { return intID; } set { intID = value; } }
       }
    

    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. So this class:

       public class EntityBase : IEntityBase, IObjectWithId<object>
       {
            public object ID { get; set; }
       }
    

    will compile, because the ID property satisfies both interfaces. However, the EntityBase class still has two ID properties in its method table (one coming form each interface). The two properties are automatically assigned to the same implementation in the EntityBase class by the compiler (the process is called unification).

    The following code:

    typeof(EntityBase).GetProperty(
        "ID", BindingFlags.Instance | BindingFlags.Public);
    

    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.

    This is because you might have implemented your class like that:

       public class EntityBase : IEntityBase, IObjectWithId<object>
       {
            private object objID1;
            private int objID2;
    
            object IEntityBase.ID 
            { 
                get { return objID1; } 
                set { objID1 = value; } 
            }
    
            object IObjectWithId<object>.ID 
            {
                get { return objID2; } 
                set { objID2 = value; } 
            }
       }
    

    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). The AmbiguousMatchException you received is the .NET framework’s way to prevent you from executing code with possibly unknown/unintended behavior.

    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. 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. 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.

    You can then use

    Console.WriteLine(
        typeof(IEntityBase).GetProperty(
            "Id", BindingFlags.Instance | BindingFlags.Public));
    

    or

    Console.WriteLine(
        typeof(BusinessObject<object>).GetProperty(
            "Id", BindingFlags.Instance | BindingFlags.Public));
    

    depending which implementation you want to retrieve. 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. In the example from your question, you may use whichever interface you want, as both have one implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using System; namespace random { interface IHelper { void HelpMeNow(); } public class Base
How can I generate types like these using the System.Reflection.Emit libraries: public class Test<T>
using System; using Castle.Windsor; using Castle.MicroKernel.Registration; using System.Reflection; using Castle.MicroKernel.Resolvers.SpecializedResolvers; namespace Windsor { class
using System; using System.Reflection; namespace Reflection { class Test { protected void methodname() {
using System; using System.Reflection; namespace Reflection { class Test { protected void methodname(int i)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCount { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ConsoleApplication1 { public
using System; using System.Math; class test { public static void Main() { Console.Write(Enter any
I want to generate IL for the 2D array construction, using System.Reflection.Emit namespace. My
I am using System.Reflection to load a type that I cannot otherwise load during

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.