internal void Init(ServiceResolver ServiceResolver)
        {
            var assembly = Assembly.GetExecutingAssembly();
            //emit staff

            //-----------------
            //get all classes
            Type[] info = assembly.GetTypes();

            //foreach class in assembly
            foreach (Type currClass in info)
            {
                //get all proparties
                PropertyInfo[] propartiesInClass = currClass.GetProperties();

                //foreach propartie
                foreach (PropertyInfo p in propartiesInClass)
                {
                    //get atribute of type InjectionAttribute
                    var attr = p.GetCustomAttributes(typeof(InjectServices.InjectionAttribute));

                    //if such exists
                    if (attr.Count() == 1)
                    {
                        //get the type that must be inst
                         Type typeOfPropartie = p.PropertyType;

                        // find it in ServiceDeclaration
                        foreach (KeyValuePair<Type, object> service in ServiceResolver.Services)
                        {
                            bool interfacesMatch = service.Key == typeOfPropartie;

                            if (interfacesMatch == true)
                            {
                                //get the created instance

                                // p.SetValueObjectExtension(service.Value, );
                                Type t = service.Value as Type;
                                var instance = Activator.CreateInstance(t, true);
                               //var method = p.GetGetMethod();
                               //method = OverrideGetMethodOfPropatie;
                                this.OverrideGetMethodOfPropatie(p, assembly, currClass, instance);
                                //and set it as default value of the prop
                                //MethodInfo methodInfo = p.GetGetMethod();
                                // methodInfo.
                               // p.SetValue(

                            }
                        }

                       // var instance = Activator.CreateInstance(typeof(TaxCalculator));
                       // MethodInfo getMethodInfo = p.GetGetMethod(true);
                       // MethodBody body = getMethodInfo.GetMethodBody();
                       // p.SetValue(p.Name, instance);
                    }
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            AvailableServiceImplementations services = new AvailableServiceImplementations();
            ServiceResolver res = new ServiceResolver();

            res.AddService(typeof(ITax), typeof(TaxCalculator));

            services.Init(res);
            Bank bank = new Bank();
            var x = bank.TaxCalculator;
        }