Пример #1
0
        public void FormatStringShowCommaDelimitedParameters()
        {
            ITypeInfo type = Reflector.Wrap(typeof(NonGenericClass));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[0], 1);
            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[1], "abc");

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

            Assert.AreEqual("Foo(1, \"abc\")", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>()));
        }
Пример #2
0
        protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder,
                                                              CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
        {
            foreach (var constructor in adapter.GetConstructors(bindingAttr))
            {
                if (ParameterListMatchesTypes(constructor.Parameters, types))
                {
                    return(constructor.Resolve(false));
                }
            }

            return(null);
        }
Пример #3
0
        public void AccessorCreatesNewObjectsThroughSlotBinding()
        {
            DataSource source = new DataSource("data");

            source.AddDataSet(new ItemSequenceDataSet(new IDataItem[] { new ListDataItem <object>(new object[] { 42, typeof(int) }, null, false) }, 2));

            IDataSourceResolver resolver = Mocks.StrictMock <IDataSourceResolver>();

            using (Mocks.Record())
            {
                Expect.Call(resolver.ResolveDataSource("data")).Repeat.Twice().Return(source);
            }

            using (Mocks.Playback())
            {
                ITypeInfo        type        = Reflector.Wrap(typeof(Holder <>));
                IConstructorInfo constructor = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
                ISlotInfo        valueSlot   = constructor.Parameters[0];
                ISlotInfo        typeSlot    = (IGenericParameterInfo)type.GenericArguments[0];

                DataBindingContext context = new DataBindingContext(new NullConverter());

                ObjectDataBinder binder = new ObjectDataBinder(type);
                binder.SetSlotBinder(valueSlot, new ScalarDataBinder(new DataBinding(0, null), "data"));
                binder.SetSlotBinder(typeSlot, new ScalarDataBinder(new DataBinding(1, null), "data"));

                IDataAccessor    accessor = binder.Register(context, resolver);
                List <IDataItem> items    = new List <IDataItem>(context.GetItems(true));
                Assert.Count(1, items);

                Holder <int> holder = (Holder <int>)accessor.GetValue(items[0]);
                Assert.AreEqual(42, holder.Value, "Should have set the value via the constructor parameter.");
            }
        }
Пример #4
0
 /// <summary>
 /// Determines if the type can be instantiated using a public constructor.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>True if the type can be instantiated.</returns>
 public static bool CanInstantiate(ITypeInfo type)
 {
     return type != null
         && (type.TypeAttributes & (TypeAttributes.Abstract | TypeAttributes.Class | TypeAttributes.Public)) == (TypeAttributes.Class | TypeAttributes.Public)
         && type.ElementType == null
         && type.GetConstructors(BindingFlags.Instance | BindingFlags.Public).Count != 0;
 }
Пример #5
0
 /// <summary>
 /// Determines if the type can be instantiated using a public constructor.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>True if the type can be instantiated.</returns>
 public static bool CanInstantiate(ITypeInfo type)
 {
     return(type != null &&
            (type.TypeAttributes & (TypeAttributes.Abstract | TypeAttributes.Class | TypeAttributes.Public)) == (TypeAttributes.Class | TypeAttributes.Public) &&
            type.ElementType == null &&
            type.GetConstructors(BindingFlags.Instance | BindingFlags.Public).Count != 0);
 }
Пример #6
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>()));
        }
Пример #7
0
        public void CreateInstanceWithGenericStructInstantiation()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericStruct <int>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add(type.GetConstructors(PublicInstance)[0].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);
            GenericStruct <int> instance = (GenericStruct <int>)spec.CreateInstance();

            Assert.AreEqual(1, instance.constructorParamValue);
            Assert.AreEqual(2, instance.fieldValue);
            Assert.AreEqual(3, instance.propertyValue);
        }
        private static IConstructorInfo GetFirstConstructorWithPreferenceForPublicConsructor(ITypeInfo type)
        {
            IConstructorInfo result = null;

            foreach (IConstructorInfo constructor in type.GetConstructors(ConstructorBindingFlags))
            {
                if (constructor.IsPublic)
                {
                    return(constructor);
                }

                if (result == null)
                {
                    result = constructor;
                }
            }

            return(result);
        }
        /// <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);
        }
Пример #10
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);
        }