Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Some useful extension methods

Some useful extension methods

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brisingr Aerowing
    wrote on last edited by
    #1

    I just came up with these two methods:

    /// <summary>
    /// Gets whether the Type <paramref name="t"/> implements the interface <paramref name="interfaceType"/>.
    /// </summary>
    /// <param name="t">The type to check.</param>
    /// <param name="interfaceType">The type of the interface.</param>
    /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
    public static bool Implements(this Type t, Type interfaceType)
    {
    return t.GetInterfaces().Contains(interfaceType);
    }

    /// <summary>
    /// Gets whether the Type <paramref name="t"/> implements the interface specified by <typeparamref name="TInterface"/>.
    /// </summary>
    /// <typeparam name="TInterface">The type of the interface to check for.</typeparam>
    /// <param name="t">The type to check.</param>
    /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
    public static bool Implements<TInterface>(this Type t)
    {
    return t.Implements(typeof(TInterface));
    }

    They simply check if the Type t implements a specific interface. I don't know of anything like this in the framework (I haven't checked).

    What do you get when you cross a joke with a rhetorical question?

    P B M B 4 Replies Last reply
    0
    • B Brisingr Aerowing

      I just came up with these two methods:

      /// <summary>
      /// Gets whether the Type <paramref name="t"/> implements the interface <paramref name="interfaceType"/>.
      /// </summary>
      /// <param name="t">The type to check.</param>
      /// <param name="interfaceType">The type of the interface.</param>
      /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
      public static bool Implements(this Type t, Type interfaceType)
      {
      return t.GetInterfaces().Contains(interfaceType);
      }

      /// <summary>
      /// Gets whether the Type <paramref name="t"/> implements the interface specified by <typeparamref name="TInterface"/>.
      /// </summary>
      /// <typeparam name="TInterface">The type of the interface to check for.</typeparam>
      /// <param name="t">The type to check.</param>
      /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
      public static bool Implements<TInterface>(this Type t)
      {
      return t.Implements(typeof(TInterface));
      }

      They simply check if the Type t implements a specific interface. I don't know of anything like this in the framework (I haven't checked).

      What do you get when you cross a joke with a rhetorical question?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      How about something like: interfaceType.IsAssignableFrom ( t ) ; (Untested, but I use IsAssignableFrom in similar cases.)

      You'll never get very far if all you do is follow instructions.

      R 1 Reply Last reply
      0
      • B Brisingr Aerowing

        I just came up with these two methods:

        /// <summary>
        /// Gets whether the Type <paramref name="t"/> implements the interface <paramref name="interfaceType"/>.
        /// </summary>
        /// <param name="t">The type to check.</param>
        /// <param name="interfaceType">The type of the interface.</param>
        /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
        public static bool Implements(this Type t, Type interfaceType)
        {
        return t.GetInterfaces().Contains(interfaceType);
        }

        /// <summary>
        /// Gets whether the Type <paramref name="t"/> implements the interface specified by <typeparamref name="TInterface"/>.
        /// </summary>
        /// <typeparam name="TInterface">The type of the interface to check for.</typeparam>
        /// <param name="t">The type to check.</param>
        /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
        public static bool Implements<TInterface>(this Type t)
        {
        return t.Implements(typeof(TInterface));
        }

        They simply check if the Type t implements a specific interface. I don't know of anything like this in the framework (I haven't checked).

        What do you get when you cross a joke with a rhetorical question?

        B Offline
        B Offline
        Bill_Hallahan
        wrote on last edited by
        #3

        .NET reflection allows querying attributes of any type. Here's one example. I did not check any of the code there, but I expect the second example works. http://stackoverflow.com/questions/5114469/how-to-check-whether-an-object-has-certain-method-property[^]

        1 Reply Last reply
        0
        • B Brisingr Aerowing

          I just came up with these two methods:

          /// <summary>
          /// Gets whether the Type <paramref name="t"/> implements the interface <paramref name="interfaceType"/>.
          /// </summary>
          /// <param name="t">The type to check.</param>
          /// <param name="interfaceType">The type of the interface.</param>
          /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
          public static bool Implements(this Type t, Type interfaceType)
          {
          return t.GetInterfaces().Contains(interfaceType);
          }

          /// <summary>
          /// Gets whether the Type <paramref name="t"/> implements the interface specified by <typeparamref name="TInterface"/>.
          /// </summary>
          /// <typeparam name="TInterface">The type of the interface to check for.</typeparam>
          /// <param name="t">The type to check.</param>
          /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
          public static bool Implements<TInterface>(this Type t)
          {
          return t.Implements(typeof(TInterface));
          }

          They simply check if the Type t implements a specific interface. I don't know of anything like this in the framework (I haven't checked).

          What do you get when you cross a joke with a rhetorical question?

          M Offline
          M Offline
          Matt T Heffron
          wrote on last edited by
          #4

          Is it normally the case that you have a Type that you need to check vs. an actual instance? For an instance there's:

          if (foo is ISomething)

          B 1 Reply Last reply
          0
          • M Matt T Heffron

            Is it normally the case that you have a Type that you need to check vs. an actual instance? For an instance there's:

            if (foo is ISomething)

            B Offline
            B Offline
            Brisingr Aerowing
            wrote on last edited by
            #5

            Reflecting over the types in an assembly, for instance (i.e. a basic plugin system).

            What do you get when you cross a joke with a rhetorical question?

            1 Reply Last reply
            0
            • B Brisingr Aerowing

              I just came up with these two methods:

              /// <summary>
              /// Gets whether the Type <paramref name="t"/> implements the interface <paramref name="interfaceType"/>.
              /// </summary>
              /// <param name="t">The type to check.</param>
              /// <param name="interfaceType">The type of the interface.</param>
              /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
              public static bool Implements(this Type t, Type interfaceType)
              {
              return t.GetInterfaces().Contains(interfaceType);
              }

              /// <summary>
              /// Gets whether the Type <paramref name="t"/> implements the interface specified by <typeparamref name="TInterface"/>.
              /// </summary>
              /// <typeparam name="TInterface">The type of the interface to check for.</typeparam>
              /// <param name="t">The type to check.</param>
              /// <returns><c>True</c> if <paramref name="t"/> implements the interface, <c>false</c> otherwise.</returns>
              public static bool Implements<TInterface>(this Type t)
              {
              return t.Implements(typeof(TInterface));
              }

              They simply check if the Type t implements a specific interface. I don't know of anything like this in the framework (I haven't checked).

              What do you get when you cross a joke with a rhetorical question?

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              I guess since they're one-liners they weren't thought necessary. I don't think they exist in the framework so if they make things clearer then yes, they count as useful :)

              1 Reply Last reply
              0
              • P PIEBALDconsult

                How about something like: interfaceType.IsAssignableFrom ( t ) ; (Untested, but I use IsAssignableFrom in similar cases.)

                You'll never get very far if all you do is follow instructions.

                R Offline
                R Offline
                robocodeboy
                wrote on last edited by
                #7

                I was going to suggest the same. Basically, instead of writing someType.implements(interface) you write interface.IsAssignableFrom(someType) . For some reason, when I encountered this method the first time, I thought that an "Implements" method would make more sense, even if they are basically the same.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups