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

  • SEARCH
  • Home
  • 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 437761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:36:38+00:00 2026-05-12T20:36:38+00:00

I have a type and an interface and I need to verify that the

  • 0

I have a type and an interface and I need to verify that the type implements the interface abstractly.

I have set to write a brute force code using Reflection and it is pretty ugly.

I am wondering if there is a better way than the brute force implementation I am doing now.

Any ideas?

Thanks.

EDIT

Have not checked the implementation yet, but the brute force draft code looks like this:

  public static bool IsAbstractInterfaceImplementation(Type someType, Type someInterface)
  {
    if (!someInterface.IsAssignableFrom(someType))
    {
      return false;
    }

    if (!someType.IsAbstract)
    {
      return false;
    }

    var m_interfaceMemberNames = someInterface.GetMembers().Select(m => m.Name).ToList();
    // Make sure every interface member implementation is abstract.
    foreach (var typeMember in someType.FindMembers(MemberTypes.Event | MemberTypes.Property | MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance, null, null))
    {
      if (m_interfaceMemberNames.Contains(typeMember.Name))
      {
        MethodInfo method;
        // Make sure the ancestor member is abstract.
        switch (typeMember.MemberType)
        {
        case MemberTypes.Event:
          if (!IsAbstractImplementation(((EventInfo)typeMember).GetAddMethod()))
          {
            return false;
          }
          method = ((EventInfo)typeMember).GetRemoveMethod();
          break;
        case MemberTypes.Property:
          method = ((PropertyInfo)typeMember).GetGetMethod();
        default:
          method = (MethodInfo)typeMember;
          break;
        }
        if (!IsAbstractImplementation(method))
        {
          return false;
        }
      }
    }
    return true;
  }

  public static bool IsAbstractImplementation(MethodInfo methodInfo)
  {
    const MethodAttributes expectedAttributes =
      MethodAttributes.Abstract |
      MethodAttributes.Public |
      MethodAttributes.NewSlot |
      MethodAttributes.Virtual;

    return (methodInfo.Attributes & expectedAttributes) == expectedAttributes;
  }

Without compiling it I already see a problem with properties, that the code has to check whether interface defines getter and/or setter and verify the right method(s), instead of blindly assuming the getter. Anyway, as one can see, the code is pretty dull. I am wondering if there is a better way…

EDIT 2

  • I wish to stress, that this is just a draft implementation, it works for simple cases and it is broken for more complex ones, like when there are method overloads or method renames (I do not know VB, so I did not even think it was possible). But it emphasizes my point that it demands much work to do it right.
  • Why would I want such a thing? We need to create types dynamically using Reflection.Emit based on certain dynamically acquired metadata. The generated dynamic type implements certain interface, say IDynamicObject, and may derive from some ancestor type. That ancestor type is statically compiled. Until recently, the ancestor type was not allowed to implement the IDynamicObject interface. Given an instance of the dynamic type, one had to explicitly cast it to IDynamicObject in order to gain access to its methods (remember, that the generated dynamic type does implement the interface). I would like to eliminate these explicit casts. The only way to do so is by letting the ancestor type implement the IDynamicObject interface. However, the implementation must be all abstract, which is verified by the dynamic type creation code. Voila.
  • 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-12T20:36:38+00:00Added an answer on May 12, 2026 at 8:36 pm

    You can determine if a type implements a particular interface by using Type.IsAssignableFrom:

    typeof(MyInterface).IsAssignableFrom(abstractType);
    

    Edit: after clarification was added to the answer – to determine if all of an interface’s implementations are abstract for a given class, you can do so much more easily by getting an InterfaceMap for the type in question:

    bool IsAbstractOfInterface(Type classType, Type interfaceType)
    {
        var map = classType.GetInterfaceMap(interfaceType);
        foreach (var info in map.TargetMethods)
        {
            if (!info.IsAbstract)
            {
                return false;
            }
        }
        return true;
    }
    

    Or maybe a generic extension method…

    public static bool IsAbstractOf<TInterface>(this Type type)
    {
        var map = type.GetInterfaceMap(typeof(TInterface));
        foreach (var info in map.TargetMethods)
        {
            if (!info.IsAbstract)
            {
                return false;
            }
        }
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to have a type-safe bag of items that all implement a generic interface.
I need to know if a Type implements an interface. Dim asmRule As System.Reflection.Assembly
I have an interface that has a generic method with two type parameters. I
I have a MethodInfo of an interface method and Type of a class that
I have a legacy interface that gives me the type to instance under the
I have an interface specifying method that takes a generic type as input which
I have an interface hierarchy similar to - interface Type { Type copy(); };
I have an interface parametrized on a type which extends a parametrized type, and
I have a hierarchy interface of IRoot where H is the hierarchy type and
A single interface may also have multiple IPv6 addresses of any type(unicast, anycast, and

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.