Advertisement
Guest User

AbstractAttributeReflectionFactory

a guest
Feb 16th, 2011
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.ComponentModel.Property;
  3. using System.Linq;
  4.  
  5.  
  6. namespace System.Reflection
  7. {
  8.     /// <summary>
  9.     ///   An abstract factory which allows to create instances for a specific type
  10.     ///   by using reflection to get information on what to create through attributes applied to its class members.
  11.     /// </summary>
  12.     /// <author>Steven Jeuris</author>
  13.     public abstract class AbstractAttributeReflectionFactory
  14.     {
  15.         readonly Property<Type> _ownerType = new Property<Type>();
  16.  
  17.         /// <summary>
  18.         ///   The type of the class for which to create instances.
  19.         /// </summary>
  20.         protected Type OwnerType
  21.         {
  22.             get { return _ownerType.GetValue(); }
  23.             set { _ownerType.SetValue( value, OnNewOwnerType ); }
  24.         }
  25.  
  26.         /// <summary>
  27.         ///   The list of members with relevant attibute(s) added to it.
  28.         /// </summary>
  29.         protected Dictionary<MemberInfo, Attribute[]> AttributedMembers { get; private set; }
  30.  
  31.  
  32.         /// <summary>
  33.         ///   Create a new attribute reflection factory for a given type.
  34.         /// </summary>
  35.         /// <param name = "ownerType">The type on which to do reflection and generate instances.</param>
  36.         protected AbstractAttributeReflectionFactory( Type ownerType )
  37.         {
  38.             OwnerType = ownerType;
  39.         }
  40.  
  41.  
  42.         #region Abstract definitions
  43.  
  44.         /// <summary>
  45.         ///   Get a list of all the attribute types used by the factory.
  46.         /// </summary>
  47.         /// <returns>A list of all the attribute types used by the factory.</returns>
  48.         protected abstract Type[] GetAttributeTypes();
  49.  
  50.         #endregion
  51.  
  52.  
  53.         void OnNewOwnerType( Type oldType, Type newType )
  54.         {
  55.             // Initialize the list of attributed members for the new owner type.
  56.             if ( newType == null )
  57.             {
  58.                 AttributedMembers = new Dictionary<MemberInfo, Attribute[]>();
  59.             }
  60.             else
  61.             {
  62.                 // Get all wanted attribute types for all members.
  63.                 AttributedMembers = (from member in newType.GetMembers( ReflectionHelper.AllClassMembers )
  64.                                      from type in GetAttributeTypes()
  65.                                      from attribute in (Attribute[])member.GetCustomAttributes( type, false )
  66.                                      group attribute by member).ToDictionary( g => g.Key, g => g.ToArray() );
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement