Example #1
0
        /// <summary>
        /// 【Extends】获取枚举类型 <typeparamref name="T"/> 的第一个特性类型为 <paramref name="attributeType"/> 的特性 <see href="Attribute"/> 。
        /// </summary>
        /// <param name="provider">An enumeration value on which to search for the attribute.</param>
        /// <param name="attributeType">The attribute type to search for.</param>
        /// <returns>The first attribute found on the source.</returns>
        public static Attribute Attribute(this Enum provider, Type attributeType)
        {
            Type       type = provider.GetType();
            MemberInfo info = type.Member(provider.ToString(), Flags.StaticAnyVisibility | Flags.DeclaredOnly);

            return(info.Attribute(attributeType));
        }
Example #2
0
        static InjectableInfo CreateForMember(MemberInfo memInfo, Type enclosingType)
        {
            var injectAttr = memInfo.Attribute<InjectAttribute>();

            var info = new InjectableInfo()
            {
                Optional = memInfo.HasAttribute(typeof(InjectOptionalAttribute)),
                Identifier = (injectAttr == null ? null : injectAttr.Identifier),
                SourceName = memInfo.Name,
                EnclosingType = enclosingType,
            };

            if (memInfo is FieldInfo)
            {
                var fieldInfo = (FieldInfo)memInfo;
                info.Setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
                info.ContractType = fieldInfo.FieldType;
            }
            else
            {
                Assert.That(memInfo is PropertyInfo);
                var propInfo = (PropertyInfo)memInfo;
                info.Setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
                info.ContractType = propInfo.PropertyType;
            }

            return info;
        }