コード例 #1
0
        public void TestInjectedTypeForOverrideIsCorrect()
        {
            IReflectedClass overrideBase     = reflector.Get <BaseInheritanceOverride>();
            IReflectedClass overrideExtended = reflector.Get <ExtendedInheritanceOverride>();

            Assert.AreEqual(overrideExtended.Setters[0].type, typeof(IExtendedInterface));
        }
コード例 #2
0
        public object Inject(object target, bool attemptConstructorInjection)
        {
            failIf(binder == null, "Attempt to inject into Injector without a Binder", InjectionExceptionType.NO_BINDER);
            failIf(reflector == null, "Attempt to inject without a reflector", InjectionExceptionType.NO_REFLECTOR);
            failIf(target == null, "Attempt to inject into null instance", InjectionExceptionType.NULL_TARGET);

            //Some things can't be injected into. Bail out.
            Type t = target.GetType();

            if (t.IsPrimitive || t == typeof(Decimal) || t == typeof(string))
            {
                return(target);
            }

            IReflectedClass reflection = reflector.Get(t);

            if (attemptConstructorInjection)
            {
                target = performConstructorInjection(target, reflection);
            }
            performSetterInjection(target, reflection);
            PerformFieldInjection(target, reflection);
            postInject(target, reflection);
            return(target);
        }
コード例 #3
0
ファイル: Injector.cs プロジェクト: Pikhota/CourseProject
 //Note that uninjection can only clean publicly settable points
 private void performUninjection(object target, IReflectedClass reflection)
 {
     foreach (ReflectedAttribute attr in reflection.Setters)
     {
         attr.propertyInfo.SetValue(target, null, null);
     }
 }
コード例 #4
0
        public void TestSettersOnDerivedClass()
        {
            IReflectedClass reflected = reflector.Get <InjectableDerivedClass> ();

            Assert.AreEqual(2, reflected.Setters.Length);

            bool foundIntType = false;
            bool foundClassToBeInjectedType = false;

            foreach (ReflectedAttribute attr in reflected.Setters)
            {
                if (attr.type == typeof(int))
                {
                    foundIntType = true;
                    Assert.AreEqual("intValue", attr.propertyInfo.Name);
                }
                if (attr.type == typeof(ClassToBeInjected))
                {
                    foundClassToBeInjectedType = true;
                    Assert.AreEqual("injected", attr.propertyInfo.Name);
                }
            }
            Assert.True(foundIntType);
            Assert.True(foundClassToBeInjectedType);
        }
コード例 #5
0
        public void TestNamedSetters()
        {
            IReflectedClass reflected = reflector.Get <HasNamedInjections> ();

            Assert.AreEqual(2, reflected.Setters.Length);

            int  a = 0;
            int  injectableSuperClassCount = 0;
            bool foundSomeEnum             = false;
            bool foundMarkerClass          = false;

            foreach (ReflectedAttribute attr in reflected.Setters)
            {
                if (attr.type == typeof(InjectableSuperClass))
                {
                    injectableSuperClassCount++;

                    if (attr.name.Equals(SomeEnum.ONE))
                    {
                        Assert.False(foundSomeEnum);
                        foundSomeEnum = true;
                    }
                    if (attr.name.Equals(typeof(MarkerClass)))
                    {
                        Assert.False(foundMarkerClass);
                        foundMarkerClass = true;
                    }
                }
                a++;
            }

            Assert.AreEqual(2, injectableSuperClassCount);
        }
コード例 #6
0
        private void MapMethods(IReflectedClass reflected, IBinding binding, Type type)
        {
            MethodInfo[] methods = type.GetMethods(BindingFlags.FlattenHierarchy |
                                                   BindingFlags.Public |
                                                   BindingFlags.NonPublic |
                                                   BindingFlags.Instance |
                                                   BindingFlags.InvokeMethod);
            ArrayList methodList = new ArrayList();
            List <KeyValuePair <MethodInfo, Attribute> > attrMethods = new List <KeyValuePair <MethodInfo, Attribute> >();

            foreach (MethodInfo method in methods)
            {
                object[] tagged = method.GetCustomAttributes(typeof(PostConstruct), true);
                if (tagged.Length > 0)
                {
                    methodList.Add(method);
                    attrMethods.Add(new KeyValuePair <MethodInfo, Attribute>(method, (Attribute)tagged[0]));
                }
                object[] listensToAttr = method.GetCustomAttributes(typeof(ListensTo), true);
                if (listensToAttr.Length > 0)
                {
                    for (int i = 0; i < listensToAttr.Length; i++)
                    {
                        attrMethods.Add(new KeyValuePair <MethodInfo, Attribute>(method, (ListensTo)listensToAttr[i]));
                    }
                }
            }

            methodList.Sort(new PriorityComparer());
            reflected.postConstructors = (MethodInfo[])methodList.ToArray(typeof(MethodInfo));
            reflected.attrMethods      = attrMethods.ToArray();
        }
コード例 #7
0
        public void TestMultipleSetters()
        {
            IReflectedClass reflected = reflector.Get <HasTwoInjections> ();

            Assert.AreEqual(2, reflected.Setters.Length);
            Assert.AreEqual(2, reflected.SetterNames.Length);
            Assert.IsNull(reflected.SetterNames[0]);

            bool foundStringType = false;
            bool foundInjectableSuperClassType = false;

            foreach (KeyValuePair <Type, PropertyInfo> pair in reflected.Setters)
            {
                if (pair.Key == typeof(string))
                {
                    foundStringType = true;
                    Assert.AreEqual("injectionTwo", pair.Value.Name);
                }
                if (pair.Key == typeof(InjectableSuperClass))
                {
                    foundInjectableSuperClassType = true;
                    Assert.AreEqual("injectionOne", pair.Value.Name);
                }
            }
            Assert.True(foundStringType);
            Assert.True(foundInjectableSuperClassType);
        }
コード例 #8
0
        private void mapSetters(IReflectedClass reflected, IBinding binding, Type type)
        {
            KeyValuePair <Type, PropertyInfo>[] pairs = new KeyValuePair <Type, PropertyInfo> [0];
            object[] names = new object[0];

            MemberInfo[] members = type.FindMembers(MemberTypes.Property,
                                                    BindingFlags.FlattenHierarchy |
                                                    BindingFlags.SetProperty |
                                                    BindingFlags.Public |
                                                    BindingFlags.Instance,
                                                    null, null);

            foreach (MemberInfo member in members)
            {
                object[] injections = member.GetCustomAttributes(typeof(Inject), true);
                if (injections.Length > 0)
                {
                    Inject       attr      = injections [0] as Inject;
                    PropertyInfo point     = member as PropertyInfo;
                    Type         pointType = point.PropertyType;
                    KeyValuePair <Type, PropertyInfo> pair = new KeyValuePair <Type, PropertyInfo> (pointType, point);
                    pairs = AddKV(pair, pairs);

                    object bindingName = attr.name;
                    names = Add(bindingName, names);
                }
            }
            reflected.setters     = pairs;
            reflected.setterNames = names;
        }
コード例 #9
0
        public void TestNamedSetters()
        {
            IReflectedClass reflected = reflector.Get <HasNamedInjections> ();

            Assert.AreEqual(2, reflected.Setters.Length);
            Assert.AreEqual(2, reflected.SetterNames.Length);

            int  a = 0;
            int  injectableSuperClassCount = 0;
            bool foundSomeEnum             = false;
            bool foundMarkerClass          = false;

            foreach (KeyValuePair <Type, PropertyInfo> pair in reflected.Setters)
            {
                if (pair.Key == typeof(InjectableSuperClass))
                {
                    injectableSuperClassCount++;

                    object name = reflected.SetterNames [a];
                    if (name.Equals(SomeEnum.ONE))
                    {
                        Assert.False(foundSomeEnum);
                        foundSomeEnum = true;
                    }
                    if (name.Equals(typeof(MarkerClass)))
                    {
                        Assert.False(foundMarkerClass);
                        foundMarkerClass = true;
                    }
                }
                a++;
            }

            Assert.AreEqual(2, injectableSuperClassCount);
        }
コード例 #10
0
        public void TestConstructorNamedInjection()
        {
            IReflectedClass reflected = reflector.Get <ConstructorNamedInjection>();

            Assert.That(reflected.ConstructorParameters.Length == 2);
            Assert.That(reflected.ConstructorParameterNames.Length == 2);
        }
コード例 #11
0
        public void TestSettersOnDerivedClass()
        {
            IReflectedClass reflected = reflector.Get <InjectableDerivedClass> ();

            Assert.AreEqual(2, reflected.Setters.Length);
            Assert.AreEqual(2, reflected.SetterNames.Length);

            bool foundIntType = false;
            bool foundClassToBeInjectedType = false;

            foreach (KeyValuePair <Type, PropertyInfo> pair in reflected.Setters)
            {
                if (pair.Key == typeof(int))
                {
                    foundIntType = true;
                    Assert.AreEqual("intValue", pair.Value.Name);
                }
                if (pair.Key == typeof(ClassToBeInjected))
                {
                    foundClassToBeInjectedType = true;
                    Assert.AreEqual("injected", pair.Value.Name);
                }
            }
            Assert.True(foundIntType);
            Assert.True(foundClassToBeInjectedType);
        }
コード例 #12
0
        private void mapPreferredConstructor(IReflectedClass reflected, IBinding binding, Type type)
        {
            ConstructorInfo constructor = findPreferredConstructor(type);

            if (constructor == null)
            {
                throw new ReflectionException("The reflector requires concrete classes.\nType " + type + " has no constructor. Is it an interface?",
                                              ReflectionExceptionType.CANNOT_REFLECT_INTERFACE);
            }
            ParameterInfo[] parameters = constructor.GetParameters();


            Type[]   paramList = new Type[parameters.Length];
            object[] names     = new object[parameters.Length];
            int      i         = 0;

            foreach (ParameterInfo param in parameters)
            {
                Type paramType = param.ParameterType;
                paramList [i] = paramType;

                object[] attributes = param.GetCustomAttributes(typeof(Name), false);
                if (attributes.Length > 0)
                {
                    names [i] = ((Name)attributes [0]).name;
                }
                i++;
            }
            reflected.Constructor               = constructor;
            reflected.ConstructorParameters     = paramList;
            reflected.ConstructorParameterNames = names;
        }
コード例 #13
0
ファイル: Injector.cs プロジェクト: Pikhota/CourseProject
        private object performConstructorInjection(object target, IReflectedClass reflection)
        {
            failIf(target == null, "Attempt to perform constructor injection into a null object", InjectionExceptionType.NULL_TARGET);
            failIf(reflection == null, "Attempt to perform constructor injection without a reflection", InjectionExceptionType.NULL_REFLECTION);

            ConstructorInfo constructor = reflection.constructor;

            failIf(constructor == null, "Attempt to construction inject a null constructor", InjectionExceptionType.NULL_CONSTRUCTOR);

            Type[]   parameterTypes = reflection.constructorParameters;
            object[] parameterNames = reflection.ConstructorParameterNames;
            object[] values         = new object[parameterTypes.Length];

            int i = 0;

            foreach (Type type in parameterTypes)
            {
                values[i] = getValueInjection(type, parameterNames[i], target, null);
                i++;
            }
            if (values.Length == 0)
            {
                return(target);
            }

            object constructedObj = constructor.Invoke(values);

            return((constructedObj == null) ? target : constructedObj);
        }
コード例 #14
0
        public void TestMultipleSetters()
        {
            IReflectedClass reflected = reflector.Get <HasTwoInjections> ();

            Assert.AreEqual(2, reflected.Setters.Length);
            Assert.IsNull(reflected.Setters[0].name);

            bool foundStringType = false;
            bool foundInjectableSuperClassType = false;

            foreach (ReflectedAttribute attr in reflected.Setters)
            {
                if (attr.type == typeof(string))
                {
                    foundStringType = true;
                    Assert.AreEqual("injectionTwo", attr.propertyInfo.Name);
                }
                if (attr.type == typeof(InjectableSuperClass))
                {
                    foundInjectableSuperClassType = true;
                    Assert.AreEqual("injectionOne", attr.propertyInfo.Name);
                }
            }
            Assert.True(foundStringType);
            Assert.True(foundInjectableSuperClassType);
        }
コード例 #15
0
        private void mapPostConstructors(IReflectedClass reflected, IBinding binding, Type type)
        {
            MethodInfo[] methods = type.GetMethods(
                BindingFlags.FlattenHierarchy |
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.InvokeMethod);
            ArrayList methodList = new ArrayList();

            for (int i = 0; i < methods.Length; i++)
            {
                MethodInfo info = methods[i];
                if (info == null)
                {
                    continue;
                }
                object[] tagged = info.GetCustomAttributes(typeof(PostConstruct), true);
                if (tagged.Length > 0)
                {
                    methodList.Add(info);
                }
            }

            methodList.Sort(new PriorityComparer());

            MethodInfo[] postConstructors = (MethodInfo[])methodList.ToArray(typeof(MethodInfo));
            reflected.PostConstructors = postConstructors;
        }
コード例 #16
0
        public void TestPrereflectAll()
        {
            binder.Bind <HasNamedInjections> ().To <HasNamedInjections> ();
            binder.Bind <ISimpleInterface> ().To <SimpleInterfaceImplementer> ().ToName(SomeEnum.ONE);
            binder.Bind <ISimpleInterface> ().To <PolymorphicClass> ();
            binder.Bind <InjectableSuperClass> ().To <InjectableDerivedClass> ();
            binder.Bind <int>().ToValue(42);
            binder.Bind <string>().ToValue("zaphod"); //primitives won't get reflected...

            int count = binder.ReflectAll();

            Assert.AreEqual(4, count);             //...so list length will not include primitives

            ISimpleInterface s = binder.GetInstance <ISimpleInterface> () as ISimpleInterface;

            Assert.IsTrue(s is PolymorphicClass);

            IReflectedClass reflected1 = binder.injector.reflector.Get <HasNamedInjections> ();

            Assert.True(reflected1.preGenerated);

            IReflectedClass reflected2 = binder.injector.reflector.Get <SimpleInterfaceImplementer> ();

            Assert.True(reflected2.preGenerated);

            IReflectedClass reflected3 = binder.injector.reflector.Get <PolymorphicClass> ();

            Assert.True(reflected3.preGenerated);
            Assert.AreNotEqual(reflected2.constructor, reflected3.constructor);

            IReflectedClass reflected4 = binder.injector.reflector.Get <InjectableDerivedClass> ();

            Assert.True(reflected4.preGenerated);
        }
コード例 #17
0
        public bool IsForeignKey(IReflectedProperty reflectedProperty, IReflectedClass reflectedClass)
        {
            var potentialForeignKeyNames = reflectedClass.ReflectedProperties.Where(x => x.IsComplex).Select(x => _namingConventionManager.GetForiegnKeyByComplexProperty(reflectedClass.Name, x.Name)).ToList();
            var foreignKeyNames          = reflectedClass.ReflectedProperties.Where(x => potentialForeignKeyNames.Contains(x.Name)).Select(x => x.Name).ToList();

            return(foreignKeyNames.Contains(reflectedProperty.Name));
        }
コード例 #18
0
        public IEnumerable <DynamicMethod> GetDynamicMethods(IReflectedClass reflectedClass)
        {
            var dynamicMethods = new List <DynamicMethod>();

            var reflectedMethods = reflectedClass.ReflectedMethods.Where(x => x.HasAttribute <DynamicMethodAttribute>());

            foreach (var reflectedMethod in reflectedMethods)
            {
                var dynamicMethodAttribute = reflectedMethod.GetAttribute <DynamicMethodAttribute>();
                var dynamicMethod          = new DynamicMethod();


                dynamicMethod.MethodName = reflectedMethod.Name;

                dynamicMethod.SubmitValue      = dynamicMethodAttribute.SubmitValue ?? reflectedMethod.Name;
                dynamicMethod.ButtonText       = dynamicMethodAttribute.ButtonText ?? reflectedMethod.Name;
                dynamicMethod.RedirectUrl      = dynamicMethodAttribute.RedirectUrl;
                dynamicMethod.TemplateTypeEnum = dynamicMethodAttribute.TemplateTypeEnum;
                var dynamicMethodInvokers = _dynamicMethodInvokers.Where(x => x.DynamicMethodInvokerName() == dynamicMethodAttribute.InvokerName);
                if (dynamicMethodInvokers.Count() > 1)
                {
                    throw new Exception("More than one DynamicMethodInvoker was found for InvokerName " + dynamicMethodAttribute.InvokerName + " (Method: " + reflectedMethod.Name + ")");
                }
                if (!dynamicMethodInvokers.Any())
                {
                    throw new Exception("No DynamicMethodInvoker was found for InvokerName " + dynamicMethodAttribute.InvokerName + " (Method: " + reflectedMethod.Name + ")");
                }
                dynamicMethod.DynamicMethodInvoker = dynamicMethodInvokers.Single();
                dynamicMethod.InvokeMethodFunction = reflectedMethod.ReflectedMethodOperations.InvokeFunction;
                dynamicMethods.Add(dynamicMethod);
            }

            return(dynamicMethods);
        }
コード例 #19
0
        public void TestVirtualOverrideInjection()
        {
            IReflectedClass overrideBase     = reflector.Get <BaseInheritanceOverride>();
            IReflectedClass overrideExtended = reflector.Get <ExtendedInheritanceImplied>();

            Assert.AreEqual(1, overrideBase.Setters.Length);
            Assert.AreEqual(1, overrideExtended.Setters.Length);
        }
コード例 #20
0
        public void TestReflectionCaching()
        {
            IReflectedClass reflected = reflector.Get <HasNamedInjections> ();

            Assert.False(reflected.PreGenerated);
            IReflectedClass reflected2 = reflector.Get <HasNamedInjections> ();

            Assert.True(reflected2.PreGenerated);
        }
コード例 #21
0
        private void mapSetters(IReflectedClass reflected, IBinding binding, Type type)
        {
            //MemberInfo[] privateMembers = type.FindMembers(MemberTypes.Property,
            //										BindingFlags.FlattenHierarchy |
            //										BindingFlags.SetProperty |
            //										BindingFlags.NonPublic |
            //										BindingFlags.Instance,
            //										null, null);
            //foreach (MemberInfo member in privateMembers)
            //{
            //	object[] injections = member.GetCustomAttributes(typeof(Inject), true);
            //	if (injections.Length > 0)
            //	{
            //		throw new ReflectionException ("The class " + type.Name + " has a non-public Injection setter " + member.Name + ". Make the setter public to allow injection.", ReflectionExceptionType.CANNOT_INJECT_INTO_NONPUBLIC_SETTER);
            //	}
            //}

            MemberInfo[] members = type.FindMembers(MemberTypes.Property,
                                                    BindingFlags.FlattenHierarchy |
                                                    BindingFlags.SetProperty |
                                                    BindingFlags.Public |
                                                    BindingFlags.NonPublic |
                                                    BindingFlags.Instance,
                                                    null, null);

            //propertyinfo.name to reflectedattribute
            //This is to test for 'hidden' or overridden injections.
            Dictionary <String, ReflectedAttribute> namedAttributes = new Dictionary <string, ReflectedAttribute>();

            foreach (MemberInfo member in members)
            {
                object[] injections = member.GetCustomAttributes(typeof(Inject), true);
                if (injections.Length > 0)
                {
                    Inject       attr                 = injections [0] as Inject;
                    PropertyInfo point                = member as PropertyInfo;
                    Type         baseType             = member.DeclaringType.BaseType;
                    bool         hasInheritedProperty = baseType != null?baseType.GetProperties().Any(p => p.Name == point.Name) : false;

                    bool toAddOrOverride = true;                     //add or override by default

                    //if we have an overriding value, we need to know whether to override or leave it out.
                    //We leave out the base if it's hidden
                    //And we add if its overriding.
                    if (namedAttributes.ContainsKey(point.Name))
                    {
                        toAddOrOverride = hasInheritedProperty;                         //if this attribute has been 'hidden' by a new or override keyword, we should not add this.
                    }
                    if (toAddOrOverride)
                    {
                        namedAttributes[point.Name] = new ReflectedAttribute(point.PropertyType, point, attr.name);
                    }
                }
            }
            reflected.Setters = namedAttributes.Values.ToArray();
        }
コード例 #22
0
        private void mapSetters(IReflectedClass reflected, IBinding binding, Type type)
        {
            KeyValuePair <Type, PropertyInfo>[] pairs = new KeyValuePair <Type, PropertyInfo> [0];
            object[] names = new object[0];

            MemberInfo[] privateMembers = type.FindMembers(
                MemberTypes.Property,
                BindingFlags.FlattenHierarchy |
                BindingFlags.SetProperty |
                BindingFlags.NonPublic |
                BindingFlags.Instance,
                null, null);
            for (int i = 0; i < privateMembers.Length; i++)
            {
                MemberInfo info = privateMembers[i];
                if (info == null)
                {
                    continue;
                }
                object[] injections = info.GetCustomAttributes(typeof(Inject), true);
                if (injections.Length > 0)
                {
                    throw new ReflectionException("The class " + type.Name + " has a non-public Injection setter " + info.Name + ". Make the setter public to allow injection.", ReflectionExceptionType.CANNOT_INJECT_INTO_NONPUBLIC_SETTER);
                }
            }

            MemberInfo[] members = type.FindMembers(MemberTypes.Property,
                                                    BindingFlags.FlattenHierarchy |
                                                    BindingFlags.SetProperty |
                                                    BindingFlags.Public |
                                                    BindingFlags.Instance,
                                                    null, null);

            for (int i = 0; i < members.Length; i++)
            {
                MemberInfo info = members[i];
                if (info == null)
                {
                    continue;
                }
                object[] injections = info.GetCustomAttributes(typeof(Inject), true);
                if (injections.Length > 0)
                {
                    Inject       attr      = injections[0] as Inject;
                    PropertyInfo point     = info as PropertyInfo;
                    Type         pointType = point.PropertyType;
                    KeyValuePair <Type, PropertyInfo> pair = new KeyValuePair <Type, PropertyInfo>(pointType, point);
                    pairs = AddKV(pair, pairs);

                    object bindingName = attr.name;
                    names = Add(bindingName, names);
                }
            }
            reflected.Setters     = pairs;
            reflected.SetterNames = names;
        }
コード例 #23
0
ファイル: Injector.cs プロジェクト: shdwp/unity_playground
        //Note that uninjection can only clean publicly settable points
        private void performUninjection(object target, IReflectedClass reflection)
        {
            int aa = reflection.setters.Length;

            for (int a = 0; a < aa; a++)
            {
                KeyValuePair <Type, PropertyInfo> pair = reflection.setters [a];
                pair.Value.SetValue(target, null, null);
            }
        }
コード例 #24
0
        public void TestMultipleLevelsOfInheritance()
        {
            IReflectedClass overrideBase        = reflector.Get <BaseInheritanceOverride>();
            IReflectedClass overrideExtended    = reflector.Get <ExtendedInheritanceOverride>();
            IReflectedClass overrideExtendedTwo = reflector.Get <ExtendedInheritanceOverrideTwo>();

            Assert.AreEqual(1, overrideBase.Setters.Length);
            Assert.AreEqual(1, overrideExtended.Setters.Length);
            Assert.AreEqual(1, overrideExtendedTwo.Setters.Length);
        }
コード例 #25
0
ファイル: Injector.cs プロジェクト: gellios3/My-Match-3
        //Note that uninjection can only clean publicly settable points
        private void performUninjection(object target, IReflectedClass reflection)
        {
            var aa = reflection.setters.Length;

            for (var a = 0; a < aa; a++)
            {
                var pair = reflection.setters [a];
                pair.Value.SetValue(target, null, null);
            }
        }
コード例 #26
0
ファイル: Injector.cs プロジェクト: Pikhota/CourseProject
        private void performSetterInjection(object target, IReflectedClass reflection)
        {
            failIf(target == null, "Attempt to inject into a null object", InjectionExceptionType.NULL_TARGET);
            failIf(reflection == null, "Attempt to inject without a reflection", InjectionExceptionType.NULL_REFLECTION);

            foreach (ReflectedAttribute attr in reflection.Setters)
            {
                object value = getValueInjection(attr.type, attr.name, target, attr.propertyInfo);
                injectValueIntoPoint(value, target, attr.propertyInfo);
            }
        }
コード例 #27
0
        private void PerformFieldInjection(object target, IReflectedClass reflection)
        {
            failIf(target == null, "Attempt to inject into a null object", InjectionExceptionType.NULL_TARGET);
            failIf(reflection == null, "Attempt to inject without a reflection", InjectionExceptionType.NULL_REFLECTION);

            foreach (var attr in reflection.Fields)
            {
                var value = getValueInjection(attr.Type, attr.Name, target, attr.FieldInfo);
                InjectValueIntoField(value, target, attr.FieldInfo);
            }
        }
コード例 #28
0
        public void TestSingleSetter()
        {
            IReflectedClass reflected = reflector.Get <PostConstructTwo> ();

            Assert.AreEqual(1, reflected.Setters.Length);
            Assert.IsNull(reflected.Setters[0].name);

            ReflectedAttribute attr = reflected.Setters [0];

            Assert.AreEqual(attr.type, typeof(float));
        }
コード例 #29
0
        public void TestSingleSetter()
        {
            IReflectedClass reflected = reflector.Get <PostConstructTwo> ();

            Assert.AreEqual(1, reflected.Setters.Length);
            Assert.AreEqual(1, reflected.SetterNames.Length);
            Assert.IsNull(reflected.SetterNames[0]);

            KeyValuePair <Type, PropertyInfo> pair = reflected.Setters [0];

            Assert.AreEqual(pair.Key, typeof(float));
        }
コード例 #30
0
ファイル: Injector.cs プロジェクト: Pikhota/CourseProject
        public object Instantiate(IInjectionBinding binding, bool tryInjectHere)
        {
            failIf(binder == null, "Attempt to instantiate from Injector without a Binder", InjectionExceptionType.NO_BINDER);
            failIf(factory == null, "Attempt to inject into Injector without a Factory", InjectionExceptionType.NO_FACTORY);

            armorAgainstInfiniteLoops(binding);

            object retv           = null;
            Type   reflectionType = null;

            if (binding.value is Type)
            {
                reflectionType = binding.value as Type;
            }
            else if (binding.value == null)
            {
                object[] tl = binding.key as object[];
                reflectionType = tl [0] as Type;
                if (reflectionType.IsPrimitive || reflectionType == typeof(Decimal) || reflectionType == typeof(string))
                {
                    retv = binding.value;
                }
            }
            else
            {
                retv = binding.value;
            }

            if (retv == null)             //If we don't have an existing value, go ahead and create one.
            {
                IReflectedClass reflection = reflector.Get(reflectionType);

                Type[]   parameterTypes = reflection.constructorParameters;
                object[] parameterNames = reflection.ConstructorParameterNames;

                int      aa   = parameterTypes.Length;
                object[] args = new object [aa];
                for (int a = 0; a < aa; a++)
                {
                    args [a] = getValueInjection(parameterTypes[a] as Type, parameterNames[a], reflectionType, null);
                }
                retv = factory.Get(binding, args);

                if (tryInjectHere)
                {
                    TryInject(binding, retv);
                }
            }
            infinityLock = null;             //Clear our infinity lock so the next time we instantiate we don't consider this a circular dependency

            return(retv);
        }
コード例 #31
0
ファイル: Injector.cs プロジェクト: noodle/strangeioc
        //After injection, call any methods labelled with the [PostConstruct] tag
        private void postInject(object target, IReflectedClass reflection)
        {
            failIf(target == null, "Attempt to PostConstruct a null target", InjectionExceptionType.NULL_TARGET);
            failIf(reflection == null, "Attempt to PostConstruct without a reflection", InjectionExceptionType.NULL_REFLECTION);

            MethodInfo[] postConstructors = reflection.postConstructors;
            if (postConstructors != null)
            {
                foreach(MethodInfo method in postConstructors)
                {
                    method.Invoke (target, null);
                }
            }
        }
コード例 #32
0
ファイル: Injector.cs プロジェクト: noodle/strangeioc
 //Note that uninjection can only clean publicly settable points
 private void performUninjection(object target, IReflectedClass reflection)
 {
     int aa = reflection.setters.Length;
     for(int a = 0; a < aa; a++)
     {
         KeyValuePair<Type, PropertyInfo> pair = reflection.setters [a];
         pair.Value.SetValue (target, null, null);
     }
 }
コード例 #33
0
ファイル: Injector.cs プロジェクト: noodle/strangeioc
        private void performSetterInjection(object target, IReflectedClass reflection)
        {
            failIf(target == null, "Attempt to inject into a null object", InjectionExceptionType.NULL_TARGET);
            failIf(reflection == null, "Attempt to inject without a reflection", InjectionExceptionType.NULL_REFLECTION);
            failIf(reflection.setters.Length != reflection.setterNames.Length, "Attempt to perform setter injection with mismatched names.\nThere must be exactly as many names as setters.", InjectionExceptionType.SETTER_NAME_MISMATCH);

            int aa = reflection.setters.Length;
            for(int a = 0; a < aa; a++)
            {
                KeyValuePair<Type, PropertyInfo> pair = reflection.setters [a];
                object value = getValueInjection(pair.Key, reflection.setterNames[a], target);
                injectValueIntoPoint (value, target, pair.Value);
            }
        }
コード例 #34
0
ファイル: Injector.cs プロジェクト: noodle/strangeioc
        private void performPseudoConstructorInjection(object target, IReflectedClass reflection)
        {
            if (reflection.PseudoConstructor == null)
            {
                return;
            }

            failIf(target == null, "Attempt to perform pseudo-constructor injection into a null object", InjectionExceptionType.NULL_TARGET);
            failIf(reflection == null, "Attempt to perform pseudo-constructor injection without a reflection", InjectionExceptionType.NULL_REFLECTION);

            MethodInfo pseudoConstructor = reflection.PseudoConstructor;
            Type[] parameterTypes = reflection.PseudoConstructorParameters;
            object[] parameterNames = reflection.PseudoConstructorParameterNames;
            object[] values = new object[parameterTypes.Length];

            int i = 0;
            foreach (Type type in parameterTypes)
            {
                values[i] = getValueInjection(type, parameterNames[i], target);
                ++i;
            }

            pseudoConstructor.Invoke(target, values);
        }
コード例 #35
0
ファイル: Injector.cs プロジェクト: noodle/strangeioc
        private object performConstructorInjection(object target, IReflectedClass reflection)
        {
            failIf(target == null, "Attempt to perform constructor injection into a null object", InjectionExceptionType.NULL_TARGET);
            failIf(reflection == null, "Attempt to perform constructor injection without a reflection", InjectionExceptionType.NULL_REFLECTION);

            ConstructorInfo constructor = reflection.constructor;
            failIf(constructor == null, "Attempt to construction inject a null constructor", InjectionExceptionType.NULL_CONSTRUCTOR);

            Type[] parameterTypes = reflection.constructorParameters;
            object[] parameterNames = reflection.ConstructorParameterNames;
            object[] values = new object[parameterTypes.Length];

            int i = 0;
            foreach (Type type in parameterTypes)
            {
                values[i] = getValueInjection(type, parameterNames[i], target);
                i++;
            }
            if (values.Length == 0)
            {
                return target;
            }

            object constructedObj = constructor.Invoke (values);
            return (constructedObj == null) ? target : constructedObj;
        }