Some useful extension methods
-
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?
-
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?
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.
-
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?
.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[^]
-
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?
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)
-
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)
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?
-
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?
-
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.
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.