All posts in Type

In Making a Better ObservableCollection Part 1 – Extensions, I talked about ways we can extend ObservableCollections to make them more useful when working with the MVVM pattern and WPF. This time we are going to talk about extending Types in C# so that they are even more powerful than before.
 

GetPropertyExtended

One thing I have always found annoying about Type.GetProperty(“MyProperty”) is that it never observes inherited properties; only the exact class you are investigating. We can easily solve this by using the following code:


/// <summary>
/// Gets the property from the type or base types.
/// Note: Type.GetProperty only looks at the actual type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="propertyName">Name of the property.</param>
/// <returns></returns>
public static PropertyInfo GetPropertyExtended(this Type type, string propertyName)
{
    // Get property
    var property = type.GetProperty(propertyName);
    if (property != null) return property;

    // Base types
    var baseTypes = type.GetInterfaces();
    foreach (var baseType in baseTypes)
    {
        property = baseType.GetProperty(propertyName);
        if (property != null) return property;
    }

    // Nothing found
    return null;
}

 
This will investigate all interfaces associated with the target class and return the first match.

 

InheritsFrom

Another thing I would like to know is when a class inherits from another class and I would like to do this in a simple bool statement. That can be accomplished with the following code:


/// <summary>
/// Inherits from.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type">The type.</param>
/// <returns></returns>
public static bool InheritsFrom<T>(this Type type)
{
    return type.InheritsFrom(typeof(T));
}

/// <summary>
/// Inherits from.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="targetType">Type of the target.</param>
/// <returns></returns>
public static bool InheritsFrom(this Type type, Type targetType)
{
    return type.GetInterface(targetType.ToString()) != null;
}

 
All we are doing here is checking to see if the target type is a base for our class. This method can be incredibly helpful when you are quickly trying to discover parentage and only want to use interfaces.

 

IsObservableCollection

This speaks for itself. We use this extension to determine if the following property or member we are dealing with is an ObservableCollection. This can be extremely helpful when making determinations around the CollectionChanged event but you might start with a less complex object like an IList or an ICollection.


/// <summary>
/// Determines whether [is observable collection] [the specified candidate].
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public static bool IsObservableCollection(this Type type)
{
    // Evaluate
    return type.IsGenericType && !type.IsGenericTypeDefinition &&
        (type.GetGenericTypeDefinition() == targetType);
}

 
Next time, we will talk about extending Enums.

Happy coding…