Exemple #1
0
        private MemberInfo[] GetMembers(string name, MemberTypes type, BindingFlags bindingAttr)
        {
            var members = new List <MemberInfo>();

            if ((type & MemberTypes.Constructor) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetConstructors(bindingAttr));
            }
            if ((type & MemberTypes.Event) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetEvents(bindingAttr));
            }
            if ((type & MemberTypes.Field) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetFields(bindingAttr));
            }
            if ((type & MemberTypes.Method) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetMethods(bindingAttr));
            }
            if ((type & MemberTypes.Property) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetProperties(bindingAttr));
            }
            if ((type & MemberTypes.NestedType) != 0)
            {
                AddMatchingMembers(members, name, adapter.GetNestedTypes(bindingAttr));
            }

            return(members.ToArray());
        }
Exemple #2
0
 private static void AddArrayMetadata(Type type)
 {
     if (!array_metadata.ContainsKey(type))
     {
         ArrayMetadata value = default(ArrayMetadata);
         value.IsArray = type.IsArray;
         ITypeInfo typeInfo = TypeFactory.GetTypeInfo(type);
         if (typeInfo.GetInterface("System.Collections.IList") != null)
         {
             value.IsList = true;
         }
         foreach (PropertyInfo property in typeInfo.GetProperties())
         {
             if (!(property.Name != "Item"))
             {
                 ParameterInfo[] indexParameters = property.GetIndexParameters();
                 if (indexParameters.Length == 1 && indexParameters[0].ParameterType == typeof(int))
                 {
                     value.ElementType = property.PropertyType;
                 }
             }
         }
         lock (array_metadata_lock)
         {
             try
             {
                 array_metadata.Add(type, value);
             }
             catch (ArgumentException)
             {
             }
         }
     }
 }
Exemple #3
0
 private static void AddTypeProperties(Type type)
 {
     if (!type_properties.ContainsKey(type))
     {
         ITypeInfo typeInfo            = TypeFactory.GetTypeInfo(type);
         IList <PropertyMetadata> list = new List <PropertyMetadata>();
         foreach (PropertyInfo property in typeInfo.GetProperties())
         {
             if (!(property.Name == "Item"))
             {
                 PropertyMetadata item = default(PropertyMetadata);
                 item.Info    = property;
                 item.IsField = false;
                 list.Add(item);
             }
         }
         foreach (FieldInfo field in typeInfo.GetFields())
         {
             PropertyMetadata item2 = default(PropertyMetadata);
             item2.Info    = field;
             item2.IsField = true;
             list.Add(item2);
         }
         lock (type_properties_lock)
         {
             try
             {
                 type_properties.Add(type, list);
             }
             catch (ArgumentException)
             {
             }
         }
     }
 }
Exemple #4
0
        public void FormatDescribesTheObject()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[0], 1);
            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.AreEqual("Foo<int>(1): fieldValue=2, Property=3", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>()));
        }
Exemple #5
0
 private static void AddObjectMetadata(Type type)
 {
     if (!object_metadata.ContainsKey(type))
     {
         ObjectMetadata value    = default(ObjectMetadata);
         ITypeInfo      typeInfo = TypeFactory.GetTypeInfo(type);
         if (typeInfo.GetInterface("System.Collections.IDictionary") != null)
         {
             value.IsDictionary = true;
         }
         value.Properties = new Dictionary <string, PropertyMetadata>();
         foreach (PropertyInfo property in typeInfo.GetProperties())
         {
             if (property.Name == "Item")
             {
                 ParameterInfo[] indexParameters = property.GetIndexParameters();
                 if (indexParameters.Length == 1 && indexParameters[0].ParameterType == typeof(string))
                 {
                     value.ElementType = property.PropertyType;
                 }
             }
             else if (!value.IsDictionary || !dictionary_properties_to_ignore.Contains(property.Name))
             {
                 PropertyMetadata value2 = default(PropertyMetadata);
                 value2.Info = property;
                 value2.Type = property.PropertyType;
                 value.Properties.Add(property.Name, value2);
             }
         }
         foreach (FieldInfo field in typeInfo.GetFields())
         {
             PropertyMetadata value3 = default(PropertyMetadata);
             value3.Info    = field;
             value3.IsField = true;
             value3.Type    = field.FieldType;
             value.Properties.Add(field.Name, value3);
         }
         lock (object_metadata_lock)
         {
             try
             {
                 object_metadata.Add(type, value);
             }
             catch (ArgumentException)
             {
             }
         }
     }
 }
Exemple #6
0
        public void CreateInstanceWithGenericStructInstantiationDefaultConstructor()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericStruct <int>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec  spec     = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);
            GenericStruct <int> instance = (GenericStruct <int>)spec.CreateInstance();

            Assert.AreEqual(0, instance.constructorParamValue);
            Assert.AreEqual(2, instance.fieldValue);
            Assert.AreEqual(3, instance.propertyValue);
        }
Exemple #7
0
        public void CreateInstanceWithGenericClass()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[0], 1);
            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec spec     = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);
            GenericClass <int> instance = (GenericClass <int>)spec.CreateInstance();

            Assert.AreEqual(1, instance.constructorParamValue);
            Assert.AreEqual(2, instance.fieldValue);
            Assert.AreEqual(3, instance.propertyValue);
        }
        /// <summary>
        /// Infers whether the type is a test type based on its structure.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Returns true if the type any associated patterns, if it has
        /// non-nested type members (subject to <see cref="GetMemberBindingFlags" />)
        /// with patterns, if it has generic parameters with patterns, or if any
        /// of its nested types satisfy the preceding rules.
        /// </para>
        /// </remarks>
        /// <param name="evaluator">The pattern evaluator.</param>
        /// <param name="type">The type.</param>
        /// <returns>True if the type is likely a test type.</returns>
        protected virtual bool InferTestType(IPatternEvaluator evaluator, ITypeInfo type)
        {
            if (evaluator.HasPatterns(type))
            {
                return(true);
            }

            BindingFlags bindingFlags = GetMemberBindingFlags(type);

            if (HasCodeElementWithPattern(evaluator, type.GetMethods(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetProperties(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetFields(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetEvents(bindingFlags)))
            {
                return(true);
            }

            if (type.IsGenericTypeDefinition && HasCodeElementWithPattern(evaluator, type.GenericArguments))
            {
                return(true);
            }

            if (ShouldConsumeConstructors(type) &&
                HasCodeElementWithPattern(evaluator, type.GetConstructors(ConstructorBindingFlags)))
            {
                return(true);
            }

            foreach (ITypeInfo nestedType in type.GetNestedTypes(NestedTypeBindingFlags))
            {
                if (InferTestType(evaluator, nestedType))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #9
0
        public void SpecPropertiesDescribeTheObject()
        {
            ITypeInfo        type                 = Reflector.Wrap(typeof(GenericClass <>));
            IConstructorInfo constructor          = type.GetConstructors(PublicInstance)[1];
            IParameterInfo   constructorParameter = constructor.Parameters[0];
            IFieldInfo       field                = type.GetFields(PublicInstance)[0];
            IPropertyInfo    property             = type.GetProperties(PublicInstance)[0];

            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(constructorParameter, 1);
            slotValues.Add(field, 2);
            slotValues.Add(property, 3);

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.AreSame(type, spec.Type);
            Assert.AreSame(slotValues, spec.SlotValues);
            Assert.AreSame(NullConverter.Instance, spec.Converter);
            Assert.AreEqual(typeof(GenericClass <int>), spec.ResolvedType);

            Assert.AreEqual(constructor, Reflector.Wrap(spec.ResolvedConstructor));
            Assert.AreElementsEqual(new object[] { 1 }, spec.ResolvedConstructorArguments);

            List <KeyValuePair <FieldInfo, object> > fieldValues = new List <KeyValuePair <FieldInfo, object> >(spec.ResolvedFieldValues);

            Assert.Count(1, fieldValues);
            Assert.AreEqual(field, Reflector.Wrap(fieldValues[0].Key));
            Assert.AreEqual(2, fieldValues[0].Value);

            List <KeyValuePair <PropertyInfo, object> > propertyValues = new List <KeyValuePair <PropertyInfo, object> >(spec.ResolvedPropertyValues);

            Assert.Count(1, propertyValues);
            Assert.AreEqual(property, Reflector.Wrap(propertyValues[0].Key));
            Assert.AreEqual(3, propertyValues[0].Value);
        }
        /// <summary>
        /// Consumes type members including fields, properties, methods and events.
        /// </summary>
        /// <param name="typeScope">The scope to be used as the containing scope.</param>
        /// <param name="type">The type whose members are to be consumed.</param>
        protected void ConsumeMembers(IPatternScope typeScope, ITypeInfo type)
        {
            BindingFlags bindingFlags = GetMemberBindingFlags(type);

            // TODO: We should probably process groups of members in sorted order working outwards
            //       from the base type, like an onion.
            foreach (IFieldInfo field in CodeElementSorter.SortMembersByDeclaringType(type.GetFields(bindingFlags)))
            {
                typeScope.Consume(field, false, DefaultFieldPattern);
            }

            foreach (IPropertyInfo property in CodeElementSorter.SortMembersByDeclaringType(type.GetProperties(bindingFlags)))
            {
                typeScope.Consume(property, false, DefaultPropertyPattern);
            }

            foreach (IMethodInfo method in CodeElementSorter.SortMembersByDeclaringType(type.GetMethods(bindingFlags)))
            {
                typeScope.Consume(method, false, DefaultMethodPattern);
            }

            foreach (IEventInfo @event in CodeElementSorter.SortMembersByDeclaringType(type.GetEvents(bindingFlags)))
            {
                typeScope.Consume(@event, false, DefaultEventPattern);
            }
        }