Example #1
0
        /// <summary>
        ///     获取显示名
        /// </summary>
        /// <param name="customAttributeProvider"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static string GetDisplayName(this ICustomAttributeProvider customAttributeProvider, bool inherit = false)
        {
            var    displayAttribute = customAttributeProvider.GetAttribute <DisplayAttribute>();
            string displayName;

            if (displayAttribute != null)
            {
                displayName = displayAttribute.Name;
            }
            else
            {
                displayName = customAttributeProvider.GetAttribute <DisplayNameAttribute>()?.DisplayName;
            }
            return(displayName);
        }
Example #2
0
        public static string GetDisplayName(this ICustomAttributeProvider customAttributeProvider)
        {
            var displayAttribute = customAttributeProvider.GetAttribute <DisplayAttribute>();
            var displayName      = displayAttribute != null ? displayAttribute.Name : customAttributeProvider.GetAttribute <DisplayNameAttribute>()?.DisplayName;

            return(displayName);
        }
Example #3
0
        public static bool HasAttribute <T>(this ICustomAttributeProvider type, bool inherit = true) where T : class
        {
            if (type == null)
            {
                return(false);
            }

            return(type.GetAttribute <T>(inherit) != null);
        }
        public static string GetDescription(this ICustomAttributeProvider source)
        {
            if (source == null)
            {
                return(null);
            }

            var attr = source.GetAttribute <DescriptionAttribute>();

            return((attr != null) ? attr.Description : null);
        }
Example #5
0
        /// <summary>
        ///     获取Format
        /// </summary>
        /// <param name="customAttributeProvider"></param>
        /// <returns></returns>
        public static string GetDisplayFormat(this ICustomAttributeProvider customAttributeProvider)
        {
            var    formatAttribute = customAttributeProvider.GetAttribute <DisplayFormatAttribute>();
            string displayFormat   = string.Empty;

            if (formatAttribute != null)
            {
                displayFormat = formatAttribute.DataFormatString;
            }
            return(displayFormat);
        }
Example #6
0
        /// <summary>
        ///     获取类型描述
        /// </summary>
        /// <param name="customAttributeProvider"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static string GetDescription(this ICustomAttributeProvider customAttributeProvider, bool inherit = false)
        {
            var des          = string.Empty;
            var desAttribute = customAttributeProvider.GetAttribute <DescriptionAttribute>();

            if (desAttribute != null)
            {
                des = desAttribute.Description;
            }
            return(des);
        }
        public static T GetRequiredAttribute <T>(this ICustomAttributeProvider This, bool inherit = true)
        {
            var attribute = This.GetAttribute <T>(inherit);

            if (attribute == null)
            {
                throw new InvalidOperationException($"Missing required attribute of type {typeof(T).Name} on {This}");
            }

            return(attribute);
        }
Example #8
0
        /// <summary>
        /// 获取字典分组名称
        /// </summary>
        /// <param name="customAttributeProvider"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static string GetGroupName(this ICustomAttributeProvider customAttributeProvider, bool inherit = false)
        {
            string displayName      = null;
            var    displayAttribute = customAttributeProvider.GetAttribute <DisplayAttribute>();

            if (displayAttribute != null)
            {
                displayName = displayAttribute.GroupName;
            }

            return(displayName);
        }
Example #9
0
        private static AllowableValues getAllowableValues(ICustomAttributeProvider propertyInfo)
        {
            var allowableValues = propertyInfo.GetAttribute <AllowableValuesAttribute>();

            if (allowableValues == null)
            {
                return(null);
            }

            return(new AllowableValues {
                valueType = "LIST", values = allowableValues.AllowableValues
            });
        }
Example #10
0
        public static TValue GetAttributeValue <TAttribute, TValue>(
            this ICustomAttributeProvider attributeProvider,
            Func <TAttribute, TValue> accessor,
            TValue defaultValue = default(TValue)) where TAttribute : class
        {
            TAttribute attribute = attributeProvider.GetAttribute <TAttribute>();

            if (attribute != null)
            {
                return(accessor(attribute));
            }

            return(defaultValue);
        }
        private static bool ConsumeAttribute(ICustomAttributeProvider attributeProvider)
        {
            const string attributeName = MakeWeakAttributeName;

            var attribute = attributeProvider.GetAttribute(attributeName);

            if (attribute == null)
            {
                return(false);
            }

            attributeProvider.CustomAttributes.Remove(attribute);
            return(true);
        }
Example #12
0
        public static TAttribute AssertGetAttribute <TAttribute>(this ICustomAttributeProvider info) where TAttribute : Attribute
        {
            var attribute = info.GetAttribute <TAttribute>();

            if (attribute == null)
            {
                throw new InvalidOperationException(
                          string.Format(
                              "The type or property must have the Attribute: '{0}'.",
                              typeof(TAttribute).FullName)
                          );
            }

            return(attribute);
        }
        private TypeRef GetTypeRef(Type type, ICustomAttributeProvider attributeSource, string location, MethodBase paramOwner = null)
        {
            var scalarAttr = attributeSource.GetAttribute <ScalarAttribute>();

            UnwrapClrType(type, attributeSource, out var baseType, out var kinds, paramOwner);

            TypeDefBase typeDef;

            if (scalarAttr != null)
            {
                typeDef = _model.GetScalarTypeDef(scalarAttr.ScalarName);
                if (type == null)
                {
                    AddError($"{location}: scalar type {scalarAttr.ScalarName} is not defined. ");
                    return(null);
                }
            }
            else if (_model.TypesByEntityType.TryGetValue(baseType, out var mappedTypeDef))
            {
                typeDef = mappedTypeDef;
            }
            else if (!_model.TypesByClrType.TryGetValue(baseType, out typeDef))
            {
                AddError($"{location}: type {baseType} is not registered. ");
                return(null);
            }

            // add typeDef kind to kinds list and find/create type ref
            var allKinds = new List <TypeKind>();

            allKinds.Add(typeDef.Kind);

            // Flags enums are represented by enum arrays
            if (typeDef.IsEnumFlagArray())
            {
                allKinds.Add(TypeKind.NonNull);
                allKinds.Add(TypeKind.List);
            }

            allKinds.AddRange(kinds);
            var typeRef = typeDef.GetCreateTypeRef(allKinds);

            return(typeRef);
        }
Example #14
0
        private static bool ConsumeLazyAttribute(ICustomAttributeProvider attributeProvider, out int mode)
        {
            mode = default;

            const string attributeName = "Lazy.LazyAttribute";

            var attribute = attributeProvider.GetAttribute(attributeName);

            if (attribute == null)
            {
                return(false);
            }

            mode = attribute.GetValueTypeConstructorArgument <int>() ?? default;

            attributeProvider.CustomAttributes.Remove(attribute);

            return(true);
        }
Example #15
0
        /// <summary>
        /// Gets the default value.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static object GetDefaultValue(this ICustomAttributeProvider provider)
        {
            var defaultValueAttribute = provider.GetAttribute <DefaultValueAttribute>();

            if (defaultValueAttribute != null)
            {
                return(defaultValueAttribute.Value);
            }

            Type type = provider.GetMemberType();

            if (type != null)
            {
                defaultValueAttribute = type.GetAttribute <DefaultValueAttribute>();
                if (defaultValueAttribute != null)
                {
                    return(defaultValueAttribute.Value);
                }
            }
            return(null);
        }
Example #16
0
        public static Object GetAttributeValue <TAttribute>(this ICustomAttributeProvider member, string valueName) where TAttribute : Attribute
        {
            var attr = member.GetAttribute <TAttribute>();

            if (attr != null)
            {
                var property = attr.GetType().GetProperty(valueName);
                if (property == null)
                {
                    return(null);
                }

                // TODO: Some properties throw an exception here. DisplayAttribute.Order. Not sure why.
                try {
                    return(property.GetValue(attr, null));
                }
                catch (Exception)
                {
                    return(null);
                }
            }
            return(null);
        }
Example #17
0
        public static string GetDisplayName(this ICustomAttributeProvider provider)
        {
            if (provider.HasAttribute <DisplayNameAttribute>())
            {
                return(provider.GetAttribute <DisplayNameAttribute>().DisplayName);
            }
            else
            {
                string     name = provider.ToString().Prettify();
                MemberInfo mi   = provider as MemberInfo;
                if (mi != null)
                {
                    name = mi.Name.Prettify();
                }

                string[] arr = name.Split(' ');
                if ((arr.Length > 1) && (arr[arr.Length - 1].ToLower() == "id"))
                {
                    arr[arr.Length - 1] = "";
                }
                return(arr.Delimit(" ", "").TrimEnd());
            }
        }
Example #18
0
        /// <summary>
        /// Gets the converter.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static TypeConverter GetConverter(this ICustomAttributeProvider provider)
        {
            var converterAttribute = provider.GetAttribute <TypeConverterAttribute>();

            try
            {
                if (converterAttribute != null)
                {
                    Type t = Type.GetType(converterAttribute.ConverterTypeName, true, true);
                    return((TypeConverter)Activator.CreateInstance(t, true));
                }
            }
            catch
            {
                Type type = provider.GetMemberType();

                if (type != null)
                {
                    return(TypeDescriptor.GetConverter(type));
                }
            }
            return(null);
        }
Example #19
0
        private static AllowableValues getAllowableValues(ICustomAttributeProvider propertyInfo)
        {
            var allowableValues = propertyInfo.GetAttribute<AllowableValuesAttribute>();

            if(allowableValues == null)
                return null;

            return new AllowableValues {valueType = "LIST", values = allowableValues.AllowableValues};
        }
Example #20
0
 public static T GetAttribute <T>(this ICustomAttributeProvider attributeProvider)
     where T : Attribute
 {
     return(attributeProvider.GetAttribute <T>(false));
 }
Example #21
0
 public static bool HasAttribute <TAttr>(this ICustomAttributeProvider provider) where TAttr : Attribute
 {
     return(provider.GetAttribute <TAttr>() != null);
 }
Example #22
0
 public static bool HasAttribute(this ICustomAttributeProvider @this, string fullAttrName)
 => @this.GetAttribute(fullAttrName) != null;
Example #23
0
 /// <summary>
 /// Gets the first attribute. Attributes that inherits from typeof(TAttribute) are also retreived (matchExactType is false by default)
 /// </summary>
 /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
 /// <param name="provider">The provider.</param>
 /// <returns></returns>
 public static TAttribute GetAttribute <TAttribute>(this ICustomAttributeProvider provider) where TAttribute : Attribute
 {
     return(provider.GetAttribute <TAttribute>(false));
 }
 public virtual IDependencyAttribute GetAttribute(ICustomAttributeProvider p, Type type, bool inherit = false) =>
 p.GetAttribute <DependencyAttribute>(type);
Example #25
0
        /// <summary>
        /// 获取成员元数据的Description特性描述信息
        /// </summary>
        /// <param name="memberInfo">成员元数据对象</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>存在返回描述信息,否则返回为空</returns>
        public static string GetDescription(this ICustomAttributeProvider memberInfo, bool inherit = false)
        {
            DescriptionAttribute desc = memberInfo.GetAttribute <DescriptionAttribute>(inherit);

            return(desc == null ? "无描述信息" : desc.Description);
        }
Example #26
0
 /// <summary>
 /// Returns a boolean value indicating whether a given attribute T exists on an attribute provider.
 /// </summary>
 /// <typeparam name="T">The type of the attribute to look for.</typeparam>
 /// <param name="instance">The target type.</param>
 /// <returns>True if the attribute exists on the type, false otherwise.</returns>
 public static bool HasAttribute <T>(this ICustomAttributeProvider instance) where T : Attribute
 {
     return(instance.GetAttribute <T>() != null);
 }
Example #27
0
 public static bool HasAttribute(this ICustomAttributeProvider provider, string attributeName)
 {
     return(provider.GetAttribute(attributeName) != null);
 }
 public virtual IInjectionAttribute GetAttribute(ICustomAttributeProvider p, Type type, bool inherit = false) =>
 p.GetAttribute <InjectionAttribute>(type);
Example #29
0
		/// <summary>
		/// Translates the type.
		/// </summary>
		/// <param name="fieldInfo">The field info.</param>
		/// <param name="fieldType">Type of the field.</param>
		/// <returns></returns>
		public static string ToCppTypename(ICustomAttributeProvider fieldInfo, Type fieldType)
		{
			string translatedType;

			if (fieldType == typeof(string)) {
				return fieldInfo.HasAttribute<MarshalAsAttribute>() &&
					   fieldInfo.GetAttribute<MarshalAsAttribute>(false).Value == UnmanagedType.LPStr
						? "_string"
						: "_wstring";
			}

			if (TranslatedTypes.TryGetValue(fieldType, out translatedType))
				return translatedType;

			if (fieldType.IsByRef) {
				string pointedTypename = fieldType.FullName.Substring(0, fieldType.FullName.Length - 1);
				Type pointedType = fieldType.Assembly.GetType(pointedTypename);

				return ToCppTypename(fieldInfo, pointedType) + "*";
			}

			if (fieldType.IsArray) {
				return ToCppTypename(fieldInfo, fieldType.GetElementType()) + "*";
			}

			if (fieldType.IsPointer) {
				string pointedTypename = fieldType.FullName.Substring(0, fieldType.FullName.Length - 1);
				Type pointedType = fieldType.Assembly.GetType(pointedTypename);

				return ToCppTypename(fieldInfo, pointedType) + "*";
			}

			if (fieldType.IsEnum)
				return GetCppEnumName(fieldType);

			if (fieldType.HasICppInterface())
				return ToCppTypename(fieldInfo, typeof(Handle));

			return GetCppTypename(fieldType);
		}
 internal static bool TryGetAttribute <TAttribute>(this ICustomAttributeProvider provider, bool inherit, out TAttribute attribute)
     where TAttribute : Attribute
 {
     attribute = provider.GetAttribute <TAttribute>(inherit);
     return(attribute != null);
 }
Example #31
0
 /// <summary>
 /// Determines if the target has the specified attribute
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="provider"></param>
 /// <returns></returns>
 public static bool HasAttribute <T>(this ICustomAttributeProvider provider)
     where T : Attribute
 {
     return(provider.GetAttribute <T>().Any());
 }
        /// <summary>
        /// Determines whether the given type or member is browsable
        /// </summary>
        /// <param name="source">The custom attribute provider</param>
        /// <returns>True, if the type or member is browsable; otherwise, false</returns>
        public static bool IsBrowsable(this ICustomAttributeProvider source)
        {
            var browsableAttribute = source.GetAttribute <BrowsableAttribute>();

            return(browsableAttribute?.Browsable ?? true);
        }