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 4086642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:45:12+00:00 2026-05-20T18:45:12+00:00

I have a class like the below. GetInterfaces() says If the current Type represents

  • 0

I have a class like the below. GetInterfaces() says

If the current Type represents a type
parameter in the definition of a
generic type or generic method, this
method searches the interface
constraints and any interfaces
inherited from class or interface
constraints.

Is it possible for me to not get any inherited interface? When i use GetInterfaces on ABC i only want to see DEF, not DEF and GHI.

interface DEF : GHI {...}
class ABC : DEF {...}
  • 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-20T18:45:13+00:00Added an answer on May 20, 2026 at 6:45 pm

    Firstly, the MSDN snippet you’ve posted doesn’t have anything to do with your actual question. It deals with when you have, for example, a generic type such as class Foo<T> where T : IEnumerable, and you try calling GetInterfaces on the type-parameter T, for example through typeof(Foo<>).GetGenericArguments().Single().GetInterfaces().

    Secondly, the problem is slightly ill-specified. Note that when a class implements an interface, it must implement all of the interfaces ‘inherited’ by that interface. It’s simply a C# convenience feature that lets you omit the inherited interfaces in the class-declaration. In your example, it’s perfectly legal (and no different) to explicitly include the ‘inherited’ GHI interface:

    class ABC : DEF, GHI {...}
    

    I’ve assumed that what you really want to do is find a ‘minimal set’ of interfaces that ‘covers’ all of the type’s implemented interfaces. This results in a slightly simplified version of the Set cover problem.

    Here’s one way to solve it, without any attempt whatsoever to be algorithmically efficient. The idea is to produce the minimal interface-set by filtering out those interfaces that are already implemented by other interfaces implemented by the type.

    Type type = ...
    
    var allInterfaces = type.GetInterfaces();    
    var minimalInterfaces = from iType in allInterfaces 
                            where !allInterfaces.Any(t => t.GetInterfaces()
                                                           .Contains(iType))
                            select iType;
    

    (
    EDIT – Here’s a better way of doing the above:

    var minimalInterfaces = allInterfaces.Except
                            (allInterfaces.SelectMany(t => t.GetInterfaces()));
    

    )

    For example, for List<int>:

    allInterfaces: 
    
    System.Collections.Generic.IList`1[System.Int32]
    System.Collections.Generic.ICollection`1[System.Int32]
    System.Collections.Generic.IEnumerable`1[System.Int32]
    System.Collections.IEnumerable
    System.Collections.IList
    System.Collections.ICollection
    
    minimalInterfaces:
    
    System.Collections.Generic.IList`1[System.Int32]
    System.Collections.IList
    

    Do note that this solution covers interface ‘hierarchies’ only (which is what you appear to want), not how they relate to the class’s class hierarchy. In particular, it pays no attention to where in a class’s hierarchy an interface was first implemented.

    For example, let’s say we have:

    interface IFoo { }
    interface IBar : IFoo { }
    interface IBaz { } 
    
    class Base : IBar {  }
    class Derived : Base, IBaz {  }
    

    Now if you try using the solution I’ve described to get the minimal interface-set for Derived, you would get IBaz as well as IBar. If you don’t want IBar, you would have to go to more effort: eliminate interfaces implemented by base-classes. The easiest way to do this would be to remove from the minimal interface-set those interfaces implemented by the class’s immediate base-class, as is mentioned in @MikeEast’s answer.

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

Sidebar

Related Questions

I have class like this below shown. which contains the shopping items where the
If I have a class which has an interface definition like below; public class
I have a class like the below and am wondering, will this be thread-safe
I have a class like below with a method that just returns a String,
I have a generic class like below: public class MyClass<T, TProperty> { MyClass(Expression<Func<T, TProperty>>
I have created the class like below. But Override method is not called. Is
I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
If I have a class that looks like below, how to I pull out
Say I have a simple address class like below: public class Address { public
i have a model say modela with a calculate field. like below class modelA

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.