private static T GetAttributeFromCache <T> (MemberInfo memberInfo)
     where T : Attribute
 {
     return((T)s_attributeCache.GetOrCreateValue(
                Tuple.Create(memberInfo, typeof(T)),
                key => AttributeUtility.GetCustomAttribute <T> (key.Item1, true)));
 }
Exemplo n.º 2
0
        public void Test_FromBaseWithInterface()
        {
            ICustomAttribute attribute =
                (ICustomAttribute)AttributeUtility.GetCustomAttribute(_basePropertyWithSingleAttribute, typeof(ICustomAttribute), true);

            Assert.That(attribute, Is.Not.Null);
        }
Exemplo n.º 3
0
        private static Type FindProviderAttributeType(Type type)
        {
            var concreteType = MixinTypeUtility.GetConcreteMixedType(type);
            var attribute    = AttributeUtility.GetCustomAttribute <BusinessObjectProviderAttribute> (concreteType, true);

            if (attribute == null)
            {
                var message = string.Format(
                    "The type '{0}' does not have the '{1}' applied.",
                    type.FullName,
                    typeof(BusinessObjectProviderAttribute).FullName);
                throw new ArgumentException(message, "type");
            }

            if (!TypeExtensions.CanAscribeTo(attribute.BusinessObjectProviderType, typeof(BindableObjectProvider)))
            {
                var message = string.Format(
                    "The business object provider associated with the type '{0}' is not of type '{1}'.",
                    type.FullName,
                    typeof(BindableObjectProvider).FullName);
                throw new ArgumentException(message, "type");
            }

            return(attribute.GetType());
        }
        protected virtual bool PropertyFilter(MemberInfo memberInfo, object filterCriteria)
        {
            // properties explicitly marked invisible are ignored
            var attribute = AttributeUtility.GetCustomAttribute <ObjectBindingAttribute> (memberInfo, true);

            if (attribute != null && !attribute.Visible)
            {
                return(false);
            }

            var propertyInfo = (PropertyInfo)memberInfo;

            // indexed properties are ignored
            if (propertyInfo.GetIndexParameters().Length > 0)
            {
                return(false);
            }

            // properties with a public getter are included, as long as that getter is not an infrastructure property
            var publicGetter = propertyInfo.GetGetMethod(false);

            if (publicGetter != null && !IsInfrastructureProperty(propertyInfo, publicGetter))
            {
                return(true);
            }

            // property can be any interface implementation as long as it is not an infrastructure property
            var publicOrNonPublicGetter = publicGetter ?? propertyInfo.GetGetMethod(true);

            return(publicOrNonPublicGetter != null &&
                   _interfaceMethodImplementations.ContainsKey(publicOrNonPublicGetter) &&
                   !_interfaceMethodImplementations[publicOrNonPublicGetter].TrueForAll(m => IsInfrastructureProperty(propertyInfo, m)));
        }
Exemplo n.º 5
0
        private Enum GetUndefinedValue()
        {
            var undefinedEnumValueAttribute = AttributeUtility.GetCustomAttribute <UndefinedEnumValueAttribute> (UnderlyingType, false);

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

            if (!UnderlyingType.IsInstanceOfType(undefinedEnumValueAttribute.GetValue()))
            {
                throw new InvalidOperationException(
                          string.Format(
                              "The enum type '{0}' defines a '{1}' with an enum value that belongs to a different enum type.",
                              UnderlyingType,
                              typeof(UndefinedEnumValueAttribute)));
            }

            if (IsNullable)
            {
                throw new InvalidOperationException(
                          string.Format(
                              "The property '{0}' defined on type '{1}' must not be nullable since the property's type already defines a '{2}'.",
                              Identifier,
                              PropertyInfo.DeclaringType,
                              typeof(UndefinedEnumValueAttribute)));
            }

            return(undefinedEnumValueAttribute.GetValue());
        }
 private MethodInfo GetWrapper(Type type, MethodInfo wrappedMethod)
 {
     return((from method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
             let attribute = AttributeUtility.GetCustomAttribute <GeneratedMethodWrapperAttribute> (method, false)
                             where attribute != null && attribute.ResolveReferencedMethod().Equals(wrappedMethod)
                             select method).Single());
 }
        public static WxeParameterAttribute GetAttribute(PropertyInfo property)
        {
            ArgumentUtility.CheckNotNull("property", property);

            WxeParameterAttribute attribute = AttributeUtility.GetCustomAttribute <WxeParameterAttribute> (property, false);

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

            if (!property.IsOriginalDeclaration())
            {
                throw new WxeException(
                          string.Format(
                              "Property '{0}', overridden by '{1}', has a WxeParameterAttribute applied. The WxeParameterAttribute may only be applied to the original declaration of a property.",
                              property.Name,
                              property.DeclaringType));
            }

            if (!attribute._required.HasValue)
            {
                attribute._required = property.PropertyType.IsValueType;
            }
            return(attribute);
        }
        public string GetClassID(Type type)
        {
            ArgumentUtility.CheckNotNull("type", type);

            var attribute = AttributeUtility.GetCustomAttribute <ClassIDAttribute> (type, false);

            return(attribute != null ? attribute.ClassID : type.Name);
        }
        public void GetCustomAttribute()
        {
            var type    = typeof(DateTime);
            var adapter = TypeAdapter.Create(type);

            Assert.That(
                adapter.GetCustomAttribute <SerializableAttribute> (true),
                Is.EqualTo(AttributeUtility.GetCustomAttribute <SerializableAttribute> (type, true)));
        }
Exemplo n.º 10
0
        public IEnumerable <IMetaValidationRule> GetMetaValidationRules()
        {
            var lengthConstraintAttribute = AttributeUtility.GetCustomAttribute <ILengthConstrainedPropertyAttribute> (_implementationProperty, false);

            if (lengthConstraintAttribute != null && lengthConstraintAttribute.MaximumLength.HasValue)
            {
                yield return(new RemotionMaxLengthMetaValidationRule(_implementationProperty, lengthConstraintAttribute.MaximumLength.Value));
            }
        }
Exemplo n.º 11
0
        private Type GetGetObjectServiceType()
        {
            var attribute = AttributeUtility.GetCustomAttribute <IBusinessObjectServiceTypeAttribute <IGetObjectService> > (ConcreteType, true);

            if (attribute == null)
            {
                return(typeof(IGetObjectService));
            }
            return(attribute.Type);
        }
Exemplo n.º 12
0
        public Type GetValidatedType(Type collectorType)
        {
            ArgumentUtility.CheckNotNull("collectorType", collectorType);

            if (collectorType.IsDefined(typeof(ApplyWithMixinAttribute), false))
            {
                return(AttributeUtility.GetCustomAttribute <ApplyWithMixinAttribute> (collectorType, false).MixinType);
            }
            return(_validatedTypeResolver.GetValidatedType(collectorType));
        }
        public void NoPublicWrapperGeneratedForInfrastructureMembers()
        {
            Type type = ((IMixinTarget)CreateMixedObject <BaseType1> (typeof(MixinWithProtectedOverrider))).Mixins[0].GetType();
            IEnumerable <MethodInfo> wrappedMethods =
                from method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                let attribute = AttributeUtility.GetCustomAttribute <GeneratedMethodWrapperAttribute> (method, false)
                                let declaringType = attribute != null?attribute.ResolveReferencedMethod().DeclaringType                                                           : null
                                                    let declaringTypeDefinition = declaringType != null && declaringType.IsGenericType ? declaringType.GetGenericTypeDefinition() : declaringType
                                                                                      where attribute != null && (declaringTypeDefinition == typeof(Mixin <>) || declaringTypeDefinition == typeof(Mixin <,>))
                                                                                  select method;

            Assert.That(wrappedMethods.ToArray(), Is.Empty);
        }
        private MemberVisibility GetVisibility(MemberInfo implementingMemberInfo)
        {
            MemberVisibilityAttribute visibilityAttribute = AttributeUtility.GetCustomAttribute <MemberVisibilityAttribute> (implementingMemberInfo, false);

            if (visibilityAttribute != null)
            {
                return(visibilityAttribute.Visibility);
            }
            else
            {
                return(_defaultVisibility);
            }
        }
Exemplo n.º 15
0
        public EntityNameDefinition GetTableName(ClassDefinition classDefinition)
        {
            ArgumentUtility.CheckNotNull("classDefinition", classDefinition);

            var tableAttribute = AttributeUtility.GetCustomAttribute <DBTableAttribute> (classDefinition.ClassType, false);

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

            return(new EntityNameDefinition(null, String.IsNullOrEmpty(tableAttribute.Name) ? classDefinition.ID : tableAttribute.Name));
        }
Exemplo n.º 16
0
        public void GetPublicMethodWrapper_HasAttribute()
        {
            var classEmitter = new CustomClassEmitter(Scope, "GetPublicMethodWrapper_HasAttribute", typeof(ClassWithProtectedMethod));

            classEmitter.GetPublicMethodWrapper(typeof(ClassWithProtectedMethod).GetMethod("GetSecret", _declaredInstanceBindingFlags));

            object     instance      = Activator.CreateInstance(classEmitter.BuildType());
            MethodInfo publicWrapper = instance.GetType().GetMethod("__wrap__GetSecret");

            var attribute = AttributeUtility.GetCustomAttribute <GeneratedMethodWrapperAttribute> (publicWrapper, false);

            Assert.That(attribute, Is.Not.Null);
        }
 private XmlSerializer GetXmlSerializer(Type concreteType)
 {
     return(_attributeOverridesCache.GetOrCreateValue(
                concreteType,
                delegate(Type type)
     {
         return new XmlSerializer(
             type,
             CreateAttributeOverrides(type),
             new Type[0],
             AttributeUtility.GetCustomAttribute <XmlRootAttribute> (type, true),
             null);
     }));
 }
Exemplo n.º 18
0
        public IEnumerable <IPropertyValidator> GetHardConstraintPropertyValidators()
        {
            var nullableAttribute = AttributeUtility.GetCustomAttribute <INullablePropertyAttribute> (_implementationProperty, false);

            if (nullableAttribute != null && !nullableAttribute.IsNullable)
            {
                yield return(new NotNullValidator());

                if (ReflectionUtility.IsObjectList(_implementationProperty.PropertyType))
                {
                    yield return(new NotEmptyValidator(GetDefaultValue(_implementationProperty.PropertyType)));
                }
            }
        }
Exemplo n.º 19
0
        private static string GetMessage(Rule rule)
        {
            MethodInfo method    = rule.Method;
            var        attribute = AttributeUtility.GetCustomAttribute <DelegateRuleDescriptionAttribute> (method, true);

            if (attribute == null || attribute.Message == null)
            {
                return(FormatMessage(rule.Method.Name));
            }
            else
            {
                return(attribute.Message);
            }
        }
Exemplo n.º 20
0
        private static string GetRuleName(Rule rule)
        {
            MethodInfo method    = rule.Method;
            var        attribute = AttributeUtility.GetCustomAttribute <DelegateRuleDescriptionAttribute> (method, true);

            if (attribute == null || attribute.RuleName == null)
            {
                return(method.DeclaringType.FullName + "." + rule.Method.Name);
            }
            else
            {
                return(attribute.RuleName);
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Returns the types of the mixins that were used to generate <paramref name="concreteMixedType"/>. The mixins are ordered and open generic
        /// mixins are closed exactly as used by the code generation (and as defined by <see cref="TargetClassDefinition.Mixins"/>.
        /// </summary>
        /// <param name="concreteMixedType">The concrete mixed type whose mixins should be retrieved.</param>
        /// <returns>An ordered array of mixin types that directly corresponds to the mixins held by instances of the mixed type.</returns>
        public static Type[] GetOrderedMixinTypesFromConcreteType(Type concreteMixedType)
        {
            ArgumentUtility.CheckNotNull("concreteMixedType", concreteMixedType);

            var attribute = AttributeUtility.GetCustomAttribute <ConcreteMixedTypeAttribute> (concreteMixedType, true);

            if (attribute == null)
            {
                return(null);
            }
            else
            {
                return(attribute.OrderedMixinTypes);
            }
        }
Exemplo n.º 22
0
        public IEnumerable <ExtensibleEnumInfo <T> > GetValueInfosForType <T> (ExtensibleEnumDefinition <T> definition, Type typeDeclaringMethods)
            where T : ExtensibleEnum <T>
        {
            ArgumentUtility.CheckNotNull("definition", definition);
            ArgumentUtility.CheckNotNull("typeDeclaringMethods", typeDeclaringMethods);

            var methods          = typeDeclaringMethods.GetMethods(BindingFlags.Static | BindingFlags.Public);
            var extensionMethods = GetValueExtensionMethods(typeof(T), methods);

            return(from mi in extensionMethods
                   let value = (T)mi.Invoke(null, new object[] { definition })
                               let positionAttribute = AttributeUtility.GetCustomAttribute <ExtensibleEnumPositionAttribute> (mi, true)
                                                       let positionalKey = positionAttribute != null ? positionAttribute.PositionalKey : 0.0
                                                                           select new ExtensibleEnumInfo <T> (value, mi, positionalKey));
        }
Exemplo n.º 23
0
        public void GetPublicMethodWrapper_Attribute_Properties()
        {
            var classEmitter      = new CustomClassEmitter(Scope, "GetPublicMethodWrapper_Attribute_HasRightToken", typeof(ClassWithProtectedMethod));
            var methodToBeWrapped = typeof(ClassWithProtectedMethod).GetMethod("GetSecret", _declaredInstanceBindingFlags);

            classEmitter.GetPublicMethodWrapper(methodToBeWrapped);

            Type       type          = classEmitter.BuildType();
            MethodInfo publicWrapper = type.GetMethod("__wrap__GetSecret");

            var attribute = AttributeUtility.GetCustomAttribute <GeneratedMethodWrapperAttribute> (publicWrapper, false);

            Assert.That(attribute.DeclaringType, Is.EqualTo(typeof(ClassWithProtectedMethod)));
            Assert.That(attribute.MethodName, Is.EqualTo("GetSecret"));
            Assert.That(attribute.MethodSignature, Is.EqualTo(methodToBeWrapped.ToString()));

            Assert.That(attribute.ResolveReferencedMethod(), Is.EqualTo(methodToBeWrapped));
        }
Exemplo n.º 24
0
        /// <summary>
        /// Returns the <see cref="ClassContext"/> that was used as the mixin configuration when the given <paramref name="concreteMixedType"/>
        /// was created by the <see cref="TypeFactory"/>.
        /// </summary>
        /// <param name="concreteMixedType">The type whose mixin configuration is to be retrieved.</param>
        /// <returns>The <see cref="ClassContext"/> used when the given <paramref name="concreteMixedType"/> was created, or <see langword="null"/>
        /// if <paramref name="concreteMixedType"/> is no mixed type.</returns>
        /// <remarks>
        /// <para>
        /// The results of this method are cached.
        /// </para>
        /// </remarks>
        public static ClassContext GetClassContextForConcreteType(Type concreteMixedType)
        {
            ArgumentUtility.CheckNotNull("concreteMixedType", concreteMixedType);

            return(s_classContextForConcreteTypesCache.GetOrCreateValue(
                       concreteMixedType,
                       t =>
            {
                var attribute = AttributeUtility.GetCustomAttribute <ConcreteMixedTypeAttribute> (t, true);
                if (attribute == null)
                {
                    return null;
                }
                else
                {
                    return attribute.GetClassContext();
                }
            }));
        }
Exemplo n.º 25
0
        public IEnumerable <IPropertyValidator> GetAddingPropertyValidators()
        {
            var lengthConstraintAttribute = AttributeUtility.GetCustomAttribute <ILengthConstrainedPropertyAttribute> (_implementationProperty, false);

            if (lengthConstraintAttribute != null)
            {
                if (lengthConstraintAttribute.MaximumLength.HasValue)
                {
                    yield return(new LengthValidator(0, lengthConstraintAttribute.MaximumLength.Value));
                }
            }

            var nullableAttribute = AttributeUtility.GetCustomAttribute <INullablePropertyAttribute> (_implementationProperty, false);

            if (nullableAttribute != null && !nullableAttribute.IsNullable && typeof(IEnumerable).IsAssignableFrom(_implementationProperty.PropertyType) &&
                !ReflectionUtility.IsObjectList(_implementationProperty.PropertyType))
            {
                yield return(new NotEmptyValidator(GetDefaultValue(_implementationProperty.PropertyType)));
            }
        }
Exemplo n.º 26
0
        private Lazy <Tuple <ServiceProvider, Type> > GetServiceDeclaration <TService> ()
            where TService : IBusinessObjectService
        {
            return(new Lazy <Tuple <ServiceProvider, Type> > (
                       () =>
            {
                var attributeFromDeclaringType = PropertyInfo.GetCustomAttribute <IBusinessObjectServiceTypeAttribute <TService> > (true);
                if (attributeFromDeclaringType != null)
                {
                    return new Tuple <ServiceProvider, Type> (ServiceProvider.DeclaringType, attributeFromDeclaringType.Type);
                }

                var attributeFromPropertyType =
                    AttributeUtility.GetCustomAttribute <IBusinessObjectServiceTypeAttribute <TService> > (_concreteType.Value, true);
                if (attributeFromPropertyType != null)
                {
                    return new Tuple <ServiceProvider, Type> (ServiceProvider.PropertyType, attributeFromPropertyType.Type);
                }

                return new Tuple <ServiceProvider, Type> (ServiceProvider.DeclaringType, typeof(TService));
            }));
        }
        private XmlAttributeOverrides CreateAttributeOverrides(Type concreteType)
        {
            XmlAttributeOverrides attributeOverrides = new XmlAttributeOverrides();

            foreach (MemberInfo memberInfo in concreteType.FindMembers(
                         MemberTypes.Field | MemberTypes.Property,
                         BindingFlags.Instance | BindingFlags.Public,
                         delegate { return(true); },
                         null))
            {
                XmlAttributes attributes = new XmlAttributes();
                foreach (XmlElementAttribute attribute in AttributeUtility.GetCustomAttributes <XmlElementAttribute> (memberInfo, true))
                {
                    attributes.XmlElements.Add(attribute);
                }
                attributes.XmlAttribute = AttributeUtility.GetCustomAttribute <XmlAttributeAttribute> (memberInfo, true);
                attributes.XmlIgnore    = attributes.XmlAttribute == null && attributes.XmlElements.Count == 0;

                attributeOverrides.Add(concreteType, memberInfo.Name, attributes);
            }

            return(attributeOverrides);
        }
        public IEnumerable <MemberOverridePair <TMember> > Analyze(IEnumerable <TMember> overriderMembers)
        {
            ArgumentUtility.CheckNotNull("overriderMembers", overriderMembers);

            foreach (TMember member in overriderMembers)
            {
                var overrideAttribute = (IOverrideAttribute)AttributeUtility.GetCustomAttribute(member.MemberInfo, _attributeType, true);
                if (overrideAttribute != null)
                {
                    TMember baseMember = FindOverriddenMember(overrideAttribute, member);

                    if (baseMember == null)
                    {
                        string message = string.Format(
                            "The member overridden by '{0}' declared by type '{1}' could not be found. Candidates: {2}.",
                            member.MemberInfo,
                            member.DeclaringClass.FullName,
                            BuildCandidateStringForExceptionMessage(BaseMembersByName[member.Name]));
                        throw new ConfigurationException(message);
                    }
                    yield return(new MemberOverridePair <TMember> (baseMember, member));
                }
            }
        }
Exemplo n.º 29
0
        public BindableObjectClass(
            Type concreteType,
            BindableObjectProvider businessObjectProvider,
            BindableObjectGlobalizationService bindableObjectGlobalizationService,
            IEnumerable <PropertyBase> properties)
        {
            ArgumentUtility.CheckNotNull("concreteType", concreteType);
            Assertion.IsFalse(concreteType.IsValueType, "mixed types cannot be value types");
            ArgumentUtility.CheckNotNull("businessObjectProvider", businessObjectProvider);
            ArgumentUtility.CheckNotNull("bindableObjectGlobalizationService", bindableObjectGlobalizationService);
            ArgumentUtility.CheckNotNull("properties", properties);

            _targetType                         = MixinTypeUtility.GetUnderlyingTargetType(concreteType);
            _concreteType                       = concreteType;
            _businessObjectProvider             = businessObjectProvider;
            _businessObjectProviderAttribute    = AttributeUtility.GetCustomAttribute <BusinessObjectProviderAttribute> (concreteType, true);
            _properties                         = new PropertyCollection(properties);
            _bindableObjectGlobalizationService = bindableObjectGlobalizationService;

            foreach (PropertyBase property in _properties.ToArray())
            {
                property.SetReflectedClass(this);
            }
        }
Exemplo n.º 30
0
 public void TestGeneric_FromBaseWithInvalidType()
 {
     AttributeUtility.GetCustomAttribute <object> (_basePropertyWithSingleAttribute, true);
 }