Click here to Skip to main content
15,884,836 members
Articles / Programming Languages / C# 4.0

ReflectionHelper

Rate me:
Please Sign up or sign in to vote.
4.93/5 (31 votes)
6 Aug 2012CPOL4 min read 72.4K   2.2K   79  
This class makes getting MemberInfos easy, without the use of magic strings (so it is refactoring friendly) and also allows you to create delegates to do fast accesses to those items, much faster than the normal Invoke, GetValue or SetValue methods.
using System;
using System.Reflection;

namespace Pfz.Extensions
{
	/// <summary>
	/// Adds some useful methods to the MemberInfo and Type types to
	/// work easily with attributes (Generics).
	/// </summary>
	public static class PfzAttributeExtensions
	{
		#region GetCustomAttributes
			/// <summary>
			/// Gets the attributes of the specified type, and return them typed.
			/// </summary>
			/// <typeparam name="T">The type of the attribute to find and to return.</typeparam>
			/// <param name="memberInfo">The memberInfo where the attributes will be searched.</param>
			/// <returns>A typed array with the attributes found.</returns>
			public static T[] GetCustomAttributes<T>(this MemberInfo memberInfo)
			where
				T: Attribute
			{
				if (memberInfo == null)
					throw new ArgumentNullException("memberInfo");

				return (T[])memberInfo.GetCustomAttributes(typeof(T), false);
			}

			/// <summary>
			/// Gets the attributes of the specified type, and return them typed.
			/// </summary>
			/// <typeparam name="T">The type of the attribute to find and to return.</typeparam>
			/// <param name="type">The type that can contains the attributes that will be searched.</param>
			/// <param name="inherit">If true search the attribute in base classes, but only if the attribute supports inheritance.</param>
			/// <returns>A typed array with the attributes found.</returns>
			public static T[] GetCustomAttributes<T>(this Type type, bool inherit)
			where
				T: Attribute
			{
				if (type == null)
					throw new ArgumentNullException("type");

				return (T[])type.GetCustomAttributes(typeof(T), inherit);
			}

			/// <summary>
			/// Gets the custom attributes of a parameterInfo, already typed.
			/// </summary>
			public static T[] GetCustomAttributes<T>(this ParameterInfo parameterInfo)
			where
				T: Attribute
			{
				if (parameterInfo == null)
					throw new ArgumentNullException("parameterInfo");

				return (T[])parameterInfo.GetCustomAttributes(typeof(T), false);
			}
		#endregion
		#region GetCustomAttribute
			/// <summary>
			/// Gets an attribute of the specified type, or null.
			/// This is useful when the attribute has AllowMultiple=false, but
			/// don't use it if the class can have more than one attribute of such
			/// type, as this method throws an exception when this happens.
			/// </summary>
			/// <typeparam name="T">The type of the parameter to find.</typeparam>
			/// <param name="memberInfo">The member info to search the attribute.</param>
			/// <returns>The found attribute or null.</returns>
			public static T GetCustomAttribute<T>(this MemberInfo memberInfo)
			where
				T: Attribute
			{
				T[] attributes = memberInfo.GetCustomAttributes<T>();
				
				switch(attributes.Length)
				{
					case 0:
						return null;
					
					case 1:
						return attributes[0];
				}

				throw new InvalidOperationException("There is more than one attribute of type " + typeof(T).FullName + ".");
			}

			/// <summary>
			/// Gets an attribute of the specified type, or null.
			/// This is useful when the attribute has AllowMultiple=false, but
			/// don't use it if the class can have more than one attribute of such
			/// type, as this method throws an exception when this happens.
			/// </summary>
			/// <typeparam name="T">The type of the parameter to find.</typeparam>
			/// <param name="type">The type to search the attribute.</param>
			/// <param name="inherit">true to search in base classes for attributes that support inheritance.</param>
			/// <returns>The found attribute or null.</returns>
			public static T GetCustomAttribute<T>(this Type type, bool inherit)
			where
				T: Attribute
			{
				T[] attributes = type.GetCustomAttributes<T>(inherit);
				
				switch(attributes.Length)
				{
					case 0:
						return null;
					
					case 1:
						return attributes[0];
				}

				throw new InvalidOperationException("There is more than one attribute of type " + typeof(T).FullName + ".");
			}

			/// <summary>
			/// Gets a CustomAttribute set to this parameterInfo.
			/// </summary>
			public static T GetCustomAttribute<T>(this ParameterInfo parameterInfo)
			where
				T: Attribute
			{
				T[] result = parameterInfo.GetCustomAttributes<T>();

				switch(result.Length)
				{
					case 0:
						return null;

					case 1:
						return result[0];
				}

				throw new InvalidOperationException("There is more than one attribute of type " + typeof(T).FullName + ".");
			}
		#endregion
		#region ContainsCustomAttribute
			/// <summary>
			/// Verifies if a member contains an specific custom attribute.
			/// </summary>
			/// <typeparam name="T">The type of the attribute to check for existance.</typeparam>
			/// <param name="member">The member in which to find search for attribute.</param>
			/// <returns>true if the member constains the attribute, false otherwise.</returns>
			public static bool ContainsCustomAttribute<T>(this MemberInfo member)
			where
				T: Attribute
			{
				if (member == null)
					throw new ArgumentNullException("member");

				return member.IsDefined(typeof(T), false);
			}

			/// <summary>
			/// Verifies if a type contains an specific custom attribute.
			/// </summary>
			/// <typeparam name="T">The type of the attribute to check for existance.</typeparam>
			/// <param name="type">The member in which to find search for attribute.</param>
			/// <param name="inherit">true to search in base classes for attributes that support inheritance.</param>
			/// <returns>true if the member constains the attribute, false otherwise.</returns>
			public static bool ContainsCustomAttribute<T>(this Type type, bool inherit)
			where
				T: Attribute
			{
				if (type == null)
					throw new ArgumentNullException("type");

				return type.IsDefined(typeof(T), inherit);
			}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft
United States United States
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).

Comments and Discussions