Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomSortDictionary<T> : Dictionary<ID, Comparison<T>>
- {
- #region Fields
- private Type sortMethod;
- private static BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
- #endregion
- #region Properties
- public Type SortMethod { get { return sortMethod; } }
- #endregion
- #region Constructors
- /// <summary>
- /// Creates a new CustomSortDictionary.
- /// </summary>
- /// <param name="sortMethod">The SortMethod class to use.</param>
- public CustomSortDictionary(Type sortMethod) : base()
- {
- this.sortMethod = sortMethod;
- MethodInfo m = SortMethod.GetMethod("ConstructCustomSortMethods", bindingFlags);
- m.Invoke(null, null);
- }
- #endregion
- #region Methods
- /// <summary>
- /// Adds a custom sort method to the dictionary and the CitationCollection class.
- /// </summary>
- /// <param name="customSortMethod">The comparison by which to sort in the custom sort.</param>
- /// <param name="name">The name that will be used to refer to the custom sort in the CitationCollection class.</param>
- public void Add(ref string name, Comparison<T> customSortMethod)
- {
- ID key = ((IDCollection)SortMethod.GetNestedType("CustomSort", bindingFlags).GetField("Methods").GetValue(null)).Add(ref name);
- this.Add(key, customSortMethod);
- }
- #endregion
- }
- /// <summary>
- /// Abstract class for SortMethod classes.
- /// </summary>
- /// <remarks>Added IDs must start at the value 3.</remarks>
- public abstract class ASortMethod
- {
- public static readonly ID Manual = 1;
- private static readonly ID CustomSortEnum = 2;
- /// <summary>
- /// Used for custom sorting methods.
- /// </summary>
- public static class CustomSort
- {
- public static readonly ID None = CustomSortEnum[0];
- /// <summary>
- /// An IDCollection containing the custom sorting methods that correspond to CitationCollection.CustomSorts.
- /// </summary>
- public static IDCollection Methods;
- }
- /// <summary>
- /// If SortMethod.CustomSort.Methods is not initialized, it is initialized.
- /// </summary>
- public static void ConstructCustomSortMethods()
- {
- if (CustomSort.Methods == null)
- {
- CustomSort.Methods = new IDCollection(CustomSortEnum, typeof(CustomSort));
- }
- }
- /// <summary>
- /// Determines whether the given ID exists in SortMethod.
- /// </summary>
- /// <param name="method">The ID to check for.</param>
- /// <returns>Whether or not it exists in either the class or its CustomSort methods.</returns>
- public static bool Exists(ID method)
- {
- List<FieldInfo> fields = (from FieldInfo f in typeof(ASortMethod).GetFields()
- where f.FieldType == typeof(ID) &&
- (ID)f.GetValue(null) == method
- select f).ToList<FieldInfo>();
- return fields.Count > 0;
- }
- /// <summary>
- /// Determines whether the ID with the given name exists in SortMethod.
- /// </summary>
- /// <param name="methodName">The name of the ID.</param>
- /// <returns>Whether or not it exists in either the class or its CustomSort methods.</returns>
- public static bool Exists(string methodName)
- {
- List<FieldInfo> fields = (from FieldInfo f in typeof(ASortMethod).GetFields()
- where f.FieldType == typeof(ID) &&
- f.Name == methodName
- select f).ToList<FieldInfo>();
- return fields.Count > 0;
- }
- }
- class CitationCollection : List<Citation>
- {
- /// <summary>
- /// The methods by which the CitationCollection may be sorted.
- /// </summary>
- public sealed class SortMethod : ASortMethod
- {
- public static readonly ID Bibliography = 3;
- private static readonly ID ByDateAddedEnum = 4;
- public static class ByDateAdded
- {
- public static readonly ID MostRecentFirst = ByDateAddedEnum[0];
- public static readonly ID MostRecentLast = ByDateAddedEnum[1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement