private void RegisterTemplate(Type templateType) { ITemplate template = (ITemplate)state.aopEngine.CreateProxy(templateType); template.Context = this; template.Initialize(); this.template = (TEMPLATE)template; foreach (MethodInfo method in template.GetType().GetMethods()) { FactoryMethodAttribute attrib = method.GetCustomAttributes(typeof(FactoryMethodAttribute), true).FirstOrDefault() as FactoryMethodAttribute; if (attrib == null) { continue; //not a factory method } if (!method.IsVirtual) { throw new Exception(string.Format("Factory method '{0}.{1}' must be marked as virtual", template.GetType().Name, method.Name)); } RegisterObjectFactoryMethod(template, method, attrib); } foreach (MethodInfo method in template.GetType().GetMethods()) { ConfigurationMethodAttribute attrib = method.GetCustomAttributes(typeof(ConfigurationMethodAttribute), true).FirstOrDefault() as ConfigurationMethodAttribute; if (attrib == null) { continue; //not a configuration method } RegisterObjectConfigurationMethod(template, method, attrib); } }
private void RegisterObjectConfigurationMethod(ITemplate factory, MethodInfo method, ConfigurationMethodAttribute attrib) { Type objectType = method.ReturnType; if (method.GetParameters().Length != 1) { throw new NotSupportedException("Configuration methods must have only 1 parameter"); } ConfigureDelegate configDelegate = CreateMethodConfigurationDelegate(factory, method); Type defaultType = method.GetParameters().First().ParameterType; if (attrib.ConfigId != null) { RegisterObjectConfigurationMethod(attrib.ConfigId, configDelegate); } else if (attrib.RegisterAs == ConfigurationType.DefaultForType) { RegisterObjectConfigurationMethod(defaultType, configDelegate); } else if (attrib.RegisterAs == ConfigurationType.AppliesToAll) { RegisterObjectTemplateConfigurationMethod(defaultType, configDelegate); } else { string objectId = method.Name; RegisterObjectConfigurationMethod(objectId, configDelegate); } }