示例#1
0
        private void InjectObjects(Application app, IProxy proxy)
        {
            Type localClass = app.GetType();

            BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic
                                 | BindingFlags.Instance | BindingFlags.DeclaredOnly;

            foreach (FieldInfo field in localClass.GetFields(flags))
            {
                foreach (object injectAttribute in field.GetCustomAttributes(true))
                {
                    if (injectAttribute is Inject)
                    {
                        if (field.FieldType.IsInterface)
                        {
                            Inject inject = injectAttribute as Inject;
                            Debug.WriteLine(">> Interface: '" + field.FieldType.Name + "' request a injecting class: " + inject.Type.Name);

                            bool remoteSupport = false;
                            foreach (MethodInfo method in field.FieldType.GetMethods())
                            {
                                foreach (object remotableAttribute in method.GetCustomAttributes(true))
                                {
                                    if (remotableAttribute is Remotable)
                                    {
                                        remoteSupport = true;
                                        goto FoundSupport;
                                    }
                                }
                            }

FoundSupport:
                            if (remoteSupport)
                            {
                                field.SetValue(app, ProxyHandler.NewInstance(proxy, inject.Type, field.FieldType));
                            }
                            else
                            {
                                throw new InstantiationException("The injection required a interface with remotable annotation!");
                            }
                        }
                        else
                        {
                            throw new InstantiationException("The injection annotation required a object interface, not a concrete class or primitive type!");
                        }
                    }
                }
            }
        }