Advertisement
Guest User

StackOverflow-Getting Nested Types from Higher in Hierarchy

a guest
Feb 17th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.24 KB | None | 0 0
  1. public class CustomSortDictionary<T> : Dictionary<ID, Comparison<T>>
  2. {
  3.     #region Fields
  4.     private Type sortMethod;
  5.     private static BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
  6.     #endregion
  7.  
  8.     #region Properties
  9.     public Type SortMethod { get { return sortMethod; } }
  10.     #endregion
  11.  
  12.     #region Constructors
  13.     /// <summary>
  14.     /// Creates a new CustomSortDictionary.
  15.     /// </summary>
  16.     /// <param name="sortMethod">The SortMethod class to use.</param>
  17.     public CustomSortDictionary(Type sortMethod) : base()
  18.     {
  19.         this.sortMethod = sortMethod;
  20.  
  21.         MethodInfo m = SortMethod.GetMethod("ConstructCustomSortMethods", bindingFlags);
  22.         m.Invoke(null, null);
  23.     }
  24.     #endregion
  25.  
  26.     #region Methods
  27.     /// <summary>
  28.     /// Adds a custom sort method to the dictionary and the CitationCollection class.
  29.     /// </summary>
  30.     /// <param name="customSortMethod">The comparison by which to sort in the custom sort.</param>
  31.     /// <param name="name">The name that will be used to refer to the custom sort in the CitationCollection class.</param>
  32.     public void Add(ref string name, Comparison<T> customSortMethod)
  33.     {
  34.         ID key = ((IDCollection)SortMethod.GetNestedType("CustomSort", bindingFlags).GetField("Methods").GetValue(null)).Add(ref name);
  35.         this.Add(key, customSortMethod);
  36.     }
  37.     #endregion
  38. }
  39.  
  40. /// <summary>
  41. /// Abstract class for SortMethod classes.
  42. /// </summary>
  43. /// <remarks>Added IDs must start at the value 3.</remarks>
  44. public abstract class ASortMethod
  45. {
  46.     public static readonly ID Manual = 1;
  47.  
  48.     private static readonly ID CustomSortEnum = 2;
  49.  
  50.     /// <summary>
  51.     /// Used for custom sorting methods.
  52.     /// </summary>
  53.     public static class CustomSort
  54.     {
  55.         public static readonly ID None = CustomSortEnum[0];
  56.  
  57.         /// <summary>
  58.         /// An IDCollection containing the custom sorting methods that correspond to CitationCollection.CustomSorts.
  59.         /// </summary>
  60.         public static IDCollection Methods;
  61.     }
  62.  
  63.     /// <summary>
  64.     /// If SortMethod.CustomSort.Methods is not initialized, it is initialized.
  65.     /// </summary>
  66.     public static void ConstructCustomSortMethods()
  67.     {
  68.         if (CustomSort.Methods == null)
  69.         {
  70.             CustomSort.Methods = new IDCollection(CustomSortEnum, typeof(CustomSort));
  71.         }
  72.     }
  73.  
  74.     /// <summary>
  75.     /// Determines whether the given ID exists in SortMethod.
  76.     /// </summary>
  77.     /// <param name="method">The ID to check for.</param>
  78.     /// <returns>Whether or not it exists in either the class or its CustomSort methods.</returns>
  79.     public static bool Exists(ID method)
  80.     {
  81.         List<FieldInfo> fields = (from FieldInfo f in typeof(ASortMethod).GetFields()
  82.                                   where f.FieldType == typeof(ID) &&
  83.                                         (ID)f.GetValue(null) == method
  84.                                   select f).ToList<FieldInfo>();
  85.  
  86.         return fields.Count > 0;
  87.     }
  88.  
  89.     /// <summary>
  90.     /// Determines whether the ID with the given name exists in SortMethod.
  91.     /// </summary>
  92.     /// <param name="methodName">The name of the ID.</param>
  93.     /// <returns>Whether or not it exists in either the class or its CustomSort methods.</returns>
  94.     public static bool Exists(string methodName)
  95.     {
  96.         List<FieldInfo> fields = (from FieldInfo f in typeof(ASortMethod).GetFields()
  97.                                   where f.FieldType == typeof(ID) &&
  98.                                         f.Name == methodName
  99.                                   select f).ToList<FieldInfo>();
  100.  
  101.         return fields.Count > 0;
  102.     }
  103. }
  104.  
  105. class CitationCollection : List<Citation>
  106. {
  107.     /// <summary>
  108.     /// The methods by which the CitationCollection may be sorted.
  109.     /// </summary>
  110.     public sealed class SortMethod : ASortMethod
  111.     {
  112.         public static readonly ID Bibliography = 3;
  113.  
  114.         private static readonly ID ByDateAddedEnum = 4;
  115.         public static class ByDateAdded
  116.         {
  117.             public static readonly ID MostRecentFirst = ByDateAddedEnum[0];
  118.             public static readonly ID MostRecentLast = ByDateAddedEnum[1];
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement