Пример #1
0
 public IRuntimeImportContext Get(object target) => new RuntimeImportContext(this,
                                                                             ImportContext.Get(null, target.GetType()))
 {
     Target = target,
 };
        private Tuple <DependencyInjector, DependencyInjector, DependencyInjector> GetNewClassInjector(IActivatorTree tree)
        {
            var type = tree.Key.ReturnType;

            if (type.IsAbstract)
            {
                throw new Exception("Unable to locate Abstract class : " + type.Name);
            }
            if (type.IsInterface)
            {
                throw new Exception("Unable to locate Interface : " + type.Name);
            }

            DependencyInjector activator      = null;
            DependencyInjector activatorCtor  = null;
            DependencyInjector activatorAfter = null;


            var types = new Stack <Type>();
            var t     = type;


            while (t != null)
            {
                types.Push(t);
                t = t.BaseType;
            }

            while (types.Count > 0)
            {
                t = types.Pop();
                foreach (var p in t.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
                {
                    foreach (var attr in p.GetCustomAttributes <ImportAttribute>())
                    {
                        var ctx = ImportContext.Get(type, p).Get(typeof(IActivator));
                        var l   = GetLocator(new ActivatorTree(tree, ctx));
                        var a   = (IActivator)(l(RuntimeImportContext.GetStatic(null, ctx), null));

                        if (p is ConstructorInfo ci)
                        {
                            activatorCtor = a.GetActivator(GetLocator, new ActivatorTree(tree, ImportContext.Get(type, p)));
                        }
                        else
                        {
                            switch (attr.Location)
                            {
                            case InjectLocation.BeforeConstructor:
                                activator += a.GetActivator(GetLocator, new ActivatorTree(tree, ImportContext.Get(type, p)));
                                break;

                            case InjectLocation.AfterConstructor:
                                activatorAfter += a.GetActivator(GetLocator, new ActivatorTree(tree, ImportContext.Get(type, p)));
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }
            }


            if (typeof(IInitializer).IsAssignableFrom(type))
            {
                activator += (ctx, args, o) => _initializerMethodInfo.Invoke(o, new object [] { ctx, args });
            }

            foreach (var k in _initializers)
            {
                if (k.Key.IsAssignableFrom(type))
                {
                    foreach (var action in k.Value)
                    {
                        activator += action;
                    }
                }
            }

            return(new Tuple <DependencyInjector, DependencyInjector, DependencyInjector>(activator, activatorCtor, activatorAfter));
        }
Пример #3
0
 public static IRuntimeImportContext GetStatic(object target, Type importType) => GetStatic(target,
                                                                                            ImportContext.Get(target?.GetType(), importType));