none
Implemented interfaces RRS feed

  • Question

  • Hi!

    How to obtain only implemented interfaces (not inherited) for specified type? Method Type.GetInterfaces() returns all the interfaces implemented and inherited by the current System.Type.

    Thanks.

    Saturday, February 24, 2007 8:53 AM

Answers

  • As far as I can tell the only way to is to check the interface mapping, and if the interface method is on the specified type you are interested in then it is not inherited.

    For example:

            private static Type[] GetDeclaredInterfaces(object x)
            {
                Type type = x.GetType();
                List<Type> typeInterfaces = new List<Type>();

                foreach (Type intrface in type.GetInterfaces())
                {
                    InterfaceMapping map = type.GetInterfaceMap(intrface);
                    // Check the first method, and assume that all the others are the same :o
                    if (map.TargetMethods[0].DeclaringType.Equals(type))
                    {
                        // Add this interface to the list of returned interfaces
                        typeInterfaces.Add(intrface);
                    }
                }

                return typeInterfaces.ToArray();         
            }
    Sunday, February 25, 2007 11:59 AM

All replies

  • As far as I can tell the only way to is to check the interface mapping, and if the interface method is on the specified type you are interested in then it is not inherited.

    For example:

            private static Type[] GetDeclaredInterfaces(object x)
            {
                Type type = x.GetType();
                List<Type> typeInterfaces = new List<Type>();

                foreach (Type intrface in type.GetInterfaces())
                {
                    InterfaceMapping map = type.GetInterfaceMap(intrface);
                    // Check the first method, and assume that all the others are the same :o
                    if (map.TargetMethods[0].DeclaringType.Equals(type))
                    {
                        // Add this interface to the list of returned interfaces
                        typeInterfaces.Add(intrface);
                    }
                }

                return typeInterfaces.ToArray();         
            }
    Sunday, February 25, 2007 11:59 AM
  • Thanks!
    Monday, February 26, 2007 6:29 AM