protected IJsonTypeConverter CreateTypeConverter(ICustomAttributeProvider provider)
 {
     if (provider.IsDefined(typeof(JsonConvertAttribute), false))
     {
         JsonConvertAttribute convAttr = (JsonConvertAttribute)provider.GetCustomAttributes(typeof(JsonConvertAttribute), false)[0];
         return CreateTypeConverter(convAttr);
     }
     return null;
 }
 public override void Process(IMetaData metaData, ICustomAttributeProvider attributeProvider, IConfiguration config)
 {
     if (metaData is IPropertyData)
     {
         IPropertyData property = (IPropertyData)metaData;
         if (attributeProvider.IsDefined(typeof(XmlIgnoreAttribute), false))
             property.Ignored = true;
     }
 }
Example #3
0
        private static bool GetIsRtimplFromCache(ICustomAttributeProvider cap)
        {
            lock (_isRtimplCache)
            {
                if (!_isRtimplCache.ContainsKey(cap))
                {
                    _isRtimplCache.Add(cap, cap.IsDefined(typeof(RtimplAttribute), false));
                }

                return _isRtimplCache[cap];
            }
        }
        public static IPropertyEditor TryCreate(Type type, ICustomAttributeProvider attributes)
        {
            if (type.IsNullableType()) {
                return new NullablePropertyEditor(type.GetGenericArguments()[0]);
            }

            if (attributes != null &&
                type.IsClass && attributes.IsDefined(typeof(InspectorNullableAttribute), /*inherit:*/true)) {
                return new NullablePropertyEditor(type);
            }

            return null;
        }
Example #5
0
        public void CustomAttributes(Type type)
        {
            Assembly           assembly       = Helpers.ExecutingAssembly;
            IEnumerable <Type> attributesData = assembly.CustomAttributes.Select(customAttribute => customAttribute.AttributeType);

            Assert.Contains(type, attributesData);

            ICustomAttributeProvider attributeProvider = assembly;

            Assert.Single(attributeProvider.GetCustomAttributes(type, false));
            Assert.True(attributeProvider.IsDefined(type, false));

            IEnumerable <Type> customAttributes = attributeProvider.GetCustomAttributes(false).Select(attribute => attribute.GetType());

            Assert.Contains(type, customAttributes);
        }
Example #6
0
        /// <summary>
        /// Retrieves a custom attribute of a specified type that is applied to a specified member.
        /// </summary>
        /// <typeparam name="T">The type of attribute to search for.</typeparam>
        /// <param name="attributeProvider">The member to inspect.</param>
        /// <param name="inherit"><see langword="true" /> to inspect the ancestors of element; otherwise, <see langword="false" /> (the default).</param>
        /// <returns>A custom attribute that matches <typeparamref name="T"/>, or <see langword="null" /> if no such attribute is found.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="attributeProvider"/> is <see langword="null"/>.</exception>
        /// <exception cref="System.InvalidOperationException">Thrown if more than one attribute of the specified type <typeparamref name="T"/> is found.</exception>
        internal static T GetCustomAttribute <T>(
            this ICustomAttributeProvider attributeProvider,
            bool inherit = false) where T : class
        {
            if (attributeProvider == null)
            {
                throw new ArgumentNullException(nameof(attributeProvider));
            }

            if (!attributeProvider.IsDefined(typeof(T), inherit))
            {
                return(null);
            }

            return((T)attributeProvider.GetCustomAttributes(typeof(T), inherit).Single());
        }
Example #7
0
        /// <summary>
        /// Returns a new instance with <see cref="RequiredAttribute"/>, <see cref="OptionalAttribute"/>, <see cref="RequiredListAttribute"/>,
        /// <see cref="OptionalListAttribute"/>, <see cref="IdAttribute"/> and <see cref="DIGraphAttribute"/>
        /// applied as necessary.
        /// </summary>
        internal TypeInformation ApplyAttributes(ICustomAttributeProvider member)
        {
            var typeInformation = this; //copy struct

            //var member = examineParent ? (ICustomAttributeProvider)typeInformation.ParameterInfo.Member : typeInformation.ParameterInfo;
            if (typeInformation.IsNullable)
            {
                if (member.IsDefined(typeof(RequiredAttribute), false))
                {
                    typeInformation.IsNullable = false;
                }
                if (member.IsDefined(typeof(System.ComponentModel.DataAnnotations.RequiredAttribute), false))
                {
                    typeInformation.IsNullable = false;
                }
            }
            else
            {
                if (member.IsDefined(typeof(OptionalAttribute), false))
                {
                    typeInformation.IsNullable = true;
                }
            }
            if (typeInformation.IsList)
            {
                if (typeInformation.ListIsNullable)
                {
                    if (member.IsDefined(typeof(RequiredListAttribute), false))
                    {
                        typeInformation.ListIsNullable = false;
                    }
                }
                else
                {
                    if (member.IsDefined(typeof(OptionalListAttribute), false))
                    {
                        typeInformation.ListIsNullable = true;
                    }
                }
            }
            if (member.IsDefined(typeof(IdAttribute), false))
            {
                typeInformation.GraphType = typeof(IdGraphType);
            }
            else if (member.GetCustomAttributes(typeof(DIGraphAttribute), false).SingleOrDefault() is DIGraphAttribute diGraphAttribute)
            {
                var iface = diGraphAttribute.GraphBaseType.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDIObjectGraphBase <>));
                if (iface == null)
                {
                    throw new InvalidOperationException($"Member '{typeInformation.MemberInfo.DeclaringType.Name}.{typeInformation.MemberInfo.Name}' is marked with [DIGraph] specifying type '{diGraphAttribute.GraphBaseType.Name}' which does not inherit {nameof(IDIObjectGraphBase)}<T>.");
                }
                typeInformation.GraphType = typeof(DIObjectGraphType <,>).MakeGenericType(diGraphAttribute.GraphBaseType, iface.GetGenericArguments()[0]);
            }
            return(typeInformation);
        }
Example #8
0
        public static bool IsJsonIgnore(object value)
        {
            if (value == null)
            {
                return(false);
            }
            Type type = value.GetType();
            ICustomAttributeProvider customAttributeProvider = null;

            customAttributeProvider = ((!type.IsEnum) ? (value as ICustomAttributeProvider) : type.GetField(Enum.GetName(type, value)));
            if (customAttributeProvider == null)
            {
                throw new ArgumentException();
            }
            return(customAttributeProvider.IsDefined(typeof(JsonIgnoreAttribute), inherit: true));
        }
Example #9
0
        /// <summary>
        /// Retrieves all custom attributes of a specified type that are applied to a specified member.
        /// </summary>
        /// <typeparam name="T">The type of attribute to search for.</typeparam>
        /// <param name="attributeProvider">The member to inspect.</param>
        /// <param name="inherit"><see langword="true" /> to inspect the ancestors of element; otherwise, <see langword="false" /> (the default).</param>
        /// <returns>A sequence of custom attributes that match <typeparamref name="T"/> (possibly empty).</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="attributeProvider"/> is <see langword="null"/>.</exception>
        internal static IEnumerable <T> GetCustomAttributes <T>(
            this ICustomAttributeProvider attributeProvider,
            bool inherit = false) where T : class
        {
            if (attributeProvider == null)
            {
                throw new ArgumentNullException(nameof(attributeProvider));
            }

            if (!attributeProvider.IsDefined(typeof(T), inherit))
            {
                return(new T[0]);
            }

            return(attributeProvider.GetCustomAttributes(typeof(T), inherit).OfType <T>());
        }
    public static string?GetGraphQLDescription(
        this ICustomAttributeProvider attributeProvider)
    {
        if (attributeProvider.IsDefined(
                typeof(GraphQLDescriptionAttribute),
                false))
        {
            GraphQLDescriptionAttribute attribute =
                (GraphQLDescriptionAttribute)
                attributeProvider.GetCustomAttributes(
                    typeof(GraphQLDescriptionAttribute),
                    false)[0];
            return(attribute.Description);
        }

        return(null);
    }
Example #11
0
        public void CustomAttributes(Type type)
        {
            Assembly assembly = Helpers.ExecutingAssembly;
            IEnumerable <CustomAttributeData> attributesData = assembly.CustomAttributes;
            bool result = attributesData.Any(customAttribute => customAttribute.AttributeType.Equals(type));

            Assert.True(result, $"Did not find custom attribute of type {type}.");

            ICustomAttributeProvider attributeProvider = assembly;

            Assert.Equal(1, attributeProvider.GetCustomAttributes(type, false).Length);
            Assert.True(attributeProvider.IsDefined(type, false));

            object[] customAttributes = attributeProvider.GetCustomAttributes(false);
            result = customAttributes.Any(attribute => attribute.GetType().Equals(type));
            Assert.True(result, $"Did not find custom attribute of type {type}.");
        }
        public static string GetGraphQLDescription(
            this ICustomAttributeProvider attributeProvider)
        {
            if (attributeProvider.IsDefined(
                    typeof(GraphQLDescriptionAttribute),
                    false))
            {
                var attribute = attributeProvider.GetCustomAttributes(
                    typeof(GraphQLDescriptionAttribute),
                    false)
                                .OfType <GraphQLDescriptionAttribute>()
                                .FirstOrDefault();
                return(attribute.Description);
            }

            return(null);
        }
        public static TAttribute GetAttribute <TAttribute>(this ICustomAttributeProvider target, bool inherits)
            where TAttribute : Attribute
        {
            if (!target.IsDefined(typeof(TAttribute), inherits))
            {
                return(null);
            }

            var attributes = target.GetCustomAttributes(typeof(TAttribute), inherits);

            if (attributes.Length > 1)
            {
                throw Error.MoreThanOneElement();
            }

            return((TAttribute)attributes[0]);
        }
Example #14
0
        private static bool IsTypeHierarchyBase(ICustomAttributeProvider type)
        {
            if (type.IsDefined(typeof(JoinedBaseAttribute), false))
            {
                return(true);
            }

            object[] attrs = type.GetCustomAttributes(typeof(ActiveRecordAttribute), false);

            if (attrs != null && attrs.Length > 0)
            {
                ActiveRecordAttribute att = (ActiveRecordAttribute)attrs[0];

                return(att.DiscriminatorColumn != null);
            }

            return(false);
        }
Example #15
0
        public static object GetAttachData(this ICustomAttributeProvider provider, object key)
        {
            if (!provider.IsDefined(typeof(AttachDataAttribute), true))
            {
                return(null);
            }

            var attributes = provider.GetCustomAttributes(typeof(AttachDataAttribute), true);

            if (attributes.IsNullOrEmpty())
            {
                return(null);
            }
            else
            {
                return(attributes.As <AttachDataAttribute[]>().First(a => a.Key.Equals(key)).Value);
            }
        }
Example #16
0
 public static bool HasAttr <T>(this ICustomAttributeProvider cap, bool inherit)
     where T : Attribute
 {
     // a crude workaround
     if (cap is PropertyInfo && inherit)
     {
         var pi        = (PropertyInfo)cap;
         var hierarchy = pi.DeclaringType.LookupInheritanceChain();
         return(hierarchy.Any(t =>
         {
             var basep = t.GetProperties(BF.All).SingleOrDefault(p => p.Name == pi.Name && p.DeclaringType == t);
             return basep == null ? false : basep.IsDefined(typeof(T), true);
         }));
     }
     else
     {
         return(cap.IsDefined(typeof(T), inherit));
     }
 }
        public static void CannotDoWithReflectionOnlyAssemblies()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                // Storing as ICustomAttributeProvider so we don't accidentally pick up the CustomAttributeExtensions extension methods.
                ICustomAttributeProvider icp = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);

                Assert.Throws <InvalidOperationException>(() => icp.GetCustomAttributes(inherit: false));
                Assert.Throws <InvalidOperationException>(() => icp.GetCustomAttributes(null, inherit: false));
                Assert.Throws <InvalidOperationException>(() => icp.IsDefined(null, inherit: false));
            }

            Assembly coreAssembly = typeof(object).Project().Assembly;

            if (coreAssembly.ReflectionOnly)
            {
                Assert.Throws <ArgumentException>(() => coreAssembly.CreateInstance("System.Object")); // Compat quirk: Why ArgumentException instead of InvalidOperationException?
            }
        }
 public bool IsDefined(ICustomAttributeProvider provider, Type attributeType, bool inherit)
 {
     if (provider is MemberInfo memberInfo)
     {
         return(Attribute.IsDefined(memberInfo, attributeType, inherit));
     }
     if (provider is ParameterInfo parameterInfo)
     {
         return(Attribute.IsDefined(parameterInfo, attributeType, inherit));
     }
     if (provider is Assembly assembly)
     {
         return(Attribute.IsDefined(assembly, attributeType, inherit));
     }
     if (provider is Module module)
     {
         return(Attribute.IsDefined(module, attributeType, inherit));
     }
     return(provider.IsDefined(attributeType, inherit));
 }
        private static void VerifyCustomAttribute(Type type, String attributeStr)
        {
            Assembly asm = GetExecutingAssembly();
            IEnumerator <CustomAttributeData> customAttrs = asm.CustomAttributes.GetEnumerator();
            CustomAttributeData current = null;

            bool result = false;

            while (customAttrs.MoveNext())
            {
                current = customAttrs.Current;
                if (current.AttributeType.Equals(type))
                {
                    result = true;
                    break;
                }
            }

            Assert.True(result, string.Format("Did not find custom attribute of type {0} ", type));

            result = false;
            // Also double check that we can get these values via ICustomAttributeProvider
            ICustomAttributeProvider prov = asm as ICustomAttributeProvider;

            Assert.NotNull(prov.GetCustomAttributes(type, false));
            Assert.Equal(1, prov.GetCustomAttributes(type, false).Length);
            Assert.True(prov.IsDefined(type, false));

            // Check that there exists a custom attribute with the same type.
            object[] atrs = prov.GetCustomAttributes(false);
            for (int i = 0; i < atrs.Length; i++)
            {
                if (atrs[i].GetType().Equals(type))
                {
                    result = true;
                    break;
                }
            }

            Assert.True(result, string.Format("Did not find custom attribute of type {0} ", type));
        }
        public static TAttribute[] GetAttributes <TAttribute>(this ICustomAttributeProvider target, bool inherits)
            where TAttribute : Attribute
        {
            if (!target.IsDefined(typeof(TAttribute), inherits))
            {
                return(new TAttribute[0]);
            }

            var attributes = target
                             .GetCustomAttributes(typeof(TAttribute), inherits)
                             .Cast <TAttribute>();

            return(attributes.ToArray());

            #region Obsolete

            //return target
            //    .GetCustomAttributes(typeof(TAttribute), inherits)
            //    .ToArray(a => (TAttribute)a);

            #endregion

            #region Obsolete

            //// OBSOLETE 1
            //return target.GetCustomAttributes(typeof(TAttribute), inherits).Cast<TAttribute>().ToArray();

            //// OBSOLETE 2
            //object[] attributesAsObjects = member.GetCustomAttributes(typeof(TAttribute), inherits);
            //TAttribute[] attributes = new TAttribute[attributesAsObjects.Length];
            //int index = 0;
            //Array.ForEach(attributesAsObjects,
            //    delegate(object o)
            //    {
            //        attributes[index++] = (TAttribute)o;
            //    });
            //return attributes;

            #endregion
        }
Example #21
0
 public static bool HasAttr(this ICustomAttributeProvider cap, Type t, bool inherit)
 {
     // a crude workaround
     if (cap == null)
     {
         return(false);
     }
     if (cap is PropertyInfo && inherit)
     {
         var pi        = (PropertyInfo)cap;
         var hierarchy = pi.DeclaringType.Hierarchy();
         return(hierarchy.Any(t1 =>
         {
             var basep = t1.GetProperties(BF.All | BF.DeclOnly).SingleOrDefault(p => p.DeclaringType == t1 &&
                                                                                p.Name == pi.Name && Seq.Equal(p.GetIndexParameters().Select(ppi => ppi.ParameterType), pi.GetIndexParameters().Select(ppi => ppi.ParameterType)));
             return basep == null ? false : basep.IsDefined(t, true);
         }));
     }
     else
     {
         return(cap.IsDefined(t, inherit));
     }
 }
Example #22
0
        public static bool IsJsonIgnore(object value)
        {
            if (value == null)
            {
                return(false);
            }
            Type enumType = value.GetType();
            ICustomAttributeProvider field = null;

            if (enumType.IsEnum)
            {
                field = enumType.GetField(Enum.GetName(enumType, value));
            }
            else
            {
                field = value as ICustomAttributeProvider;
            }
            if (field == null)
            {
                throw new ArgumentException();
            }
            return(field.IsDefined(typeof(JsonIgnoreAttribute), true));
        }
        /// <summary>
        /// Gets a value which indicates if should be ignored in Json serialization.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsJsonIgnore(object value)
        {
            if (value == null)
            {
                return(false);
            }
            Type type = value.GetType();

#if NETFX_CORE
            if (value is MemberInfo)
            {
                var mi = (MemberInfo)value;
                return(mi.GetCustomAttribute <JsonIgnoreAttribute>(true) != null);
            }
            else
            {
                var attrs = type.GetCustomAttributes(typeof(JsonIgnoreAttribute), true);
                return(attrs != null && attrs.Length > 0);
            }
#else
            ICustomAttributeProvider provider = null;
            if (type.IsEnum)
            {
                provider = type.GetField(Enum.GetName(type, value));
            }
            else
            {
                provider = value as ICustomAttributeProvider;
            }
            if (provider == null)
            {
                throw new ArgumentException();
            }

            return(provider.IsDefined(typeof(JsonIgnoreAttribute), true));
#endif
        }
        /// <summary>
        /// Gets a value which indicates if should be ignored in Json serialization.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsXmlIgnore(object value)
        {
            if (value == null)
            {
                return(false);
            }

            Type type = value.GetType();


#if !UNITY3D && !NETFX_CORE
            if (type.IsEnum())
            {
                provider = type.GetField(Enum.GetName(type, value)).ToICustomAttributeProvider();
            }

            ICustomAttributeProvider provider = null;
            if (type.IsEnum)
            {
                provider = type.GetField(Enum.GetName(type, value));
            }
            else
            {
                provider = value as ICustomAttributeProvider;
            }

            if (provider == null)
            {
                throw new ArgumentException();
            }


            return(provider.IsDefined(typeof(XmlIgnoreAttribute), true));
#endif
            return(false);
        }
Example #25
0
 /// <summary>
 /// Return true if the type has a [ActiveRecord] attribute
 /// </summary>
 private static bool IsActiveRecordType(ICustomAttributeProvider type)
 {
     return(type.IsDefined(typeof(ActiveRecordAttribute), false));
 }
Example #26
0
		static bool IsDynamicType (ICustomAttributeProvider ca, int index)
		{
#if NET_4_0
			if (ca.IsDefined (typeof (DynamicAttribute), false)) {
				if (index == 0)
					return true;

				var v = (DynamicAttribute) ca.GetCustomAttributes (typeof (DynamicAttribute), false)[0];
				return v.TransformFlags[index];
			}
#endif
			return false;
		}
Example #27
0
        /// <summary>
        /// Gets a value indicating if the method info <paramref name="t"/> is tagged
        /// by a <paramref name="customAttributeType"/> instance.
        /// </summary>
        /// <param name="t">method to test</param>
        /// <param name="customAttributeType">custom attribute type to search</param>
        /// <returns>
        /// true if <param name="t"/> is tagged by a <paramref name="customAttributeType"/>
        /// attribute, false otherwise.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="t"/> or <paramref name="customAttributeType"/>
        /// is a null reference
        /// </exception>
        /// <remarks>
        /// You can use this method to check that a method is tagged by a
        /// specified attribute.
        /// </remarks>
        public static bool HasCustomAttribute(ICustomAttributeProvider t, Type customAttributeType)
        {
            if (t == null)
                throw new ArgumentNullException("t");
            if (customAttributeType == null)
                throw new ArgumentNullException("customAttributeType");

            return t.IsDefined(customAttributeType, true);
        }
 public static bool IsDefined(ICustomAttributeProvider provider, Type attributeType)
 {
     return provider.IsDefined(attributeType, true);
 }
Example #29
0
 public static bool HasAttribute <T>(this ICustomAttributeProvider member, bool inherit)
     where T : Attribute
 {
     return(member.IsDefined(typeof(T), inherit));
 }
Example #30
0
 public static bool IsDeprecated(ICustomAttributeProvider attrProvider)
 {
     return attrProvider.IsDefined (typeof (ObsoleteAttribute), true);
 }
 private static bool memberClaimsToImplementInterface(
     ICustomAttributeProvider member
 )
 {
     return member.IsDefined(typeof(Implements), false);
 }
	// Determine if an attribute is defined on a particular program item.
	private static bool IsAttrDefined(ICustomAttributeProvider element,
									  Type attributeType, bool inherit)
			{
				if(element == null)
				{
					throw new ArgumentNullException("element");
				}
				if(attributeType == null)
				{
					throw new ArgumentNullException("attributeType");
				}
				return element.IsDefined(attributeType, inherit);
			}
Example #33
0
		internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
		{
			if (attributeType == null)
				throw new ArgumentNullException ("attributeType");

			AttributeUsageAttribute usage = null;
			do {
				if (IsUserCattrProvider (obj))
					return obj.IsDefined (attributeType, inherit);

				if (IsDefinedInternal (obj, attributeType))
					return true;

				object[] pseudoAttrs = GetPseudoCustomAttributes (obj, attributeType);
				if (pseudoAttrs != null) {
					for (int i = 0; i < pseudoAttrs.Length; ++i)
						if (attributeType.IsAssignableFrom (pseudoAttrs[i].GetType ()))
							return true;
				}

				if (usage == null) {
					if (!inherit)
						return false;

					usage = RetrieveAttributeUsage (attributeType);
					if (!usage.Inherited)
						return false;
				}

				obj = GetBase (obj);
			} while (obj != null);

			return false;
		}
		/// <summary>
		/// Return true if the type has a [ActiveRecord] attribute
		/// </summary>
		private static bool IsActiveRecordType(ICustomAttributeProvider type)
		{
			return type.IsDefined(typeof(ActiveRecordAttribute), false);
		}
Example #35
0
 private static string DescriptionOf(ICustomAttributeProvider property)
 {
     if (property.IsDefined(typeof(DescriptionAttribute), false))
     {
         var attribute = (DescriptionAttribute) property.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
         return attribute.Value;
     }
     return string.Empty;
 }
Example #36
0
 public virtual bool IsByVal(ICustomAttributeProvider icap)
 {
     return icap.IsDefined (typeof (ByValAttribute), false);
 }
Example #37
0
 public static bool IsDeprecated(ICustomAttributeProvider attrProvider)
 {
     return(attrProvider.IsDefined(typeof(ObsoleteAttribute), true));
 }
Example #38
0
		public TypeSpec CreateType (Type type, TypeSpec declaringType, ICustomAttributeProvider ca, int dynamicCursor, bool canImportBaseType)
		{
			TypeSpec spec;
			if (import_cache.TryGetValue (type, out spec)) {
				if (ca == null)
					return spec;

				if (type == typeof (object)) {
					if (IsDynamicType (ca, dynamicCursor))
						return InternalType.Dynamic;

					return spec;
				}

				if (!spec.IsGeneric)
					return spec;

#if NET_4_0
				if (!ca.IsDefined (typeof (DynamicAttribute), false))
#endif
					return spec;

				// We've found same object in the cache but this one has a dynamic custom attribute
				// and it's most likely dynamic version of same type IFoo<object> agains IFoo<dynamic>
				// Do resolve the type process again in that case
			}

			if (type.IsGenericType && !type.IsGenericTypeDefinition) {
				var type_def = type.GetGenericTypeDefinition ();
				var targs = CreateGenericArguments (0, type.GetGenericArguments (), ca, dynamicCursor + 1);
				if (declaringType == null) {
					// Simple case, no nesting
					spec = CreateType (type_def, null, null, 0, canImportBaseType);
					spec = spec.MakeGenericType (targs);
				} else {
					//
					// Nested type case, converting .NET types like
					// A`1.B`1.C`1<int, long, string> to typespec like
					// A<int>.B<long>.C<string>
					//
					var nested_hierarchy = new List<TypeSpec> ();
					while (declaringType.IsNested) {
						nested_hierarchy.Add (declaringType);
						declaringType = declaringType.DeclaringType;
					}

					int targs_pos = 0;
					if (declaringType.Arity > 0) {
						spec = declaringType.MakeGenericType (targs.Skip (targs_pos).Take (declaringType.Arity).ToArray ());
						targs_pos = spec.Arity;
					} else {
						spec = declaringType;
					}

					for (int i = nested_hierarchy.Count; i != 0; --i) {
						var t = nested_hierarchy [i - 1];
						spec = MemberCache.FindNestedType (spec, t.Name, t.Arity);
						if (t.Arity > 0) {
							spec = spec.MakeGenericType (targs.Skip (targs_pos).Take (spec.Arity).ToArray ());
							targs_pos += t.Arity;
						}
					}

					string name = type.Name;
					int index = name.IndexOf ('`');
					if (index > 0)
						name = name.Substring (0, index);

					spec = MemberCache.FindNestedType (spec, name, targs.Length - targs_pos);
					if (spec.Arity > 0) {
						spec = spec.MakeGenericType (targs.Skip (targs_pos).ToArray ());
					}
				}

				// Don't add generic type with dynamic arguments, they can interfere with same type
				// using object type arguments
				if (!spec.HasDynamicElement) {

					// Add to reading cache to speed up reading
					if (!import_cache.ContainsKey (type))
						import_cache.Add (type, spec);
				}

				return spec;
			}

			Modifiers mod;
			MemberKind kind;

			var ma = type.Attributes;
			switch (ma & TypeAttributes.VisibilityMask) {
			case TypeAttributes.Public:
			case TypeAttributes.NestedPublic:
				mod = Modifiers.PUBLIC;
				break;
			case TypeAttributes.NestedPrivate:
				mod = Modifiers.PRIVATE;
				break;
			case TypeAttributes.NestedFamily:
				mod = Modifiers.PROTECTED;
				break;
			case TypeAttributes.NestedFamORAssem:
				mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
				break;
			default:
				mod = Modifiers.INTERNAL;
				break;
			}

			if ((ma & TypeAttributes.Interface) != 0) {
				kind = MemberKind.Interface;
			} else if (type.IsGenericParameter) {
				kind = MemberKind.TypeParameter;
			} else if (type.IsClass || type.IsAbstract) {  				// System.Reflection: System.Enum returns false for IsClass
				if ((ma & TypeAttributes.Sealed) != 0 && type.IsSubclassOf (typeof (MulticastDelegate))) {
					kind = MemberKind.Delegate;
					mod |= Modifiers.SEALED;
				} else {
					kind = MemberKind.Class;
					if ((ma & TypeAttributes.Sealed) != 0) {
						mod |= Modifiers.SEALED;
						if ((ma & TypeAttributes.Abstract) != 0)
							mod |= Modifiers.STATIC;
					} else if ((ma & TypeAttributes.Abstract) != 0) {
						mod |= Modifiers.ABSTRACT;
					}
				}
			} else if (type.IsEnum) {
				kind = MemberKind.Enum;
			} else {
				kind = MemberKind.Struct;
				mod |= Modifiers.SEALED;
			}

			var definition = new ImportedTypeDefinition (this, type);
			PredefinedTypeSpec pt;

			if (kind == MemberKind.Enum) {
				const BindingFlags underlying_member = BindingFlags.DeclaredOnly |
					BindingFlags.Instance |
					BindingFlags.Public | BindingFlags.NonPublic;

				var type_members = type.GetFields (underlying_member);
				foreach (var type_member in type_members) {
					spec = new EnumSpec (declaringType, definition, CreateType (type_member.FieldType), type, mod);
					break;
				}

				if (spec == null)
					kind = MemberKind.Class;

			} else if (kind == MemberKind.TypeParameter) {
				// Return as type_cache was updated
				return CreateTypeParameter (type, declaringType);
			} else if (type.IsGenericTypeDefinition) {
				definition.TypeParameters = CreateGenericParameters (type, declaringType);

				// Constraints are not loaded on demand and can reference this type
				if (import_cache.TryGetValue (type, out spec))
					return spec;

			} else if (type_2_predefined.TryGetValue (type, out pt)) {
				spec = pt;
				pt.SetDefinition (definition, type);
			}

			if (spec == null)
				spec = new TypeSpec (kind, declaringType, definition, type, mod);

			import_cache.Add (type, spec);

			//
			// Two stage setup as the base type can be inflated declaring type or
			// another nested type inside same declaring type which has not been
			// loaded, therefore we can import a base type of nested types once
			// the types have been imported
			//
			if (canImportBaseType)
				ImportTypeBase (spec, type);

			return spec;
		}
		internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
		{
			if (attributeType == null)
				throw new ArgumentNullException ("attributeType");

			if (IsUserCattrProvider (obj))
				return obj.IsDefined (attributeType, inherit);

			if (IsDefinedInternal (obj, attributeType))
				return true;

			object[] pseudoAttrs = GetPseudoCustomAttributes (obj, attributeType);
			if (pseudoAttrs != null) {
				for (int i = 0; i < pseudoAttrs.Length; ++i)
					if (attributeType.IsAssignableFrom (pseudoAttrs [i].GetType ()))
						return true;
			}

#if ONLY_1_1
			if (inherit) {
				AttributeUsageAttribute usage = RetrieveAttributeUsage (attributeType);
				if (!usage.Inherited)
					inherit = false;
			}
#endif

			// FIXME (bug #82431):
			// on 2.0 profile we should always walk the inheritance
			// chain and base the behavior on the inheritance level:
			//
			// 0  : return true if "attributeType" is assignable from
			// any of the custom attributes
			//
			// > 0: return true if "attributeType" is assignable from
			// any of the custom attributes and AttributeUsageAttribute
			// .Inherited of the assignable attribute is true

			ICustomAttributeProvider btype;
			if (inherit && ((btype = GetBase (obj)) != null))
				return IsDefined (btype, attributeType, inherit);

			return false;
		}
Example #40
0
 private static bool IsRequired(ICustomAttributeProvider property)
 {
     return property.IsDefined(typeof(RequiredAttribute), false);
 }
 private static bool HasConfiguration(ICustomAttributeProvider element)
 {
     return(element.IsDefined(typeof(GraphQLTypeAttribute), true) ||
            element.GetCustomAttributes(typeof(DescriptorAttribute), true).Length > 0);
 }
Example #42
0
 public bool IsDefined(Type attributeType, bool inherit)
 {
     return(_attributes.IsDefined(attributeType, inherit));
 }
Example #43
0
 public static bool HasAttribute <TAttribute>(this ICustomAttributeProvider target, bool inherits) where TAttribute : Attribute
 {
     return(target.IsDefined(typeof(TAttribute), inherits));
 }
Example #44
0
 /// <summary>
 /// Gets the attribute from the item
 /// </summary>
 /// <typeparam name="T">Attribute type</typeparam>
 /// <param name="Provider">Attribute provider</param>
 /// <param name="Inherit">When true, it looks up the heirarchy chain for the inherited custom attributes</param>
 /// <returns>Attribute specified if it exists</returns>
 public static T Attribute <T>(this ICustomAttributeProvider Provider, bool Inherit = true) where T : Attribute
 {
     return(Provider.IsDefined(typeof(T), Inherit) ? Provider.Attributes <T>(Inherit)[0] : default(T));
 }
Example #45
0
 /// <summary>
 /// Gets the attributes from the item
 /// </summary>
 /// <typeparam name="T">Attribute type</typeparam>
 /// <param name="Provider">Attribute provider</param>
 /// <param name="Inherit">When true, it looks up the heirarchy chain for the inherited custom attributes</param>
 /// <returns>Array of attributes</returns>
 public static T[] Attributes <T>(this ICustomAttributeProvider Provider, bool Inherit = true) where T : Attribute
 {
     return(Provider.IsDefined(typeof(T), Inherit) ? Provider.GetCustomAttributes(typeof(T), Inherit).ToArray(x => (T)x) : new T[0]);
 }
 protected override bool IsNonAction(ICustomAttributeProvider action)
 {
     return action.IsDefined(typeof(NonActionAttribute), inherit: true);
 }
 public static bool HasAttributeInherit <T>(this ICustomAttributeProvider mi) where T : Attribute
 {
     return(mi.IsDefined(typeof(T), true));
 }
		private static bool IsTypeHierarchyBase(ICustomAttributeProvider type)
		{
			if (type.IsDefined(typeof(JoinedBaseAttribute), false))
			{
				return true;
			}

			object[] attrs = type.GetCustomAttributes(typeof(ActiveRecordAttribute), false);

			if (attrs != null && attrs.Length > 0)
			{
				ActiveRecordAttribute att = (ActiveRecordAttribute) attrs[0];

				return att.DiscriminatorColumn != null;
			}

			return false;
		}
 public static bool IsAttributeDefined <T>(this ICustomAttributeProvider attributeProvider) where T : class
 {
     return(attributeProvider.IsDefined(typeof(T), false));
 }
Example #50
0
		public virtual bool IsByRef (ICustomAttributeProvider icap, Type type)
		{
			return type.IsByRef || icap.IsDefined (typeof (ByRefAttribute), false);
		}