public bool PosTest2()
    {
        bool retVal = true;
        bool expectedValue = false;

        AttributeUsageAttribute aUT = new AttributeUsageAttribute(AttributeTargets.All);

        TestLibrary.TestFramework.BeginScenario("PosTest2:get and set AllowMultiple as false.");
        try
        {
            aUT.AllowMultiple = expectedValue;
            if (expectedValue != aUT.AllowMultiple)
            {
                TestLibrary.TestFramework.LogError("003", "ExpectedValue(" + aUT.AllowMultiple + ") !=ActualValue(" + expectedValue + ")");
                retVal = false;
            }

        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("004", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }
    public bool PosTest1()
    {
        bool retVal = true;

        AttributeTargets validOn = AttributeTargets.All;

        TestLibrary.TestFramework.BeginScenario("PosTest1:set ValidOn as AttributeTargets.All and create a instance of class AttributeUsageAttribute.");
        try
        {
            AttributeUsageAttribute aUT = new AttributeUsageAttribute(validOn);
            if (aUT == null)
            {
                TestLibrary.TestFramework.LogError("001", "ExpectedObject(Not null) !=Actual(null)");
                retVal = false;
            }

        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("002", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }
    public bool PosTest17()
    {
        bool retVal = true;

        AttributeTargets expectedValue = (AttributeTargets)(TestLibrary.Generator.GetInt32(-55) % (UInt16.MaxValue + 1));

        TestLibrary.TestFramework.BeginScenario("PosTest17:set ValidOn as Random int16 and try to get it.");
        try
        {
            AttributeUsageAttribute aUT = new AttributeUsageAttribute(expectedValue);
            
            if (aUT == null)
            {
                TestLibrary.TestFramework.LogError("033", "ExpectedObject(Not null) !=Actual(null)");
                retVal = false;
            }

        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("034", "Unexpected exception:" + e);
            retVal = false;
        }

        return retVal;
    }
Esempio n. 4
0
        internal static IEnumerable <IAttributeInfo> GetCustomAttributes(Type type, Type attributeType, AttributeUsageAttribute attributeUsage)
        {
            IEnumerable <IAttributeInfo> results = Enumerable.Empty <IAttributeInfo>();

            if (type != null)
            {
                results = CustomAttributeData.GetCustomAttributes(type)
                          .Where(attr => attributeType.IsAssignableFrom(attr.Constructor.ReflectedType))
                          .OrderBy(attr => attr.Constructor.ReflectedType.Name)
                          .Select(Reflector.Wrap)
                          .Cast <IAttributeInfo>();

                if (attributeUsage.Inherited && (attributeUsage.AllowMultiple || !results.Any()))
                {
                    results = results.Concat(GetCustomAttributes(type.BaseType, attributeType, attributeUsage));
                }
            }

            return(results);
        }
Esempio n. 5
0
 public AttributeInfo(AttributeUsageAttribute usage, int inheritanceLevel)
 {
     _usage            = usage;
     _inheritanceLevel = inheritanceLevel;
 }
Esempio n. 6
0
        internal static object[] GetCustomAttributes(ICustomAttributeProvider obj, Type attributeType, bool inherit)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            if (attributeType == null)
            {
                throw new ArgumentNullException(nameof(attributeType));
            }
            if (!attributeType.IsSubclassOf(typeof(Attribute)) && !attributeType.IsInterface &&
                attributeType != typeof(Attribute) && attributeType != typeof(CustomAttribute) && attributeType != typeof(object))
            {
                throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);
            }

            // FIXME: GetCustomAttributesBase doesn't like being passed a null attributeType
            if (attributeType == typeof(CustomAttribute))
            {
                attributeType = null !;
            }
            if (attributeType == typeof(Attribute))
            {
                attributeType = null !;
            }
            if (attributeType == typeof(object))
            {
                attributeType = null !;
            }

            object[] r;
            object[] res = GetCustomAttributesBase(obj, attributeType !, false);
            // shortcut
            if (!inherit && res.Length == 1)
            {
                if (res[0] == null)
                {
                    throw new CustomAttributeFormatException("Invalid custom attribute format");
                }

                if (attributeType != null)
                {
                    if (attributeType.IsAssignableFrom(res[0].GetType()))
                    {
                        r    = (object[])Array.CreateInstance(attributeType, 1);
                        r[0] = res[0];
                    }
                    else
                    {
                        r = (object[])Array.CreateInstance(attributeType, 0);
                    }
                }
                else
                {
                    r    = (object[])Array.CreateInstance(res[0].GetType(), 1);
                    r[0] = res[0];
                }
                return(r);
            }

            if (inherit && GetBase(obj) == null)
            {
                inherit = false;
            }

            // if AttributeType is sealed, and Inherited is set to false, then
            // there's no use in scanning base types
            if ((attributeType != null && attributeType.IsSealed) && inherit)
            {
                AttributeUsageAttribute usageAttribute = RetrieveAttributeUsage(
                    attributeType);
                if (!usageAttribute.Inherited)
                {
                    inherit = false;
                }
            }

            int                      initialSize = Math.Max(res.Length, 16);
            List <object>?           a           = null;
            ICustomAttributeProvider?btype       = obj;

            object[] array;

            /* Non-inherit case */
            if (!inherit)
            {
                if (attributeType == null)
                {
                    foreach (object attr in res)
                    {
                        if (attr == null)
                        {
                            throw new CustomAttributeFormatException("Invalid custom attribute format");
                        }
                    }
                    var result = new Attribute[res.Length];
                    res.CopyTo(result, 0);
                    return(result);
                }

                a = new List <object>(initialSize);
                foreach (object attr in res)
                {
                    if (attr == null)
                    {
                        throw new CustomAttributeFormatException("Invalid custom attribute format");
                    }

                    Type attrType = attr.GetType();
                    if (attributeType != null && !attributeType.IsAssignableFrom(attrType))
                    {
                        continue;
                    }
                    a.Add(attr);
                }

                if (attributeType == null || attributeType.IsValueType)
                {
                    array = new Attribute[a.Count];
                }
                else
                {
                    array = (Array.CreateInstance(attributeType, a.Count) as object[]) !;
                }
                a.CopyTo(array, 0);
                return(array);
            }

            /* Inherit case */
            var attributeInfos   = new Dictionary <Type, AttributeInfo>(initialSize);
            int inheritanceLevel = 0;

            a = new List <object>(initialSize);

            do
            {
                foreach (object attr in res)
                {
                    AttributeUsageAttribute usage;
                    if (attr == null)
                    {
                        throw new CustomAttributeFormatException("Invalid custom attribute format");
                    }

                    Type attrType = attr.GetType();
                    if (attributeType != null)
                    {
                        if (!attributeType.IsAssignableFrom(attrType))
                        {
                            continue;
                        }
                    }

                    AttributeInfo?firstAttribute;
                    if (attributeInfos.TryGetValue(attrType, out firstAttribute))
                    {
                        usage = firstAttribute.Usage;
                    }
                    else
                    {
                        usage = RetrieveAttributeUsage(attrType);
                    }

                    // only add attribute to the list of attributes if
                    // - we are on the first inheritance level, or the attribute can be inherited anyway
                    // and (
                    // - multiple attributes of the type are allowed
                    // or (
                    // - this is the first attribute we've discovered
                    // or
                    // - the attribute is on same inheritance level than the first
                    //   attribute that was discovered for this attribute type ))
                    if ((inheritanceLevel == 0 || usage.Inherited) && (usage.AllowMultiple ||
                                                                       (firstAttribute == null || (firstAttribute != null &&
                                                                                                   firstAttribute.InheritanceLevel == inheritanceLevel))))
                    {
                        a.Add(attr);
                    }

                    if (firstAttribute == null)
                    {
                        attributeInfos.Add(attrType, new AttributeInfo(usage, inheritanceLevel));
                    }
                }

                if ((btype = GetBase(btype)) != null)
                {
                    inheritanceLevel++;
                    res = GetCustomAttributesBase(btype, attributeType, true);
                }
            } while (inherit && btype != null);

            if (attributeType == null || attributeType.IsValueType)
            {
                array = new Attribute[a.Count];
            }
            else
            {
                array = (Array.CreateInstance(attributeType, a.Count) as object[]) !;
            }

            // copy attributes to array
            a.CopyTo(array, 0);

            return(array);
        }