Guard.TypeIsAssignableFromType()

Topics: CAB & Smart Client Software Factory
Jul 24, 2007 at 3:52 PM
The parameter descriptions and ordering for the Guard.TypeIsAssignableFromType() method don't seem correct. The name implies that you are checking to see if the first parameter type can be assigned FROM the second. Instead, what it checks is whether the first parameter type can be assigned TO the second parameter type.

Here is the code:

/// <summary>
/// Verifies that an argument type is assignable from the provided type (meaning
/// interfaces are implemented, or classes exist in the base class hierarchy).
/// </summary>
/// <param name="assignee">The argument type.</param>
/// <param name="providedType">The type it must be assignable from.</param>
/// <param name="argumentName">The argument name.</param>
public static void TypeIsAssignableFromType(Type assignee, Type providedType, string argumentName)
{
if (!providedType.IsAssignableFrom(assignee))
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
Properties.Resources.TypeNotCompatible, assignee, providedType), argumentName);
}


If I had a method which took an EventArgs type as a parameter, I would want to check that the parameter passed to the method was either an EventArgs, or extended EventAgs. That is, I want to see if some local EventArgs variable is going to be assignable from the type passed in. To do this I would think you might say Guard.IsAssignableFromType(typeof(EventArgs), parameterType, "parameterType"), but really you have to say Guard.IsAssignableFromType(parametertype, typeof(EventArgs), "parameterType"). While the ordering is misleading, it wouldn't be so bad if the description of the second parameter was incorrect: "The type it must be assignable from". This should actually read, "The type it must be assignable to.".
Developer
Jul 24, 2007 at 5:33 PM
Hi, you are right. I’m going to create a WorkItem for this.

Thanks for your feedback.

Ezequiel Jadib
[url:http://staff.southworks.net/blogs/ejadib
Developer
Jul 24, 2007 at 5:34 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.