check if object implements interface not working ;/

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    check if object implements interface not working ;/

    I wrote this function that is suppose to returnt rue if an array of
    types(actually stuff returned by GetInterfaces) implements the interface Q.


    private bool ContainsInterfa ce<A, Q>(A[] list)
    {
    foreach (A a in list)
    if (a is Q)
    return true;
    return false;
    }

    and is used like

    ContainsInterfa ce<Type, IEnumerable>(fi eld.FieldType.G etInterfaces()) )



    So it checks the list for elements that implement IEnumerable.



    I used this successfully in a "ContainsTy pe"



    ContainsType<At tribute, RawExcludeAttri bute>(attribute s)



    Where its the same code. The above essentially just checks if any of the
    attribute's in attributes is a RawExcludeAttri bute. It works fine. My logic
    was the same for interfaces but it doesn't work. (Maybe interfaces cannot
    implement themselfs as "a is Q" is always false... yet when I debug I get
    exactly these type for a and typeof(Q).

    Even something like

    foreach (A a in list)

    {

    Type t = typeof(Q);

    if (a.GetType() == typeof(Q))

    return true;

    }



    does not work.



    In this case

    t = + t {Name = "IEnumerabl e" FullName = "System.Collect ions.IEnumerabl e"}
    System.Type {System.Runtime Type}
    a = + a {Name = "IEnumerabl e" FullName = "System.Collect ions.IEnumerabl e"}
    System.Type {System.Runtime Type}


    So I don't see why they are not the same types ;/



    Thanks,

    Jon


  • Jon Slaughter

    #2
    Re: check if object implements interface not working ;/

    In this case
    >
    t = + t {Name = "IEnumerabl e" FullName =
    "System.Collect ions.IEnumerabl e"} System.Type {System.Runtime Type}
    a = + a {Name = "IEnumerabl e" FullName =
    "System.Collect ions.IEnumerabl e"} System.Type {System.Runtime Type}
    >
    >
    So I don't see why they are not the same types ;/
    >

    I do see something like a.GetType().ToS tring() always returns the RunTime
    Type and that is probably the reason why its not working but I don't see how
    to fix it.


    Comment

    • Jon Slaughter

      #3
      Re: check if object implements interface not working ;/

      NM, was being stupid on what I was doing. Was checking to see if the
      interfaces contained an interface but all I had to do was check and see if
      the object itself implemented it(using is).


      Comment

      • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

        #4
        Re: check if object implements interface not working ;/


        "Jon Slaughter" wrote:
        NM, was being stupid on what I was doing. Was checking to see if the
        interfaces contained an interface but all I had to do was check and see if
        the object itself implemented it(using is).
        >
        Indeed, if you have an object this should work fine

        if(myObject is IEnumerable) { ... }

        If, however, you only have the types then something like this should work

        public bool ContainsInterfa ce(Type A, Type Q)
        {
        Type[] interfaces = A.GetInterfaces ();
        foreach (Type type in interfaces)
        {
        if (type == Q)
        return true;
        }

        return false;
        }

        --
        Happy Coding!
        Morten Wennevik [C# MVP]

        Comment

        Working...