예제 #1
0
        /// <summary>
        /// Gets the associated EnumValue class, or null if there is no associated class.
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        private static Type GetEnumValueClassForEnumType(Type enumType)
        {
            EnumValueClassAttribute attr = CollectionUtils.FirstElement <EnumValueClassAttribute>(
                enumType.GetCustomAttributes(typeof(EnumValueClassAttribute), false));

            return(attr == null ? null : attr.EnumValueClass);
        }
예제 #2
0
        /// <summary>
        /// Converts a C# enum value to a <see cref="EnumValueInfo"/> object.
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="code"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static EnumValueInfo GetEnumValueInfo <TEnum>(TEnum code, IPersistenceContext context)
            where TEnum : struct
        {
            EnumValueClassAttribute attr = CollectionUtils.FirstElement <EnumValueClassAttribute>(
                typeof(TEnum).GetCustomAttributes(typeof(EnumValueClassAttribute), false));

            if (attr == null)
            {
                throw new ArgumentException(string.Format("{0} is not marked with the EnumValueClassAttribute", typeof(TEnum).FullName));
            }

            EnumValue enumValue = context.GetBroker <IEnumBroker>().Find(attr.EnumValueClass, code.ToString());

            return(GetEnumValueInfo(enumValue));
        }
예제 #3
0
 public EnumMetadataReader()
 {
     _mapClassToEnum = new Dictionary <Type, Type>();
     foreach (PluginInfo plugin in Platform.PluginManager.Plugins)
     {
         foreach (Type type in plugin.Assembly.GetTypes())
         {
             if (type.IsEnum)
             {
                 EnumValueClassAttribute attr = CollectionUtils.FirstElement <EnumValueClassAttribute>(
                     type.GetCustomAttributes(typeof(EnumValueClassAttribute), false));
                 if (attr != null)
                 {
                     _mapClassToEnum.Add(attr.EnumValueClass, type);
                 }
             }
         }
     }
 }