none
Generic Interface Issue RRS feed

  • Question

  • I'm trying develop an interface like this

    public interface IMyInterface<T>{}

    but i don't know how can I know if a class implements it?, for example:

    public class Car{}

    public class Bus{}

    public class Example1 : IMyInterface<Car> {}

    public class Example2 : IMyInterface<Bus>{}

    If i try this:

    Example1 em = new Example1();
    
    if(em is IMyInterface<?????????>)
    i need write a type in the question marks but I just need know if Example1 implents IMyInterface<'WhatEverType'> 
    How can I do that?

    Wednesday, February 6, 2019 4:32 AM

Answers

  • Hi JuanC_LopGar97,

    Try the following method.

    Example1 em = new Example1();
    
    bool result = em.GetType().GetInterfaces().Any(x => x.IsGenericType &&
      x.GetGenericTypeDefinition() == typeof(Inter<>));
    
    if(result)
    {
    
    }


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 6, 2019 6:21 AM
    Moderator

All replies

  • How can I do that?

    I don't think you can do it.

    You should understand a major purpose of using an Interface.

    https://www.c-sharpcorner.com/blogs/understanding-interfaces-via-loose-coupling-and-tight-coupling

    http://www.dotnetfunda.com/articles/show/2319/implement-decouple-architecture-in-interface

    https://msdn.microsoft.com/en-us/magazine/mt703433.aspx?f=255&MSPPError=-2147217396

    <copied>

    When looking at code to evaluate its coupling, remember the phrase “new is glue.”
    That is, anywhere you see the “new” keyword instantiating a class, realize you’re gluing your implementation to that specific implementation code.

    The Dependency Inversion Principle (bit.ly/DI-Principle) states: “Abstractions should not depend on details; details should depend on abstractions.”

    <end>

    https://dzone.com/articles/absolute-beginners-tutorial

    Wednesday, February 6, 2019 5:13 AM
  • Hi JuanC_LopGar97,

    Try the following method.

    Example1 em = new Example1();
    
    bool result = em.GetType().GetInterfaces().Any(x => x.IsGenericType &&
      x.GetGenericTypeDefinition() == typeof(Inter<>));
    
    if(result)
    {
    
    }


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 6, 2019 6:21 AM
    Moderator
  • Maybe use the next helper functions:

    private static bool IsMyInterface1<T>( IMyInterface<T> t )
    {
        return true;
    }
    private static bool IsMyInterface1( object o )
    {
        return false;
    }
    public static bool IsMyInterface( object o )
    {
        dynamic d = o;
        return IsMyInterface1( d );
    }
    

    Usage:

       if( IsMyInterface( em ) ) . . .

    But you can also introduce a non-generic interface:

    public interface IMyInterface { }
    public interface IMyInterface<T> : IMyInterface { }
    

    Then you can use 'if( em is IMyInterface )'.

    Wednesday, February 6, 2019 6:52 AM
  • I'm trying develop an interface like this

    public interface IMyInterface<T>{}

    but i don't know how can I know if a class implements it?, for example:

    public class Car{}

    public class Bus{}

    public class Example1 : IMyInterface<Car> {}

    public class Example2 : IMyInterface<Bus>{}

    If i try this:

    Example1 em = new Example1();
    
    if(em is IMyInterface<?????????>)
    i need write a type in the question marks but I just need know if Example1 implents IMyInterface<'WhatEverType'> 
    How can I do that?

    >> ...IMyInterface<'WhatEverType'>

    >> How can I do that?

    Simple, you can do that by simply writing one of the names (Car or Bus), because the 'WhatEverType' are literals that must be part of the source code.

    Programatically, you can test for each of the names, until the correct type is found:

    Example1 em = new Example1();
    if(em is IMyInterface<Car>)
    {
        // it is IMyInterface<Car>
    }
    else if(em is IMyInterface<Bus>)
    {
        // it is IMyInterface<Bus>
    }
    else
    {
        // it is neither IMyInterface<Car> nor IMyInterface<Bus>
    }
    

    Simple.

    Wednesday, February 6, 2019 7:44 AM
  • @RiteHere, that may be simple, because it's not practical. There may be a lot more classes/types than just a Car or Bus Type that can use that Interface. One can't possibly know what they all might be (hence the reason for "generic" T) and even if you did, that could end up being a huge set of if/else statements. Both @Viorel and @Zhanglong gave good generic examples of how to do this.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, February 6, 2019 4:22 PM
    Moderator
  • @RiteHere, that may be simple, because it's not practical. There may be a lot more classes/types than just a Car or Bus Type that can use that Interface. One can't possibly know what they all might be (hence the reason for "generic" T) and even if you did, that could end up being a huge set of if/else statements. Both @Viorel and @Zhanglong gave good generic examples of how to do this.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    What practical usage and in what real world usage would it even be used? It seems like some kind of pie in the sky thing.
    Wednesday, February 6, 2019 6:47 PM
  • Thank you!, it works, maybe i will get more questions in the future hahahaha, but I was not familiar with extension methods and the fact that they use delegates and lambda operators was a bit difficult to understand, but it was a clean solution.
    Wednesday, February 6, 2019 9:11 PM
  • @RiteHere, that may be simple, because it's not practical. There may be a lot more classes/types than just a Car or Bus Type that can use that Interface. One can't possibly know what they all might be (hence the reason for "generic" T) and even if you did, that could end up being a huge set of if/else statements. Both @Viorel and @Zhanglong gave good generic examples of how to do this.

    ~~Bonnie DeWitt [C# MVP? ROFL...]

    Your conclusion is r.i.d.i.c.u.l.o.u.s!

    Try to understand why:

    1- those interfaces are markers.

    2- the variable result in the code you voted up, will not tell if the C# object is marked as Car, or Bus, or any other element.

    Thus, the variable result is inocuous!

    3- So, it doesn't matter if the types are just 2 (Car and Bus) or 20! Any coder will need to write all options (2 or 20, or even more)!

    My code tells exactly what the marker is!

    The variable result in their code means almost nothing: the more are the types (Car, Bus, etc...), more useless the result is.

    P.S.: once more I'm not surprised by your extremely naive reasoning.

    Thursday, February 7, 2019 4:46 AM
  • Thank you!, it works, maybe i will get more questions in the future hahahaha, but I was not familiar with extension methods and the fact that they use delegates and lambda operators was a bit difficult to understand, but it was a clean solution.

    Hey, OP

    You marked the wrong reply as answer.

    You still don't know if the C# object it is marked as Car, or Bus, or any other!!!

    And in you question, you, clearly, stated you want to know how to replace the '???'.

    Are you looking for technical advice or what?

    If it's what, try somewhere else, because I'm trying hard to ransform this forum in technical forum for technical advices! (yes, to many freaks making questions...)

    Thursday, February 7, 2019 4:53 AM
  •  

    >> ...IMyInterface<'WhatEverType'>

    >> How can I do that?

    Simple, you can do that by simply writing one of the names (Car or Bus), because the 'WhatEverType' are literals that must be part of the source code.

    Programatically, you can test for each of the names, until the correct type is found:

    Example1 em = new Example1();
    if(em is IMyInterface<Car>)
    {
        // it is IMyInterface<Car>
    }
    else if(em is IMyInterface<Bus>)
    {
        // it is IMyInterface<Bus>
    }
    else
    {
        // it is neither IMyInterface<Car> nor IMyInterface<Bus>
    }
    

    Simple.

    Simple, and the only one that makes sense!

    Thursday, February 7, 2019 5:27 AM