Esempio n. 1
0
        public static InjectionInfo Parse(Type type, BindingFlags bindingFlags, InjectorFlags injectorFlags)
        {
            var info = new InjectionInfo();

            if (bindingFlags.Has(BindingFlags.SetProperty))
            {
                ParsePropertyAttributes <Inject>(type, info);
            }

            if (bindingFlags.Has(BindingFlags.SetField))
            {
                ParseFieldAttributes <Inject>(type, info);
            }

            if (!injectorFlags.Has(InjectorFlags.PreventPostInjection))
            {
                ParseMethodAttributes <PostInjection>(type, info);
            }

            if (!injectorFlags.Has(InjectorFlags.PreventCleanup))
            {
                ParseMethodAttributes <Cleanup>(type, info);
            }

            return(info);
        }
Esempio n. 2
0
        private static void ParsePropertyAttributes <T> (Type type, InjectionInfo info)
        {
            var properties = type.GetProperties(BINDING_FLAGS);

            for (var i = 0; i < properties.Length; i++)
            {
                var property = properties[i];
                ParseAttributes <T>(property, property.PropertyType, info);
            }
        }
Esempio n. 3
0
        private static void ParseFieldAttributes <T> (Type type, InjectionInfo info)
        {
            var fields = type.GetFields(BINDING_FLAGS);

            for (var i = 0; i < fields.Length; i++)
            {
                var field = fields[i];
                ParseAttributes <T>(field, field.FieldType, info);
            }
        }
        static void RenderPostInjections(List <string> postInjectionBuilder, InjectionInfo info, string instanceName, int indent)
        {
            const string POST_INJECTION_CALL = "{0}.{1}();";

            var postInjections = info.GetCalls <PostInjection>();

            if (postInjections != null && postInjections.Count > 0)
            {
                foreach (var postInjection in postInjections)
                {
                    AppendLine(postInjectionBuilder, string.Format(POST_INJECTION_CALL, instanceName, postInjection.Name), indent);
                }
            }
        }
        static void RenderCleanups(List <string> cleanupBuilder, InjectionInfo info, string instanceName, int indent)
        {
            const string CLEANUP_REGISTRATION = "context.OnCleanUp.AddListener({0}.{1});";

            var cleanups = info.GetCalls <Cleanup>();

            if (cleanups != null && cleanups.Count > 0)
            {
                foreach (var cleanup in cleanups)
                {
                    AppendLine(cleanupBuilder, string.Format(CLEANUP_REGISTRATION, instanceName, cleanup.Name), indent);
                }
            }
        }
Esempio n. 6
0
        private static void ParseMethodAttributes <T> (Type type, InjectionInfo info) where T : Attribute
        {
            var methods = type.GetMethods();

            for (var i = 0; i < methods.Length; i++)
            {
                var method     = methods[i];
                var attributes = method.GetCustomAttributes(typeof(T), true);

                if (attributes.Length > 0)
                {
                    info.GetCalls <T>(true).Add(method);
                }
            }
        }
Esempio n. 7
0
        private static void ParseAttributes <T> (MemberInfo memberInfo, Type type, InjectionInfo info)
        {
            var customAttributes = memberInfo.GetCustomAttributes(typeof(T), true);
            var count            = customAttributes.Length;

            if (count == 0)
            {
                return;
            }

            if (count == 1 && info.AddInjection(memberInfo.Name, type))
            {
                return;
            }

            throw new AlreadyInjectedException("duplicate injection of " + type + " in " + memberInfo.ReflectedType);
        }
        static void RenderInjections(IDictionary <Type, string> typeVarMap, List <string> assignmentBuilder, InjectionInfo info,
                                     string instanceName, int indent)
        {
            const string ASSIGNMENT  = "{0}.{1} = {2};";
            const string CONTEXT_GET = "context.Get<{0}>()";

            var injections = info.Injections;

            if (injections != null && injections.Count > 0)
            {
                foreach (var injection in injections)
                {
                    var    injectedType = injection.Value;
                    string injectedName;

                    if (!typeVarMap.TryGetValue(injectedType, out injectedName))
                    {
                        injectedName = string.Format(CONTEXT_GET, injectedType.FullName);
                    }

                    AppendLine(assignmentBuilder, string.Format(ASSIGNMENT, instanceName, injection.Key, injectedName), indent);
                }

                AppendLine(assignmentBuilder);
            }
        }