Ejemplo n.º 1
0
        public void Test1()
        {
            ReflectionTypeAnalyzer.AddCustomInjectAttribute(typeof(InjectCustomAttribute));

            Container.Bind <Bar>().AsSingle();
            Container.Bind <Foo>().AsSingle();

            var foo = Container.Resolve <Foo>();
            var bar = Container.Resolve <Bar>();

            Assert.IsEqual(foo.BarProperty, bar);
            Assert.IsEqual(foo.BarField, bar);
            Assert.IsEqual(foo.BarMethod, bar);
            Assert.IsEqual(foo.BarParam, bar);
        }
Ejemplo n.º 2
0
        static InjectTypeInfo CreateTypeInfoFromReflection(Type type)
        {
            var reflectionInfo = ReflectionTypeAnalyzer.GetReflectionInfo(type);

            var injectConstructor = ReflectionInfoTypeInfoConverter.ConvertConstructor(
                reflectionInfo.InjectConstructor, type);

            var injectMethods = reflectionInfo.InjectMethods.Select(
                ReflectionInfoTypeInfoConverter.ConvertMethod).ToArray();

            var memberInfos = reflectionInfo.InjectFields.Select(
                x => ReflectionInfoTypeInfoConverter.ConvertField(type, x)).Concat(
                reflectionInfo.InjectProperties.Select(
                    x => ReflectionInfoTypeInfoConverter.ConvertProperty(type, x))).ToArray();

            return(new InjectTypeInfo(
                       type, injectConstructor, injectMethods, memberInfos));
        }
        public bool TryEditType(TypeDefinition typeDef, Type actualType)
        {
            if (actualType.IsEnum || actualType.IsValueType || actualType.IsInterface ||
                actualType.HasAttribute <NoReflectionBakingAttribute>() ||
                IsStaticClass(actualType) || actualType.DerivesFromOrEqual <Delegate>() || actualType.DerivesFromOrEqual <Attribute>())
            {
                return(false);
            }

            // Allow running on the same dll multiple times without causing problems
            if (IsTypeProcessed(typeDef))
            {
                return(false);
            }

            try
            {
                var typeInfo = ReflectionTypeAnalyzer.GetReflectionInfo(actualType);

                var factoryMethod      = TryAddFactoryMethod(typeDef, typeInfo);
                var genericTypeDef     = CreateGenericInstanceWithParameters(typeDef);
                var fieldSetMethods    = AddFieldSetters(typeDef, genericTypeDef, typeInfo);
                var propertySetMethods = AddPropertySetters(typeDef, genericTypeDef, typeInfo);
                var postInjectMethods  = AddPostInjectMethods(typeDef, genericTypeDef, typeInfo);

                CreateGetInfoMethod(
                    typeDef, genericTypeDef, typeInfo,
                    factoryMethod, fieldSetMethods, propertySetMethods, postInjectMethods);
            }
            catch (Exception e)
            {
                Log.ErrorException("Error when modifying type '{0}'".Fmt(actualType), e);
                throw;
            }

            return(true);
        }
 private void CreateContainer()
 {
     container = new DiContainer();
     ReflectionTypeAnalyzer.AddCustomInjectAttribute(typeof(ImportAttribute));
 }