Ejemplo n.º 1
0
        private bool AutoRegisterViaGetInstanceFunc(Type t, DIContainerAutoRegisterableTypeAttribute autoRegisterableTypeAttribute, Type autoRegisterGetInstanceFuncAttributeType)
        {
            bool ret = false;

            IList <MethodInfo> getInstanceMethodsFound = AppDomainHelper.GetMethodsMarkedWithAttribute(t, autoRegisterGetInstanceFuncAttributeType, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

            if (getInstanceMethodsFound.Any())
            {
                var q = from mi in getInstanceMethodsFound
                        where !mi.IsStatic
                        select mi;
                if (q.Any())
                {
                    throw new InvalidOperationException(String.Format("Type: {0} marked with {1} but has a nonstatic method marked with {2}!", t.FullName, autoRegisterableTypeAttribute.GetType().Name, autoRegisterGetInstanceFuncAttributeType.Name)); //LOCSTR
                }

                Type genericFuncType = typeof(Func <>);
                foreach (var mi in getInstanceMethodsFound)
                {
                    Type          specificFuncType = genericFuncType.MakeGenericType(mi.ReturnType);
                    Func <object> d = (Func <object>)Delegate.CreateDelegate(specificFuncType, null, mi);

                    InternalRegister(mi.ReturnType, d, autoRegisterableTypeAttribute.OtherTypesWeAreDependingOn);
                }

                ret = true;
            }


            return(ret);
        }
Ejemplo n.º 2
0
        private bool AutoRegisterViaCustomRegistrationFunc(Type t, DIContainerAutoRegisterableTypeAttribute autoRegisterableTypeAttribute, Type autoRegisterCustomRegistrationFuncAttributeType)
        {
            bool ret = false;

            IList <MethodInfo> customRegistrationMethodsFound = AppDomainHelper.GetMethodsMarkedWithAttribute(t, autoRegisterCustomRegistrationFuncAttributeType, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

            if (customRegistrationMethodsFound.Any())
            {
                var q = from mi in customRegistrationMethodsFound
                        where !mi.IsStatic
                        select mi;
                if (q.Any())
                {
                    throw new InvalidOperationException(String.Format("Type: {0} marked with {1} but has a nonstatic method marked with {2}!", t.FullName, autoRegisterableTypeAttribute.GetType().Name, autoRegisterCustomRegistrationFuncAttributeType.Name)); //LOCSTR
                }

                foreach (var mi in customRegistrationMethodsFound)
                {
                    var piList = mi.GetParameters();
                    if ((piList.Length != 1) || (piList[0].ParameterType != typeof(IDIContainer)))
                    {
                        throw new InvalidOperationException(String.Format("Method {0}.{1} marked with {2}, but declared nos as 'static void method(IDIContainer)'!", mi.DeclaringType.FullName, mi.Name, autoRegisterCustomRegistrationFuncAttributeType));
                    }

                    Action <IDIContainer> customRegFunc = (Action <IDIContainer>)Delegate.CreateDelegate(typeof(Action <IDIContainer>), null, mi);
                    customRegFunc(this);
                }

                ret = true;
            }


            return(ret);
        }