예제 #1
0
 internal static IEnumerable <TestFeature> GetFeatures(IMemberInfo member)
 {
     foreach (var cattr in member.GetCustomAttributes <TestFeatureAttribute> ())
     {
         yield return(cattr.Feature);
     }
 }
예제 #2
0
        internal static IEnumerable <TestCategory> GetCategories(IMemberInfo member)
        {
            var cattrs = member.GetCustomAttributes <TestCategoryAttribute> ();

            if (cattrs.Count() == 0)
            {
                return(null);
            }
            return(cattrs.Select(c => c.Category));
        }
예제 #3
0
        public static IEnumerable <CustomAttributeBuilder> GetNonInheritableAttributes(this IMemberInfo member)
        {
            Debug.Assert(member != null, "member != null");
            var attributes =
#if SILVERLIGHT
                member.GetCustomAttributes(false);
#else
                CustomAttributeData.GetCustomAttributes(member);
#endif

            foreach (var attribute in attributes)
            {
                var attributeType =
#if SILVERLIGHT
                    attribute.GetType();
#else
                    attribute.Constructor.DeclaringType;
#endif
                if (ShouldSkipAttributeReplication(attributeType))
                {
                    continue;
                }

                CustomAttributeBuilder builder;
                try
                {
                    builder = CreateBuilder(attribute
#if SILVERLIGHT
                                            as Attribute
#endif
                                            );
                }
                catch (ArgumentException e)
                {
                    var message =
                        string.Format(
                            "Due to limitations in CLR, DynamicProxy was unable to successfully replicate non-inheritable attribute {0} on {1}{2}. To avoid this error you can chose not to replicate this attribute type by calling '{3}.Add(typeof({0}))'.",
                            attributeType.FullName, (member.ReflectedType() == null) ? "" : member.ReflectedType().FullName,
#if !NETFX_CORE
                            (member is Type) ? "" : ("." + member.Name), typeof(AttributesToAvoidReplicating).FullName
#else
                            // @mbrit - 2012-05-30 - thought a ToString might be OK here?
                            member
#endif
                            );
                    throw new ProxyGenerationException(message, e);
                }
                if (builder != null)
                {
                    yield return(builder);
                }
            }
        }
예제 #4
0
        private static void AssertAttributes <TAttribute>(
            MemberInfo expected,
            IMemberInfo actual,
            Action <TAttribute, TAttribute> assertItem,
            Func <IEnumerable <TAttribute>, IEnumerable <TAttribute> > reorderDiscoveredAttributes)
            where TAttribute : Attribute
        {
            var expectedAttributes = reorderDiscoveredAttributes(expected.GetCustomAttributes <TAttribute>());
            var actualAttributes   = reorderDiscoveredAttributes(actual.GetCustomAttributes <TAttribute>());

            Assert.Equal(expectedAttributes, actualAttributes, new DelegateAssertion <TAttribute>(assertItem));
        }
예제 #5
0
 public IEnumerable <IAttribute> GetAttributes(IMemberInfo memberInfo, IAttributeDescriptor descriptor)
 {
     if (memberInfo == null)
     {
         throw new ArgumentNullException(nameof(memberInfo));
     }
     if (descriptor == null)
     {
         throw new ArgumentNullException(nameof(descriptor));
     }
     return(memberInfo.GetCustomAttributes <Attribute>().Where(attribute => MatchTypeName(attribute.GetType(), descriptor.FullTypeName)).Select(attribute => _attributeFactory(descriptor, attribute)).Where(attribute => MatchAttribute(attribute, descriptor)));
 }
        private static bool ShouldSkipDescriptorCreation(bool designTime, IMemberInfo memberInfo)
        {
            if (designTime)
            {
                var editorBrowsableAttribute = memberInfo
                                               .GetCustomAttributes <EditorBrowsableAttribute>()
                                               .FirstOrDefault();

                return(editorBrowsableAttribute != null &&
                       editorBrowsableAttribute.State == EditorBrowsableState.Never);
            }

            return(false);
        }
예제 #7
0
        static TestHost ResolveParameter(
            ReflectionTestFixtureBuilder fixture, IMemberInfo member)
        {
            if (typeof(ITestInstance).GetTypeInfo().IsAssignableFrom(member.Type))
            {
                var hostAttr = member.GetCustomAttribute <TestHostAttribute> ();
                if (hostAttr == null)
                {
                    hostAttr = member.Type.GetCustomAttribute <TestHostAttribute> ();
                }
                if (hostAttr == null)
                {
                    throw new InternalErrorException();
                }

                return(CreateCustomHost(fixture.Type, member, hostAttr));
            }

            var paramAttrs = member.GetCustomAttributes <TestParameterAttribute> ().ToArray();

            if (paramAttrs.Length == 1)
            {
                return(CreateParameterAttributeHost(fixture.Type, member, paramAttrs[0]));
            }
            else if (paramAttrs.Length > 1)
            {
                throw new InternalErrorException();
            }

            var typeAttr = GetCustomAttributeForType <TestParameterAttribute> (member.Type);

            if (typeAttr != null)
            {
                return(CreateParameterAttributeHost(fixture.Type, member, typeAttr));
            }

            if (member.Type.AsType().Equals(typeof(bool)))
            {
                return(CreateBoolean(member));
            }

            if (member.Type.IsEnum)
            {
                return(CreateEnum(member));
            }

            throw new InternalErrorException();
        }
예제 #8
0
        /// <summary>
        /// Initializes a new <see cref="ReflectedMemberProperty{TContainer,TValue}"/> instance. This is an internal constructor.
        /// </summary>
        /// <param name="info">The reflected info object backing this property.</param>
        /// <param name="name">Use this name property--this might override the MemberInfo name</param>
        /// <param name="externalContainerType">The external type which should be wrapped, if any</param>
        ReflectedExternalMemberProperty(IMemberInfo info, string name, string externalContainerType)
        {
            Name   = name;
            m_Info = info;
            if (!string.IsNullOrEmpty(externalContainerType))
            {
                m_ExternalContainerType = Type.GetType(externalContainerType);
            }

            var memberType = info.ValueType;

            if (memberType != typeof(TValue))
            {
                m_ExternalMemberType = memberType;
            }

            AddAttributes(info.GetCustomAttributes());
            var isReadOnly = m_Info.IsReadOnly || HasAttribute <ReadOnlyAttribute>();

            IsReadOnly = isReadOnly;
        }
 protected override bool MemberRequiresProcessing(IMemberInfo <PropertyInfo> member)
 {
     return(member.GetCustomAttributes(typeof(ParameterAttribute), true).Length > 0);
 }
 /// <summary>
 /// Determine whether a member should be processed.
 /// </summary>
 /// <param name="member">The member to determine processing.</param>
 /// <returns>true if the member needs processing; otherwise, false.</returns>
 protected override bool MemberRequiresProcessing(IMemberInfo <MethodInfo> member)
 {
     return(member.GetCustomAttributes(typeof(TInjectionMethodAttribute), true).Length > 0);
 }
        private static bool ShouldSkipDescriptorCreation(bool designTime, IMemberInfo memberInfo)
        {
            if (designTime)
            {
                var editorBrowsableAttribute = memberInfo
                    .GetCustomAttributes<EditorBrowsableAttribute>()
                    .FirstOrDefault();

                return editorBrowsableAttribute != null &&
                    editorBrowsableAttribute.State == EditorBrowsableState.Never;
            }

            return false;
        }
예제 #12
0
 public ReflectedArrayProperty(IMemberInfo info)
 {
     m_Info     = info;
     Attributes = new PropertyAttributeCollection(info.GetCustomAttributes().ToArray());
     IsReadOnly = Attributes.HasAttribute <ReadOnlyAttribute>() || !info.CanWrite();
 }
예제 #13
0
        /// <summary>
        /// Initializes a new <see cref="ReflectedMemberProperty{TContainer,TValue}"/> instance. This is an internal constructor.
        /// </summary>
        /// <param name="info">The reflected info object backing this property.</param>
        internal ReflectedMemberProperty(IMemberInfo info)
        {
            m_Info = info;
            m_IsStructContainerType = RuntimeTypeInfoCache <TContainer> .IsValueType;

            AddAttributes(info.GetCustomAttributes());
            var isReadOnly = m_Info.IsReadOnly || HasAttribute <ReadOnlyAttribute>();

            IsReadOnly = isReadOnly;

            if (m_Info is FieldMember fieldMember)
            {
                // TODO: optimize for NET_STANDARD, where DynamicMethod is not available by default
#if NET_4_6 && !ENABLE_IL2CPP
                var fieldInfo = fieldMember.m_FieldInfo;

                // getter
                var dynamicMethod = new DynamicMethod(string.Empty, fieldInfo.FieldType, new Type[]
                {
                    m_IsStructContainerType?fieldInfo.ReflectedType.MakeByRefType() : fieldInfo.ReflectedType
                }, true);

                var ilGenerator = dynamicMethod.GetILGenerator();
                ilGenerator.Emit(OpCodes.Ldarg_0);
                ilGenerator.Emit(OpCodes.Ldfld, fieldInfo);
                ilGenerator.Emit(OpCodes.Ret);

                if (m_IsStructContainerType)
                {
                    m_GetStructValueAction = (GetStructValueAction)dynamicMethod.CreateDelegate(typeof(GetStructValueAction));
                }
                else
                {
                    m_GetClassValueAction = (GetClassValueAction)dynamicMethod.CreateDelegate(typeof(GetClassValueAction));
                }

                // settter
                if (!isReadOnly)
                {
                    dynamicMethod = new DynamicMethod(string.Empty, typeof(void), new Type[]
                    {
                        m_IsStructContainerType?fieldInfo.ReflectedType.MakeByRefType() : fieldInfo.ReflectedType,
                            fieldInfo.FieldType
                    }, true);

                    ilGenerator = dynamicMethod.GetILGenerator();
                    ilGenerator.Emit(OpCodes.Ldarg_0);
                    ilGenerator.Emit(OpCodes.Ldarg_1);
                    ilGenerator.Emit(OpCodes.Stfld, fieldInfo);
                    ilGenerator.Emit(OpCodes.Ret);

                    if (m_IsStructContainerType)
                    {
                        m_SetStructValueAction = (SetStructValueAction)dynamicMethod.CreateDelegate(typeof(SetStructValueAction));
                    }
                    else
                    {
                        m_SetClassValueAction = (SetClassValueAction)dynamicMethod.CreateDelegate(typeof(SetClassValueAction));
                    }
                }
#endif
            }
            else if (m_Info is PropertyMember propertyMember)
            {
                if (m_IsStructContainerType)
                {
                    var getMethod = propertyMember.m_PropertyInfo.GetGetMethod(true);
                    m_GetStructValueAction = (GetStructValueAction)Delegate.CreateDelegate(typeof(GetStructValueAction), getMethod);
                    if (!isReadOnly)
                    {
                        var setMethod = propertyMember.m_PropertyInfo.GetSetMethod(true);
                        m_SetStructValueAction = (SetStructValueAction)Delegate.CreateDelegate(typeof(SetStructValueAction), setMethod);
                    }
                }
                else
                {
                    var getMethod = propertyMember.m_PropertyInfo.GetGetMethod(true);
                    m_GetClassValueAction = (GetClassValueAction)Delegate.CreateDelegate(typeof(GetClassValueAction), getMethod);
                    if (!isReadOnly)
                    {
                        var setMethod = propertyMember.m_PropertyInfo.GetSetMethod(true);
                        m_SetClassValueAction = (SetClassValueAction)Delegate.CreateDelegate(typeof(SetClassValueAction), setMethod);
                    }
                }
            }
        }
예제 #14
0
 static IReadOnlyCollection <TestCategoryAttribute> GetCategories(IMemberInfo member)
 {
     return(member.GetCustomAttributes <TestCategoryAttribute> ().ToList());
 }