コード例 #1
0
        /// <summary>
        /// Returns all information about the enum including; Name, Value and Detail.
        /// </summary>
        /// <typeparam name="T">Type of the enum.</typeparam>
        /// <returns></returns>
        public static List <Enumeration> GetEnumerations <T>()
        {
            List <Enumeration> enumerationList = new List <Enumeration>();

            foreach (FieldInfo field in typeof(T).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
            {
                foreach (Attribute attrib in field.GetCustomAttributes(true))
                {
                    Enumeration enumeration = new Enumeration();
                    enumeration.Name = field.GetValue(null).ToString();

                    Type underlyingType = Enum.GetUnderlyingType(typeof(T));
                    enumeration.Value = ConvertionHelper.GetConvertedValue((Enum.Parse(typeof(T), field.GetValue(null).ToString())), underlyingType).ToString();

                    Detail detail = (Detail)attrib;
                    enumeration.Detail = detail.Value;

                    enumerationList.Add(enumeration);
                }
            }

            return(enumerationList);
        }
コード例 #2
0
        public override T GetById(object id)
        {
            try
            {
                System.Data.Entity.Core.Metadata.Edm.EdmMember key = CurrentObjectSet.EntitySet.ElementType.KeyMembers.SingleOrDefault();

                if (key != null)
                {
                    CurrentContext.MetadataWorkspace.LoadFromAssembly(typeof(T).Assembly);

                    System.Data.Entity.Core.EntityKey e = new System.Data.Entity.Core.EntityKey(this.CurrentContext.DefaultContainerName + "." + typeof(T).Name, key.Name, ConvertionHelper.GetConvertedValue(id, ((System.Data.Entity.Core.Metadata.Edm.PrimitiveType)(key.TypeUsage.EdmType)).ClrEquivalentType));

                    return((T)this.CurrentContext.GetObjectByKey(e));
                }
                else
                {
                    throw new Exception("This object doesn't have a key specified!");
                }
            }
            catch (ObjectNotFoundException)
            {
                return(null);
            }
            catch
            {
                throw;
            }
        }