Exemple #1
0
        private static string GetAttributePropertyValue(IAttributeInfo attributeInfo, string propertyName)
        {
            Attribute    attribute = (Attribute)attributeInfo.Resolve(false);
            PropertyInfo property  = attributeInfo.Type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance).Resolve(false);
            object       value     = property.GetValue(attribute, null);

            if (value != null)
            {
                return(value.ToString());
            }
            return(null);
        }
        public void AttributeWrapper(Type type, int index)
        {
            SampleAttribute target = (SampleAttribute)type.GetCustomAttributes(typeof(SampleAttribute), true)[index];
            IAttributeInfo  info   = GenericCollectionUtils.ToArray(GetType(type).GetAttributeInfos(Reflector.Wrap(typeof(SampleAttribute)), true))[index];

            WrapperAssert.AreEquivalent(target, info, false);

            SampleAttribute resolvedAttrib = (SampleAttribute)info.Resolve(true);

            Assert.AreEqual(target.param, resolvedAttrib.param);
            Assert.AreEqual(target.Field, resolvedAttrib.Field);
            Assert.AreEqual(target.Property, resolvedAttrib.Property);

            try
            {
                WrapperAssert.AreEquivalent(typeof(SampleAttribute).GetConstructors()[0], info.Constructor, false);
            }
            catch (NotSupportedException)
            {
                // This is also acceptable behavior.
            }
            Dictionary <IFieldInfo, object> fieldValues = new Dictionary <IFieldInfo, object>();

            foreach (KeyValuePair <IFieldInfo, ConstantValue> entry in info.InitializedFieldValues)
            {
                fieldValues.Add(entry.Key, entry.Value.Resolve(true));
            }

            Dictionary <IPropertyInfo, object> propertyValues = new Dictionary <IPropertyInfo, object>();

            foreach (KeyValuePair <IPropertyInfo, ConstantValue> entry in info.InitializedPropertyValues)
            {
                propertyValues.Add(entry.Key, entry.Value.Resolve(true));
            }

            if (target.param == typeof(int))
            {
                try
                {
                    object[] values = GenericCollectionUtils.ConvertAllToArray <ConstantValue, object>(info.InitializedArgumentValues,
                                                                                                       delegate(ConstantValue constantValue) { return(constantValue.Resolve(true)); });

                    Assert.AreElementsEqual(new object[] { typeof(int) }, values);
                }
                catch (NotSupportedException)
                {
                    // This is also acceptable behavior.
                }

                if (fieldValues.Count != 0)
                {
                    Assert.AreEqual(1, fieldValues.Count, "The implementation may return values for uninitialized fields, but there is only one such field.");
                    Assert.AreEqual(0, fieldValues[GetField(typeof(SampleAttribute).GetField("Field"))]);
                }

                if (propertyValues.Count != 0)
                {
                    Assert.AreEqual(1, propertyValues.Count, "The implementation may return values uninitialized properties, but there is only one such field.");
                    Assert.AreEqual(null, propertyValues[GetProperty(typeof(SampleAttribute).GetProperty("Property"))]);
                }
            }
            else
            {
                try
                {
                    object[] values = GenericCollectionUtils.ConvertAllToArray <ConstantValue, object>(info.InitializedArgumentValues,
                                                                                                       delegate(ConstantValue constantValue) { return(constantValue.Resolve(true)); });

                    Assert.AreElementsEqual(new object[] { typeof(string[]) }, values);
                }
                catch (NotSupportedException)
                {
                    // This is also acceptable behavior.
                }


                Assert.AreElementsEqual(new KeyValuePair <IFieldInfo, object>[] {
                    new KeyValuePair <IFieldInfo, object>(GetField(typeof(SampleAttribute).GetField("Field")), 2)
                }, fieldValues);

                Assert.AreElementsEqual(new KeyValuePair <IPropertyInfo, object>[] {
                    new KeyValuePair <IPropertyInfo, object>(GetProperty(typeof(SampleAttribute).GetProperty("Property")), "foo")
                }, propertyValues);
            }
        }
 public T GetInstance <T>() where T : Attribute
 {
     return((T)target.Resolve(false));
 }
 private static string GetAttributePropertyValue(IAttributeInfo attributeInfo, string propertyName)
 {
     Attribute attribute = (Attribute)attributeInfo.Resolve(false);
     PropertyInfo property = attributeInfo.Type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance).Resolve(false);
     object value = property.GetValue(attribute, null);
     if (value != null)
         return value.ToString();
     return null;
 }