Results 1 to 5 of 5

Thread: find all classes that implement an interface

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    find all classes that implement an interface

    I searched, but only saw some code for java.

    How can this be done in C#?
    Thanks!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: find all classes that implement an interface

    Are you talking about writing code to that at run time? If so I don't see how it's practically possible. If you don't have a reference to an assembly in your project then your app can't query that assembly to know anything about what it contains. Therefore you'd have to reference every assembly in the Framework in order to do it. I'd assume that you would then have to use reflection to test every class to see if it implemented the interface using Type.GetInterface.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: find all classes that implement an interface

    You'll need to loop through the classes in the assembly. To check if it implements an interface, say

    if(typeof(classname) is interfacename)
    {
    //
    }

    Check this thread for assembly information through reflection

    http://www.vbforums.com/showthread.p...ght=reflection

    It's in VBCode, but I'm sure you can conver it.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: find all classes that implement an interface

    Quote Originally Posted by mendhak
    You'll need to loop through the classes in the assembly. To check if it implements an interface, say

    if(typeof(classname) is interfacename)
    {
    //
    }

    Check this thread for assembly information through reflection

    http://www.vbforums.com/showthread.p...ght=reflection

    It's in VBCode, but I'm sure you can conver it.
    That makes perfect sense. No wonder I didn't think of it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    Re: find all classes that implement an interface

    How do you start it out and set the type to "System". I tried several things, but none were right. It does sound weird because system is a namespace, not a type - but, you know what I mean? I'd want to loop through everything in system.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width