Example #1
0
        public object ConvertFromString(IOption option, ICustomAttributeProvider attributes, Type targetType, string text)
        {
            TypeConverter converter;
            var           collectionConverter = attributes.GetCustomAttribute <CollectionTypeConverterAttribute>();

            if (collectionConverter != null)
            {
                converter = (TypeConverter)Activator.CreateInstance(Type.GetType(collectionConverter.ConverterTypeName));
            }
            else
            {
                var typeConverterAttribute = attributes.GetCustomAttribute <TypeConverterAttribute>();
                if (typeConverterAttribute != null)
                {
                    converter = (TypeConverter)Activator.CreateInstance(Type.GetType(typeConverterAttribute.ConverterTypeName));
                }
                else
                {
                    converter = TypeDescriptor.GetConverter(targetType);
                }
            }

            if (converter == null)
            {
                throw new InvalidOperationException($"Can not get a TypeConverter for \"{targetType}\".");
            }

            return(converter.ConvertFromString(null, _cultureInfo, text));
        }
Example #2
0
        /// <inheritdoc />
        public EntryUnitType GetUnitTypeByAttributes(ICustomAttributeProvider property)
        {
            var unitType = EntryUnitType.None;

            var passwordAttr = property.GetCustomAttribute <PasswordAttribute>();

            if (passwordAttr != null)
            {
                unitType = EntryUnitType.Password;
            }

            var fileAttr = property.GetCustomAttribute <FileSystemPathAttribute>();

            if (fileAttr != null)
            {
                switch (fileAttr.Type)
                {
                case FileSystemPathType.File:
                    unitType = EntryUnitType.File;
                    break;

                case FileSystemPathType.Directory:
                    unitType = EntryUnitType.Directory;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(unitType);
        }
Example #3
0
 /// <summary>
 /// Returns a supported <see cref="IOption"/> Attribute from the given AttributeProvider, or null if no valid supported option has been found.
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="parentIdentifier"></param>
 /// <param name="attributeProvider"></param>
 /// <exception cref="AmbiguousMatchException">Gets thrown if an option name conflicts with help argument name.</exception>
 /// <returns></returns>
 public static IOption?GetOption(Configuration configuration, string parentIdentifier, ICustomAttributeProvider attributeProvider)
 {
     return(Validate(configuration, parentIdentifier,
                     attributeProvider.GetCustomAttribute <NamedOptionAttribute>() ??
                     attributeProvider.GetCustomAttribute <PositionalOptionAttribute>() ??
                     attributeProvider.GetCustomAttribute <NamedCollectionOptionAttribute>() ??
                     (IOption?)attributeProvider.GetCustomAttribute <FlagOptionAttribute>()
                     ));
 }
        /// <summary>
        /// Tries to get attributes defining display names on this attribute provider.
        /// If no attribute was found, null will be the result.
        /// The chain follows: <see cref="DisplayAttribute"/>, <see cref="ClassDisplayAttribute"/> and <see cref="DisplayNameAttribute"/>
        /// </summary>
        /// <param name="attributeProvider">Provider of the attributes</param>
        /// <returns>The value of the display name or <c>null</c>.</returns>
        public static string GetDisplayName(this ICustomAttributeProvider attributeProvider)
        {
            var name = attributeProvider.GetCustomAttribute <DisplayAttribute>(false)?.GetName();

            if (string.IsNullOrEmpty(name))
            {
                name = attributeProvider.GetCustomAttribute <ClassDisplayAttribute>(false)?.GetName();
            }

            if (string.IsNullOrEmpty(name))
            {
                name = attributeProvider.GetCustomAttribute <DisplayNameAttribute>(false)?.DisplayName;
            }

            return(name);
        }
Example #5
0
        public static ValueSerializer GetSerializer(ICustomAttributeProvider attributeProvider, TypeReference fieldType)
        {
            var attribute = attributeProvider.GetCustomAttribute <BitCountAttribute>();

            if (attribute == null)
            {
                return(default);
Example #6
0
        internal static XamlValueConverter <ValueSerializer> LookupValueSerializer(XamlType targetType, ICustomAttributeProvider provider)
        {
            if (provider == null)
            {
                return(null);
            }

#if NET46
            var a = provider.GetCustomAttribute <ValueSerializerAttribute> (true);
            if (a != null)
            {
                return(new XamlValueConverter <ValueSerializer> (a.ValueSerializerType ?? Type.GetType(a.ValueSerializerTypeName), targetType));
            }

            if (targetType.BaseType != null)
            {
                var ret = targetType.BaseType.LookupValueSerializer();
                if (ret != null)
                {
                    return(ret);
                }
            }

            if (targetType.UnderlyingType == typeof(string))
            {
                if (string_value_serializer == null)
                {
                    string_value_serializer = new XamlValueConverter <ValueSerializer> (typeof(StringValueSerializer), targetType);
                }
                return(string_value_serializer);
            }
#endif

            return(null);
        }
Example #7
0
        /// <see cref="T:Moryx.Serialization.ICustomSerialization"/>
        public override object ConvertValue(Type memberType, ICustomAttributeProvider attributeProvider, Entry mappedEntry, object currentValue)
        {
            var value = mappedEntry.Value;

            var att = attributeProvider.GetCustomAttribute <PossibleValuesAttribute>();

            if (att == null || !att.OverridesConversion || value.Type == EntryValueType.Collection)
            {
                return(base.ConvertValue(memberType, attributeProvider, mappedEntry, currentValue));
            }

            // If old and current type are identical, keep the object
            if (value.Type == EntryValueType.Class && currentValue != null && currentValue.GetType().Name == value.Current)
            {
                return(currentValue);
            }

            var instance = att.Parse(Container, mappedEntry.Value.Current);

            if (mappedEntry.Value.Type == EntryValueType.Class)
            {
                EmptyPropertyProvider.FillEmpty(instance);
            }

            return(instance);
        }
        /// <summary>
        /// Tries to get attributes defining a description on this attribute provider.
        /// If no attribute was found, null will be the result.
        /// The chain follows: <see cref="DisplayAttribute"/>, <see cref="ClassDisplayAttribute"/> and <see cref="DescriptionAttribute"/>
        /// </summary>
        /// <param name="attributeProvider">Provider of the attributes</param>
        /// <returns>The value of the description or <c>null</c>.</returns>
        public static string GetDescription(this ICustomAttributeProvider attributeProvider)
        {
            var description = attributeProvider.GetCustomAttribute <DisplayAttribute>(false)?.GetDescription();

            if (string.IsNullOrEmpty(description))
            {
                description = attributeProvider.GetCustomAttribute <ClassDisplayAttribute>(false)?.GetDescription();
            }

            if (string.IsNullOrEmpty(description))
            {
                description = attributeProvider.GetCustomAttribute <DescriptionAttribute>(false)?.Description;
            }

            return(description);
        }
Example #9
0
        public ValueSerializer GetSerializer(ModuleDefinition module, TypeDefinition holder, ICustomAttributeProvider attributeProvider, TypeReference fieldType, string fieldName)
        {
            var attribute = attributeProvider.GetCustomAttribute <TAttribute>();

            if (attribute == null)
            {
                return(default);
        public void GetCustomAttributeFailsForDuplicateAttribtueApplication()
        {
            ICustomAttributeProvider cap = typeof(Test);

            Assert.Throws <AmbiguousMatchException>(() =>
                                                    cap.GetCustomAttribute <TestAttribute>(true));
        }
Example #11
0
        public static ValueSerializer GetSerializer(ICustomAttributeProvider attributeProvider, TypeReference fieldType)
        {
            CustomAttribute attribute = attributeProvider.GetCustomAttribute <VarIntBlocksAttribute>();

            if (attribute == null)
            {
                return(default);
Example #12
0
 public static bool TryGetCustomAttribute <TAttribute>(this ICustomAttributeProvider provider,
                                                       out TAttribute result, bool inherit = false)
     where TAttribute : Attribute
 {
     result = provider.GetCustomAttribute <TAttribute>(inherit);
     return(result.IsNotNull());
 }
        public void GetCustomAttributeReturnsRequestedAttributeWhenAttributesApplied()
        {
            ICustomAttributeProvider cap = typeof(Test);
            var attribute = cap.GetCustomAttribute <DescriptionAttribute>(true);

            Assert.NotNull(attribute);
            Assert.Equal("foo", attribute.Description);
        }
        public static TProp GetCustomAttributeProperty <TAttr, TProp>(this ICustomAttributeProvider provider, Func <TAttr, TProp> selector, TProp defaultValue = default(TProp), bool inherit = false)
        {
            TAttr customAttribute = provider.GetCustomAttribute <TAttr>(inherit);

            if (customAttribute == null)
            {
                return(defaultValue);
            }
            return(selector(customAttribute));
        }
Example #15
0
        public override string[] PossibleValues(Type memberType, ICustomAttributeProvider attributeProvider)
        {
            var possibleAtt = attributeProvider.GetCustomAttribute <PossibleValuesAttribute>();

            if (possibleAtt != null)
            {
                return(possibleAtt.GetValues(null).ToArray()); // TODO: Replace with proper possible values handling
            }
            return(base.PossibleValues(memberType, attributeProvider));
        }
        /// <see cref="T:Moryx.Serialization.ICustomSerialization"/>
        public override object CreateInstance(Type memberType, ICustomAttributeProvider attributeProvider, Entry encoded)
        {
            var possibleValuesAtt = attributeProvider.GetCustomAttribute <PossibleValuesAttribute>();
            var instance          = possibleValuesAtt != null
                ? possibleValuesAtt.Parse(Container, encoded.Value.Current)
                : base.CreateInstance(memberType, encoded);

            ConfigManager.FillEmpty(instance);
            return(instance);
        }
        public static string GetTypeConverterName(this ICustomAttributeProvider member, bool inherit)
        {
            if (member == null)
            {
                return(null);
            }
            var typeConverterName = member.GetCustomAttribute <TypeConverterAttribute>(inherit)?.ConverterTypeName;

            return(typeConverterName);
        }
        public static bool GetBrowsable(this ICustomAttributeProvider customAttributeProvider)
        {
            var attribute = customAttributeProvider.GetCustomAttribute <BrowsableAttribute>();

            if (attribute == null)
            {
                return(true);
            }
            return(attribute.Browsable);
        }
        public static string GetDisplayName(this ICustomAttributeProvider customAttributeProvider)
        {
            var attribute = customAttributeProvider.GetCustomAttribute <DisplayNameAttribute>();

            if (attribute == null)
            {
                return(string.Empty);
            }
            return(attribute.DisplayName);
        }
Example #20
0
        /// <summary>
        /// GetCustomAttribute
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="provider"></param>
        /// <returns></returns>
        public static T GetCustomAttribute <T>(this ICustomAttributeProvider provider)
            where T : Attribute
        {
            if (provider == null)
            {
                return(null);
            }

            return((T)provider.GetCustomAttribute(typeof(T)));
        }
Example #21
0
        public static string GetTypeConverterName(this ICustomAttributeProvider member, bool inherit)
        {
            if (member == null)
            {
                return(null);
            }
            var typeConverterName = member.GetCustomAttribute <TypeConverterAttribute>(inherit)?.ConverterTypeName;

#if !HAS_TYPE_CONVERTER
            if (typeConverterName == null && s_typeConverterAttribute != null)
            {
                var systemTypeConverterInfo = member.GetCustomAttribute(s_typeConverterAttribute, inherit);
                if (systemTypeConverterInfo != null)
                {
                    typeConverterName = s_typeConverterTypeNameProperty.GetValue(systemTypeConverterInfo) as string;
                }
            }
#endif
            return(typeConverterName);
        }
Example #22
0
        internal static BuilderParameterInfo FromAttributeProvider(ICustomAttributeProvider provider, Type defaultType, string defaultName = null)
        {
            var    valueAttr       = provider.GetCustomAttribute <InjectValueAttribute>(true);
            var    defaultAttr     = provider.GetCustomAttribute <DefaultValueAttribute>(true);
            Type   type            = null;
            string name            = null;
            bool   hasDefaultValue = false;
            object defaultValue    = null;

            if (valueAttr != null)
            {
                type = valueAttr.Type;
                name = valueAttr.Name;
            }
            if (defaultAttr != null)
            {
                defaultValue    = defaultAttr.DefaultValue;
                hasDefaultValue = true;
            }
            if (type == null)
            {
                type = defaultType;
            }
            if (string.IsNullOrEmpty(name))
            {
                name = defaultName;
            }
            BuilderParameterInfo p;

            if (hasDefaultValue)
            {
                p = BuilderParameterInfo.MakeDefault(type, name, defaultValue);
            }
            else
            {
                p = new BuilderParameterInfo(type, name);
            }

            return(p);
        }
 internal static SemanticVersion GetNuGetVersion(ICustomAttributeProvider assembly)
 {
     try
     {
         var assemblyInformationalVersion = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>();
         return(new SemanticVersion(assemblyInformationalVersion.InformationalVersion));
     }
     catch
     {
         // Don't let GetCustomAttributes throw.
     }
     return(null);
 }
Example #24
0
        private IParameter GetOrCreateParameter(ICustomAttributeProvider info, string aname)
        {
            var ip = Parameters.FirstOrDefault(predicate => predicate.Name == aname);

            if (ip != null)
            {
                return(ip);
            }
            var selector = info.GetCustomAttribute <IParameterSelectorAttribute>(false) ?? new ParameterAttribute();

            ip = selector.Create(aname, this, info as MemberInfo, info as ParameterInfo);
            Parameters.Add(ip);
            return(ip);
        }
        /// <see cref="T:Moryx.Serialization.ICustomSerialization"/>
        public override string[] PossibleValues(Type memberType, ICustomAttributeProvider attributeProvider)
        {
            var valuesAttribute = attributeProvider.GetCustomAttribute <PossibleValuesAttribute>();

            if (valuesAttribute == null)
            {
                return(base.PossibleValues(memberType, attributeProvider));
            }

            // Use attribute
            var values = valuesAttribute.GetValues(Container);

            return(values?.Distinct().ToArray());
        }
Example #26
0
        public static void InitializeProperty(DotvvmProperty property, ICustomAttributeProvider attributeProvider)
        {
            var propertyInfo  = property.DeclaringType.GetProperty(property.Name);
            var markupOptions = propertyInfo?.GetCustomAttribute <MarkupOptionsAttribute>()
                                ?? attributeProvider.GetCustomAttribute <MarkupOptionsAttribute>()
                                ?? new MarkupOptionsAttribute()
            {
                AllowBinding        = true,
                AllowHardCodedValue = true,
                MappingMode         = MappingMode.Attribute,
                Name = property.Name
            };

            if (string.IsNullOrEmpty(markupOptions.Name))
            {
                markupOptions.Name = property.Name;
            }

            if (property == null)
            {
                property = new DotvvmProperty();
            }
            property.PropertyInfo = propertyInfo;
            property.DataContextChangeAttributes = (propertyInfo != null ?
                                                    propertyInfo.GetCustomAttributes <DataContextChangeAttribute>(true) :
                                                    attributeProvider.GetCustomAttributes <DataContextChangeAttribute>()).ToArray();
            property.DataContextManipulationAttribute = propertyInfo != null?
                                                        propertyInfo.GetCustomAttribute <DataContextStackManipulationAttribute>(true) :
                                                            attributeProvider.GetCustomAttribute <DataContextStackManipulationAttribute>();

            if (property.DataContextManipulationAttribute != null && property.DataContextChangeAttributes.Any())
            {
                throw new ArgumentException($"{nameof(DataContextChangeAttributes)} and {nameof(DataContextManipulationAttribute)} can not be set both at property '{property.FullName}'.");
            }
            property.MarkupOptions     = markupOptions;
            property.IsBindingProperty = typeof(IBinding).IsAssignableFrom(property.PropertyType);
        }
Example #27
0
        /// <see cref="T:Moryx.Serialization.ICustomSerialization"/>
        public override string[] PossibleValues(Type memberType, ICustomAttributeProvider attributeProvider)
        {
            var valuesAttribute = attributeProvider.GetCustomAttribute <PossibleValuesAttribute>();

            // Possible values for primitive collections only apply to members
            if (valuesAttribute == null || IsPrimitiveCollection(memberType))
            {
                return(base.PossibleValues(memberType, attributeProvider));
            }

            // Use attribute
            var values = valuesAttribute.GetValues(Container);

            return(values?.Distinct().ToArray());
        }
    /// <summary>
    /// Gets a user-configured description of the given attribute provider. The description is taken from an
    /// instance of the <see cref="DescriptionAttribute"/>.
    /// </summary>
    /// <param name="attributeProvider">The attribute provider.</param>
    /// <param name="defaultDescription">The default description to use if no attribute can be found.</param>
    /// <returns>The description.</returns>
    public static string GetDescriptionOrDefault
    (
        this ICustomAttributeProvider attributeProvider,
        string?defaultDescription = null
    )
    {
        var descriptionAttribute = attributeProvider.GetCustomAttribute <DescriptionAttribute>();

        if (descriptionAttribute is null)
        {
            return(defaultDescription ?? Constants.DefaultDescription);
        }

        return(string.IsNullOrWhiteSpace(descriptionAttribute.Description)
            ? Constants.DefaultDescription
            : descriptionAttribute.Description);
    }
        public static TypeConverter GetConverter(this ICustomAttributeProvider customAttributeProvider, Type type)
        {
            var attribute = customAttributeProvider.GetCustomAttribute <TypeConverterAttribute>();

            if (attribute == null)
            {
                return(TypeDescriptor.GetConverter(type));
            }
            try
            {
                var converterType = Type.GetType(attribute.ConverterTypeName);
                return(Activator.CreateInstance(converterType) as TypeConverter);
            }
            catch
            {
                return(TypeDescriptor.GetConverter(type));
            }
        }
        /// <see cref="T:Moryx.Serialization.ICustomSerialization"/>
        public override EntryPrototype[] Prototypes(Type memberType, ICustomAttributeProvider attributeProvider)
        {
            // Create prototypes from possible values
            var possibleValuesAtt = attributeProvider.GetCustomAttribute <PossibleValuesAttribute>();

            if (possibleValuesAtt == null)
            {
                return(base.Prototypes(memberType, attributeProvider));
            }

            var list = new List <EntryPrototype>();

            foreach (var value in possibleValuesAtt.GetValues(Container))
            {
                var prototype = possibleValuesAtt.Parse(Container, value);
                EmptyPropertyProvider.FillEmpty(prototype);
                list.Add(new EntryPrototype(value, prototype));
            }
            return(list.ToArray());
        }
Example #31
0
 internal static SemanticVersion GetNuGetVersion(ICustomAttributeProvider assembly)
 {
     try
     {
         var assemblyInformationalVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
         return new SemanticVersion(assemblyInformationalVersion.InformationalVersion);
     }
     catch
     {
         // Don't let GetCustomAttributes throw.
     }
     return null;
 }