Some Helpful Extensions When Dealing With Types in .NET
If you are writing reusable code, chances are high that you will write quite some code that deals with types, generics, and interfaces.
Join the DZone community and get the full member experience.Join For Free
If you are writing reusable code, chances are high that you will write quite some code that deals with types, generics, and interfaces. Over the years, the collection of my helper extensions for that have grown. As some of my upcoming posts use them, I share them (also) for future reference.
1. Check if a Type Is Deriving From Another Type
Deriving types is a common practice. To some extent, you can use pattern matching. Sometimes, that isn't enough, though (especially if you have a multi-level derivation path). This is when I use one of these two extensions:
2. Get Type of T From IEnumerable<T>
Sometimes, one needs to know the item type of an IEnumerable<T>. These two extensions will help you in this case:
3. Check if a Type Implements a Certain Interface
Interfaces are supposed to make the life of a developer easier. Like with the type derivation, sometimes we need to know if a type implements a certain interface. This extension answers the question for you:
4. Find a Type in an External Assembly
If you need a type that lives in an external assembly but you only have it's
FullName, it's easy to adapt), this one will resolve the Type for you as long as its assembly is loaded into your
Like I said in the beginning, this post will be used for future reference. These extensions made a lot of controls, MVVM implementations, and business logic I wrote in the past (both for work and my private projects) a whole lot easier. Like always, I hope this post will be helpful for some of you.
Please find the full class in this Gist.
Published at DZone with permission of Marco Siccardi, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.