public void GetCustomAttributeT_Multiple_NoInheirit()
        {
            var attributes = CustomAttributeExtensions.GetCustomAttributes <MyAttribute_AllowMultiple_Inherited>(s_typeTestClass.GetTypeInfo(), false);

            Assert.Equal(1, attributes.Count());
        }
예제 #2
0
 public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, attributeType, inherit).ToArray();
예제 #3
0
        public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this).ToArray();                                    // inherit is meaningless for Modules

        public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, attributeType).ToArray(); // inherit is meaningless for Modules
예제 #4
0
        private void SerializeItem(object item, bool list             = false, string key = null,
                                   IEnumerable <Attribute> extraAttrs = null, object commentSource = null)
        {
            _options = new CombinedOptions(_globals)
                       .OverrideWith(CustomAttributeExtensions.GetCustomAttributes(item.GetType(), true))
                       .OverrideWith(extraAttrs);

            if (_options.Ignores.Count > 0 && IsIgnored(item, _options.Ignores))
            {
                return;
            }

            if ((_allowInline == AllowInline.Anything || (_allowInline == AllowInline.PlainValues && !list && key == null)) &&
                _options.BlankLinesBefore == 0 && !_options.Comments.Any(attr => IsCommentBefore(attr)))
            {
                // Do not start a new line
            }
            else
            {
                _writer.AppendBlankLinesBefore(_options.BlankLinesBefore);
                AppendComments(commentSource ?? item, before: true);
                _writer.StartNewLine();
            }

            if (list)
            {
                _writer.AppendSequenceItemPrefix();
                _allowInline = _options.AlwaysNested ? AllowInline.Nothing : AllowInline.Anything;
            }
            else if (key != null)
            {
                SerializeString(_options.Name ?? key, true);
                _writer.AppendKeyValueSeparator();
                _allowInline = AllowInline.PlainValues;
            }

            if (item is string)
            {
                SerializeString((string)item, false);
            }
            else if (item is bool)
            {
                SerializeBool((bool)item);
            }
            else if (item.GetType().IsPrimitive)
            {
                SerializeString(item.ToString(), false);
            }
            else
            {
                if (++_depth > _globals.MaxDepth)
                {
                    throw new YamlSerializationTooDeep();
                }

                bool nextIsList  = (item is IEnumerable) && !(item is IDictionary);
                bool needsIndent = (list || ((key != null) && !nextIsList));
                if (needsIndent)
                {
                    _writer.Indent(_options.IndentStep);
                }
                var prevOptions = _options;

                if (item is IDictionary)
                {
                    SerializeDictionary((IDictionary)item);
                }
                else if (item is IEnumerable)
                {
                    SerializeList((IEnumerable)item);
                }
                else
                {
                    SerializeObject(item);
                }

                _options = prevOptions;
                if (needsIndent)
                {
                    _writer.Undent();
                }
                --_depth;
            }

            AppendComments(commentSource ?? item, after: true);
            _writer.AppendBlankLinesAfter(_options.BlankLinesAfter);

            _allowInline = AllowInline.Nothing;
        }
예제 #5
0
        public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit: false).ToArray();                                    // Desktop compat: for events, this form of the api ignores "inherit"

        public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, attributeType, inherit: false).ToArray(); // Desktop compat: for events, this form of the api ignores "inherit"
예제 #6
0
 public static IEnumerable <Attribute> GetCustomAttributes(this Type type, Type attributeType, bool includeInherited)
 {
     return(CustomAttributeExtensions.GetCustomAttributes(type.GetTypeInfo(), attributeType, includeInherited));
 }
예제 #7
0
 public static IEnumerable <T> GetCustomAttributes <T>(this Assembly assembly) where T : Attribute => CustomAttributeExtensions.GetCustomAttributes <T>(assembly);
        public MemberInfo GetMatchingMemberInfo(IGetTypeInfoMembers getTypeInfoMembers, TypeDetails typeInfo, Type destType, Type destMemberType, string nameToSearch)
        {
            Cache.GetOrAdd(typeInfo, ti => getTypeInfoMembers.GetMemberInfos(ti).ToDictionary(mi => mi, mi => CustomAttributeExtensions.GetCustomAttributes((MemberInfo)mi, typeof(SourceToDestinationMapperAttribute), true).OfType <SourceToDestinationMapperAttribute>()));

            return(Cache[typeInfo].FirstOrDefault(kp => kp.Value.Any(_ => _.IsMatch(typeInfo, kp.Key, destType, destMemberType, nameToSearch))).Key);
        }
    static int Main(string[] args)
    {
        Assembly assembly = typeof(Class).GetTypeInfo().Assembly;

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(assembly) != null);
        Assert(((ICustomAttributeProvider)assembly).GetCustomAttributes(typeof(SingleAttribute <int>), true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(assembly) != null);
        Assert(((ICustomAttributeProvider)assembly).GetCustomAttributes(typeof(SingleAttribute <bool>), true) != null);
        Assert(CustomAttributeExtensions.IsDefined(assembly, typeof(SingleAttribute <int>)));
        Assert(((ICustomAttributeProvider)assembly).IsDefined(typeof(SingleAttribute <int>), true));
        Assert(CustomAttributeExtensions.IsDefined(assembly, typeof(SingleAttribute <bool>)));
        Assert(((ICustomAttributeProvider)assembly).IsDefined(typeof(SingleAttribute <bool>), true));

        TypeInfo programTypeInfo = typeof(Class).GetTypeInfo();

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(programTypeInfo) != null);
        Assert(((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(SingleAttribute <int>), true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(programTypeInfo) != null);
        Assert(((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(SingleAttribute <bool>), true) != null);
        Assert(CustomAttributeExtensions.IsDefined(programTypeInfo, typeof(SingleAttribute <int>)));
        Assert(((ICustomAttributeProvider)programTypeInfo).IsDefined(typeof(SingleAttribute <int>), true));
        Assert(CustomAttributeExtensions.IsDefined(programTypeInfo, typeof(SingleAttribute <bool>)));
        Assert(((ICustomAttributeProvider)programTypeInfo).IsDefined(typeof(SingleAttribute <bool>), true));

        var propertyPropertyInfo = typeof(Class).GetTypeInfo().GetProperty(nameof(Class.Property));

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(propertyPropertyInfo) != null);
        Assert(((ICustomAttributeProvider)propertyPropertyInfo).GetCustomAttributes(typeof(SingleAttribute <int>), true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(propertyPropertyInfo) != null);
        Assert(((ICustomAttributeProvider)propertyPropertyInfo).GetCustomAttributes(typeof(SingleAttribute <bool>), true) != null);
        Assert(CustomAttributeExtensions.IsDefined(propertyPropertyInfo, typeof(SingleAttribute <int>)));
        Assert(((ICustomAttributeProvider)propertyPropertyInfo).IsDefined(typeof(SingleAttribute <int>), true));
        Assert(CustomAttributeExtensions.IsDefined(propertyPropertyInfo, typeof(SingleAttribute <bool>)));
        Assert(((ICustomAttributeProvider)propertyPropertyInfo).IsDefined(typeof(SingleAttribute <bool>), true));

        var deriveTypeInfo = typeof(Class.Derive).GetTypeInfo();

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(deriveTypeInfo, false) == null);
        Assert(((ICustomAttributeProvider)deriveTypeInfo).GetCustomAttributes(typeof(SingleAttribute <int>), true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(deriveTypeInfo, false) == null);
        Assert(((ICustomAttributeProvider)deriveTypeInfo).GetCustomAttributes(typeof(SingleAttribute <bool>), true) != null);
        Assert(!CustomAttributeExtensions.IsDefined(deriveTypeInfo, typeof(SingleAttribute <int>), false));
        Assert(!CustomAttributeExtensions.IsDefined(deriveTypeInfo, typeof(SingleAttribute <bool>), false));

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(deriveTypeInfo, true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(deriveTypeInfo, true) != null);
        Assert(CustomAttributeExtensions.IsDefined(deriveTypeInfo, typeof(SingleAttribute <int>), true));
        Assert(((ICustomAttributeProvider)deriveTypeInfo).IsDefined(typeof(SingleAttribute <int>), true));
        Assert(CustomAttributeExtensions.IsDefined(deriveTypeInfo, typeof(SingleAttribute <bool>), true));
        Assert(((ICustomAttributeProvider)deriveTypeInfo).IsDefined(typeof(SingleAttribute <bool>), true));

        var a1 = CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, true);

        AssertAny(a1, a => a is SingleAttribute <int>);
        AssertAny(a1, a => a is SingleAttribute <bool>);
        AssertAny(a1, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(a1, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(a1, a => (a as MultiAttribute <int>)?.Value == 2);
        AssertAny(a1, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(a1, a => (a as MultiAttribute <bool>)?.Value == true, 2);
        AssertAny(a1, a => (a as MultiAttribute <bool?>)?.Value == null);

        var b1 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(true);

        AssertAny(b1, a => a is SingleAttribute <int>);
        AssertAny(b1, a => a is SingleAttribute <bool>);
        AssertAny(b1, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(b1, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(b1, a => (a as MultiAttribute <int>)?.Value == 2);
        AssertAny(b1, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(b1, a => (a as MultiAttribute <bool>)?.Value == true, 2);
        AssertAny(b1, a => (a as MultiAttribute <bool?>)?.Value == null);

        var a2 = CustomAttributeExtensions.GetCustomAttributes(deriveTypeInfo, false);

        Assert(!a2.GetEnumerator().MoveNext());

        var b2 = ((ICustomAttributeProvider)deriveTypeInfo).GetCustomAttributes(false);

        Assert(!b2.GetEnumerator().MoveNext());

        var a3 = CustomAttributeExtensions.GetCustomAttributes(deriveTypeInfo, true);

        AssertAny(a3, a => a is SingleAttribute <int>);
        AssertAny(a3, a => a is SingleAttribute <bool>);
        AssertAny(a3, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(a3, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(a3, a => (a as MultiAttribute <int>)?.Value == 2);
        AssertAny(a3, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(a3, a => (a as MultiAttribute <bool>)?.Value == true);

        var b3 = ((ICustomAttributeProvider)deriveTypeInfo).GetCustomAttributes(true);

        AssertAny(b3, a => a is SingleAttribute <int>);
        AssertAny(b3, a => a is SingleAttribute <bool>);
        AssertAny(b3, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(b3, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(b3, a => (a as MultiAttribute <int>)?.Value == 2);
        AssertAny(b3, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(b3, a => (a as MultiAttribute <bool>)?.Value == true);

        var a4 = CustomAttributeExtensions.GetCustomAttributes <SingleAttribute <int> >(programTypeInfo, true);

        AssertAny(a4, a => a is SingleAttribute <int>);

        var b4 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(SingleAttribute <int>), true);

        AssertAny(b4, a => a is SingleAttribute <int>);

        var a5 = CustomAttributeExtensions.GetCustomAttributes <SingleAttribute <bool> >(programTypeInfo);

        AssertAny(a5, a => a is SingleAttribute <bool>);

        var b5 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(SingleAttribute <bool>), true);

        AssertAny(b5, a => a is SingleAttribute <bool>);

        var a6 = CustomAttributeExtensions.GetCustomAttributes <MultiAttribute <int> >(programTypeInfo, true);

        AssertAny(a6, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(a6, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(a6, a => (a as MultiAttribute <int>)?.Value == 2);

        var b6 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <int>), true);

        AssertAny(b6, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(b6, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(b6, a => (a as MultiAttribute <int>)?.Value == 2);

        var a7 = CustomAttributeExtensions.GetCustomAttributes <MultiAttribute <bool> >(programTypeInfo, true);

        AssertAny(a7, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(a7, a => (a as MultiAttribute <bool>)?.Value == true);

        var b7 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <bool>), true);

        AssertAny(b7, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(b7, a => (a as MultiAttribute <bool>)?.Value == true);

        var a8 = CustomAttributeExtensions.GetCustomAttributes <MultiAttribute <bool?> >(programTypeInfo, true);

        AssertAny(a8, a => (a as MultiAttribute <bool?>)?.Value == null);

        var b8 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <bool?>), true);

        AssertAny(b8, a => (a as MultiAttribute <bool?>)?.Value == null);

        Assert(CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, typeof(MultiAttribute <>), false) == null);
        Assert(CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, typeof(MultiAttribute <>), true) == null);
        Assert(!((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <>), true).GetEnumerator().MoveNext());

        return(100);
    }
예제 #10
0
 private IEnumerable <PropertyInfo> Properties()
 {
     return(GetType().GetProperties().Where(p => CustomAttributeExtensions.GetCustomAttributes <HeaderNameAttribute>((MemberInfo)p).Any()));
 }
예제 #11
0
 public IWitService[] MakeServicesFromAttributes() => Enumerable.ToArray(Enumerable.Cast <IWitService>(Enumerable.Select(CustomAttributeExtensions.GetCustomAttributes <WitModelAttribute>(GetType(), true), m => new WitService(m, new HttpService()))));
예제 #12
0
 public static Attribute[] GetCustomAttributes(this Type type, Type other, bool inherit)
 {
     return(CustomAttributeExtensions.GetCustomAttributes(type.GetTypeInfo(), other, inherit).ToArray());
 }
예제 #13
0
    static int Main(string[] args)
    {
        Assembly assembly = typeof(Class).GetTypeInfo().Assembly;

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(assembly) != null);
        Assert(((ICustomAttributeProvider)assembly).GetCustomAttributes(typeof(SingleAttribute <int>), true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(assembly) != null);
        Assert(((ICustomAttributeProvider)assembly).GetCustomAttributes(typeof(SingleAttribute <bool>), true) != null);
        Assert(CustomAttributeExtensions.IsDefined(assembly, typeof(SingleAttribute <int>)));
        Assert(((ICustomAttributeProvider)assembly).IsDefined(typeof(SingleAttribute <int>), true));
        Assert(CustomAttributeExtensions.IsDefined(assembly, typeof(SingleAttribute <bool>)));
        Assert(((ICustomAttributeProvider)assembly).IsDefined(typeof(SingleAttribute <bool>), true));
        Assert(CustomAttributeExtensions.GetCustomAttributes(assembly, typeof(SingleAttribute <>)).GetEnumerator().MoveNext());
        Assert(CustomAttributeExtensions.GetCustomAttributes(assembly, typeof(SingleAttribute <>)).GetEnumerator().MoveNext());

        // Uncomment when https://github.com/dotnet/runtime/issues/66168 is resolved
        // Module module = programTypeInfo.Module;
        // AssertAny(CustomAttributeExtensions.GetCustomAttributes(module), a => a is SingleAttribute<long>);
        // Assert(CustomAttributeExtensions.GetCustomAttributes(module, typeof(SingleAttribute<long>)).GetEnumerator().MoveNext());
        // Assert(CustomAttributeExtensions.GetCustomAttributes(module, typeof(SingleAttribute<long>)).GetEnumerator().MoveNext());
        // Assert(CustomAttributeExtensions.GetCustomAttributes(module, typeof(SingleAttribute<>)).GetEnumerator().MoveNext());
        // Assert(CustomAttributeExtensions.GetCustomAttributes(module, typeof(SingleAttribute<>)).GetEnumerator().MoveNext());

        TypeInfo programTypeInfo = typeof(Class).GetTypeInfo();

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(programTypeInfo) != null);
        Assert(((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(SingleAttribute <int>), true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(programTypeInfo) != null);
        Assert(((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(SingleAttribute <bool>), true) != null);
        Assert(CustomAttributeExtensions.IsDefined(programTypeInfo, typeof(SingleAttribute <int>)));
        Assert(((ICustomAttributeProvider)programTypeInfo).IsDefined(typeof(SingleAttribute <int>), true));
        Assert(CustomAttributeExtensions.IsDefined(programTypeInfo, typeof(SingleAttribute <bool>)));
        Assert(((ICustomAttributeProvider)programTypeInfo).IsDefined(typeof(SingleAttribute <bool>), true));

        var propertyPropertyInfo = typeof(Class).GetTypeInfo().GetProperty(nameof(Class.Property));

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(propertyPropertyInfo) != null);
        Assert(((ICustomAttributeProvider)propertyPropertyInfo).GetCustomAttributes(typeof(SingleAttribute <int>), true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(propertyPropertyInfo) != null);
        Assert(((ICustomAttributeProvider)propertyPropertyInfo).GetCustomAttributes(typeof(SingleAttribute <bool>), true) != null);
        Assert(CustomAttributeExtensions.IsDefined(propertyPropertyInfo, typeof(SingleAttribute <int>)));
        Assert(((ICustomAttributeProvider)propertyPropertyInfo).IsDefined(typeof(SingleAttribute <int>), true));
        Assert(CustomAttributeExtensions.IsDefined(propertyPropertyInfo, typeof(SingleAttribute <bool>)));
        Assert(((ICustomAttributeProvider)propertyPropertyInfo).IsDefined(typeof(SingleAttribute <bool>), true));

        var deriveTypeInfo = typeof(Class.Derive).GetTypeInfo();

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(deriveTypeInfo, false) == null);
        Assert(((ICustomAttributeProvider)deriveTypeInfo).GetCustomAttributes(typeof(SingleAttribute <int>), true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(deriveTypeInfo, false) == null);
        Assert(((ICustomAttributeProvider)deriveTypeInfo).GetCustomAttributes(typeof(SingleAttribute <bool>), true) != null);
        Assert(!CustomAttributeExtensions.IsDefined(deriveTypeInfo, typeof(SingleAttribute <int>), false));
        Assert(!CustomAttributeExtensions.IsDefined(deriveTypeInfo, typeof(SingleAttribute <bool>), false));

        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <int> >(deriveTypeInfo, true) != null);
        Assert(CustomAttributeExtensions.GetCustomAttribute <SingleAttribute <bool> >(deriveTypeInfo, true) != null);
        Assert(CustomAttributeExtensions.IsDefined(deriveTypeInfo, typeof(SingleAttribute <int>), true));
        Assert(((ICustomAttributeProvider)deriveTypeInfo).IsDefined(typeof(SingleAttribute <int>), true));
        Assert(CustomAttributeExtensions.IsDefined(deriveTypeInfo, typeof(SingleAttribute <bool>), true));
        Assert(((ICustomAttributeProvider)deriveTypeInfo).IsDefined(typeof(SingleAttribute <bool>), true));

        var a1 = CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, true);

        AssertAny(a1, a => a is SingleAttribute <int>);
        AssertAny(a1, a => a is SingleAttribute <bool>);
        AssertAny(a1, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(a1, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(a1, a => (a as MultiAttribute <int>)?.Value == 2);
        AssertAny(a1, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(a1, a => (a as MultiAttribute <bool>)?.Value == true, 2);
        AssertAny(a1, a => (a as MultiAttribute <bool?>)?.Value == null);

        var b1 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(true);

        AssertAny(b1, a => a is SingleAttribute <int>);
        AssertAny(b1, a => a is SingleAttribute <bool>);
        AssertAny(b1, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(b1, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(b1, a => (a as MultiAttribute <int>)?.Value == 2);
        AssertAny(b1, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(b1, a => (a as MultiAttribute <bool>)?.Value == true, 2);
        AssertAny(b1, a => (a as MultiAttribute <bool?>)?.Value == null);

        var a2 = CustomAttributeExtensions.GetCustomAttributes(deriveTypeInfo, false);

        Assert(!a2.GetEnumerator().MoveNext());

        var b2 = ((ICustomAttributeProvider)deriveTypeInfo).GetCustomAttributes(false);

        Assert(!b2.GetEnumerator().MoveNext());

        var a3 = CustomAttributeExtensions.GetCustomAttributes(deriveTypeInfo, true);

        AssertAny(a3, a => a is SingleAttribute <int>);
        AssertAny(a3, a => a is SingleAttribute <bool>);
        AssertAny(a3, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(a3, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(a3, a => (a as MultiAttribute <int>)?.Value == 2);
        AssertAny(a3, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(a3, a => (a as MultiAttribute <bool>)?.Value == true);

        var b3 = ((ICustomAttributeProvider)deriveTypeInfo).GetCustomAttributes(true);

        AssertAny(b3, a => a is SingleAttribute <int>);
        AssertAny(b3, a => a is SingleAttribute <bool>);
        AssertAny(b3, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(b3, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(b3, a => (a as MultiAttribute <int>)?.Value == 2);
        AssertAny(b3, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(b3, a => (a as MultiAttribute <bool>)?.Value == true);

        var a4 = CustomAttributeExtensions.GetCustomAttributes <SingleAttribute <int> >(programTypeInfo, true);

        AssertAny(a4, a => a is SingleAttribute <int>);

        var b4 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(SingleAttribute <int>), true);

        AssertAny(b4, a => a is SingleAttribute <int>);

        var a5 = CustomAttributeExtensions.GetCustomAttributes <SingleAttribute <bool> >(programTypeInfo);

        AssertAny(a5, a => a is SingleAttribute <bool>);

        var b5 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(SingleAttribute <bool>), true);

        AssertAny(b5, a => a is SingleAttribute <bool>);

        var a6 = CustomAttributeExtensions.GetCustomAttributes <MultiAttribute <int> >(programTypeInfo, true);

        AssertAny(a6, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(a6, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(a6, a => (a as MultiAttribute <int>)?.Value == 2);

        var b6 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <int>), true);

        AssertAny(b6, a => (a as MultiAttribute <int>)?.Value == 0);
        AssertAny(b6, a => (a as MultiAttribute <int>)?.Value == 1);
        AssertAny(b6, a => (a as MultiAttribute <int>)?.Value == 2);

        var a7 = CustomAttributeExtensions.GetCustomAttributes <MultiAttribute <bool> >(programTypeInfo, true);

        AssertAny(a7, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(a7, a => (a as MultiAttribute <bool>)?.Value == true);

        var b7 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <bool>), true);

        AssertAny(b7, a => (a as MultiAttribute <bool>)?.Value == false);
        AssertAny(b7, a => (a as MultiAttribute <bool>)?.Value == true);

        var a8 = CustomAttributeExtensions.GetCustomAttributes <MultiAttribute <bool?> >(programTypeInfo, true);

        AssertAny(a8, a => (a as MultiAttribute <bool?>)?.Value == null);

        var b8 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <bool?>), true);

        AssertAny(b8, a => (a as MultiAttribute <bool?>)?.Value == null);

        var a9 = CustomAttributeExtensions.GetCustomAttributes <MultiAttribute <string> >(programTypeInfo, true);

        AssertAny(a9, a => (a as MultiAttribute <string>)?.Value == "Ctor");
        AssertAny(a9, a => (a as MultiAttribute <string>)?.Value == "Property");

        var b9 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <string>), true);

        AssertAny(b9, a => (a as MultiAttribute <string>)?.Value == "Ctor");
        AssertAny(b9, a => (a as MultiAttribute <string>)?.Value == "Property");

        var a10 = CustomAttributeExtensions.GetCustomAttributes <MultiAttribute <Type> >(programTypeInfo, true);

        AssertAny(a10, a => (a as MultiAttribute <Type>)?.Value == typeof(Class));
        AssertAny(a10, a => (a as MultiAttribute <Type>)?.Value == typeof(Class.Derive));

        var b10 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <Type>), true);

        AssertAny(b10, a => (a as MultiAttribute <Type>)?.Value == typeof(Class));
        AssertAny(b10, a => (a as MultiAttribute <Type>)?.Value == typeof(Class.Derive));

        Assert(CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, typeof(MultiAttribute <>), false).GetEnumerator().MoveNext());
        Assert(CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, typeof(MultiAttribute <>), true).GetEnumerator().MoveNext());
        Assert(((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute <>), true).GetEnumerator().MoveNext());

        // Test coverage for CustomAttributeData api surface
        var a1_data = CustomAttributeData.GetCustomAttributes(programTypeInfo);

        AssertAny(a1_data, a => a.AttributeType == typeof(SingleAttribute <int>));
        AssertAny(a1_data, a => a.AttributeType == typeof(SingleAttribute <bool>));

        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <int>) && a.ConstructorArguments.Count == 0 && a.NamedArguments.Count == 0);
        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <int>) && a.ConstructorArguments.Count == 1 && a.NamedArguments.Count == 0 && a.ConstructorArguments[0].ArgumentType == typeof(int) && ((int)a.ConstructorArguments[0].Value) == 1);
        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <int>) && a.ConstructorArguments.Count == 0 && a.NamedArguments.Count == 1 && a.NamedArguments[0].TypedValue.ArgumentType == typeof(int) && ((int)a.NamedArguments[0].TypedValue.Value) == 2);

        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <bool>) && a.ConstructorArguments.Count == 0 && a.NamedArguments.Count == 0);
        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <bool>) && a.ConstructorArguments.Count == 1 && a.NamedArguments.Count == 0 && a.ConstructorArguments[0].ArgumentType == typeof(bool) && ((bool)a.ConstructorArguments[0].Value) == true);
        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <bool>) && a.ConstructorArguments.Count == 0 && a.NamedArguments.Count == 1 && a.NamedArguments[0].TypedValue.ArgumentType == typeof(bool) && ((bool)a.NamedArguments[0].TypedValue.Value) == true);

        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <bool?>) && a.ConstructorArguments.Count == 0 && a.NamedArguments.Count == 0);

        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <Type>) && a.ConstructorArguments.Count == 1 && a.NamedArguments.Count == 0 && a.ConstructorArguments[0].ArgumentType == typeof(Type) && ((Type)a.ConstructorArguments[0].Value) == typeof(Class));
        AssertAny(a1_data, a => a.AttributeType == typeof(MultiAttribute <Type>) && a.ConstructorArguments.Count == 0 && a.NamedArguments.Count == 1 && a.NamedArguments[0].TypedValue.ArgumentType == typeof(Type) && ((Type)a.NamedArguments[0].TypedValue.Value) == typeof(Class.Derive));

        return(100);
    }
예제 #14
0
 public override object[] GetCustomAttributes(ITypeInfo attributeType, bool inherit)
 {
     return(CustomAttributeExtensions.GetCustomAttributes(this.Type.GetTypeInfo(), attributeType.Type, inherit).ToArray <object>());
 }
예제 #15
0
 internal static IEnumerable <T> GetCustomAttributes <T>(this Type t, bool inherit = true) where T : Attribute
 {
     return(CustomAttributeExtensions.GetCustomAttributes <T>(t.GetTypeInfo(), inherit));
 }
예제 #16
0
        /// <summary>
        /// 获取可以赋值给T的属性
        /// </summary>
        public static IEnumerable <T> GetCustomAttributes_ <T>(this MemberInfo prop, bool inherit = true) where T : Attribute
        {
            var attrs = CustomAttributeExtensions.GetCustomAttributes(prop, inherit);

            return(attrs.Where(x => x.GetType().IsAssignableTo_ <T>()).Select(x => (T)x).ToList());
        }
예제 #17
0
 public override void LoadAttributes(bool inherit = true)
 {
     this.SetAttributes(
         CustomAttributeExtensions.GetCustomAttributes(this.PropertyInfo, inherit));
 }
예제 #18
0
 public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit: false).ToArray();                                    // Desktop compat: for properties, this form of the api ignores "inherit"
예제 #19
0
 /// <summary>
 /// Only select the properties that are not decorated with and does not inherit from a parent class an attribute of the specified type.
 /// </summary>
 public PropertyInfoSelector ThatAreNotDecoratedWithOrInherit <TAttribute>()
     where TAttribute : Attribute
 {
     selectedProperties = selectedProperties.Where(property => !CustomAttributeExtensions.GetCustomAttributes(property, true).OfType <TAttribute>().Any());
     return(this);
 }
예제 #20
0
 public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this).ToArray();                                    // inherit is meaningless for Assemblies
예제 #21
0
        /// <summary>
        /// Use this method to override how your class can be, by default, mapped from entity to Bson document.
        /// Returns an EntityMapper from each requested Type
        /// </summary>
        protected virtual EntityMapper BuildEntityMapper(Type type)
        {
            var mapper = new EntityMapper(type);

            var idAttr     = typeof(BsonIdAttribute);
            var ignoreAttr = typeof(BsonIgnoreAttribute);
            var fieldAttr  = typeof(BsonFieldAttribute);
            var dbrefAttr  = typeof(BsonRefAttribute);

            var members = this.GetTypeMembers(type);
            var id      = this.GetIdMember(members);

            foreach (var memberInfo in members)
            {
                // checks [BsonIgnore]
                if (CustomAttributeExtensions.IsDefined(memberInfo, ignoreAttr, true))
                {
                    continue;
                }

                // checks field name conversion
                var name = this.ResolveFieldName(memberInfo.Name);

                // check if property has [BsonField]
                var field = (BsonFieldAttribute)CustomAttributeExtensions.GetCustomAttributes(memberInfo, fieldAttr, true).FirstOrDefault();

                // check if property has [BsonField] with a custom field name
                if (field != null && field.Name != null)
                {
                    name = field.Name;
                }

                // checks if memberInfo is id field
                if (memberInfo == id)
                {
                    name = "_id";
                }

                // create getter/setter function
                var getter = Reflection.CreateGenericGetter(type, memberInfo);
                var setter = Reflection.CreateGenericSetter(type, memberInfo);

                // check if property has [BsonId] to get with was setted AutoId = true
                var autoId = (BsonIdAttribute)CustomAttributeExtensions.GetCustomAttributes(memberInfo, idAttr, true).FirstOrDefault();

                // get data type
                var dataType = memberInfo is PropertyInfo ?
                               (memberInfo as PropertyInfo).PropertyType :
                               (memberInfo as FieldInfo).FieldType;

                // check if datatype is list/array
                var isEnumerable = Reflection.IsEnumerable(dataType);

                // create a property mapper
                var member = new MemberMapper
                {
                    AutoId         = autoId == null ? true : autoId.AutoId,
                    FieldName      = name,
                    MemberName     = memberInfo.Name,
                    DataType       = dataType,
                    IsEnumerable   = isEnumerable,
                    UnderlyingType = isEnumerable ? Reflection.GetListItemType(dataType) : dataType,
                    Getter         = getter,
                    Setter         = setter
                };

                // check if property has [BsonRef]
                var dbRef = (BsonRefAttribute)CustomAttributeExtensions.GetCustomAttributes(memberInfo, dbrefAttr, false).FirstOrDefault();

                if (dbRef != null && memberInfo is PropertyInfo)
                {
                    BsonMapper.RegisterDbRef(this, member, _typeNameBinder, dbRef.Collection ?? this.ResolveCollectionName((memberInfo as PropertyInfo).PropertyType));
                }

                // support callback to user modify member mapper
                this.ResolveMember?.Invoke(type, memberInfo, member);

                // test if has name and there is no duplicate field
                if (member.FieldName != null && mapper.Members.Any(x => x.FieldName.Equals(name, StringComparison.OrdinalIgnoreCase)) == false)
                {
                    mapper.Members.Add(member);
                }
            }

            return(mapper);
        }
예제 #22
0
        /// <summary>
        ///     Since we want to walk up the entire inheritance change, when not
        ///     give an instance, we need another helper method to walk up
        ///     the chain...
        /// </summary>
        private static bool ValidateInternalRecursive(LicenseContext context, Type type, object instance, bool allowExceptions, out License license, out string licenseKey)
        {
            LicenseProvider provider = GetCachedProvider(type);

            if (provider == null && !GetCachedNoLicenseProvider(type))
            {
                // NOTE : Must look directly at the class, we want no inheritance.
                //

                //TODO NETSTANDARD2.0
                // original:                 LicenseProviderAttribute attr = (LicenseProviderAttribute)Attribute.GetCustomAttribute(type, typeof(LicenseProviderAttribute), false);

                IEnumerator attributes        = CustomAttributeExtensions.GetCustomAttributes <LicenseProviderAttribute>(type, false).GetEnumerator();
                LicenseProviderAttribute attr = null;
                if (attributes.MoveNext())
                {
                    attr = (LicenseProviderAttribute)attributes.Current;
                }

                // END_TODO NETSTANDARD2.0


                if (attr != null)
                {
                    Type providerType = attr.LicenseProvider;
                    provider = GetCachedProviderInstance(providerType);

                    if (provider == null)
                    {
                        provider = (LicenseProvider)SecurityUtils.SecureCreateInstance(providerType);
                    }
                }

                CacheProvider(type, provider);
            }

            license = null;
            bool isValid = true;

            licenseKey = null;
            if (provider != null)
            {
                license = provider.GetLicense(context, type, instance, allowExceptions);
                if (license == null)
                {
                    isValid = false;
                }
                else
                {
                    // For the case where a COM client is calling "RequestLicKey",
                    // we try to squirrel away the first found license key
                    //
                    licenseKey = license.LicenseKey;
                }
            }

            // When looking only at a type, we need to recurse up the inheritence
            // chain, however, we can't give out the license, since this may be
            // from more than one provider.
            //
            if (isValid && instance == null)
            {
                Type baseType = type.BaseType;
                if (baseType != typeof(object) && baseType != null)
                {
                    if (license != null)
                    {
                        license.Dispose();
                        license = null;
                    }
                    string temp;
                    isValid = ValidateInternalRecursive(context, baseType, null, allowExceptions, out license, out temp);
                    if (license != null)
                    {
                        license.Dispose();
                        license = null;
                    }
                }
            }

            return(isValid);
        }