public void AddAssembly(Assembly assembly) { Type[] types = assembly.GetTypes(); for (int i = 0; i < types.Length; i++) { Type type = types[i]; MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); for (int j = 0; j < methods.Length; j++) { MethodInfo methodInfo = methods[j]; InitializationAttribute customAttribute = methodInfo.GetCustomAttribute <InitializationAttribute>(); if (customAttribute != null) { if (type.IsGenericType) { throw new Exception("Initialization method is within a generic type."); } if (methodInfo.IsGenericMethod) { throw new Exception("Initialization method must not be generic."); } if (methodInfo.ReturnType != typeof(void)) { throw new Exception("Invalid initialization method return type."); } if (methodInfo.GetParameters().Length != 0) { throw new Exception("Invalid initialization cannot have parameters"); } if (!this.m_initializer.ContainsKey(customAttribute.Pass)) { this.m_initializer.Add(customAttribute.Pass, new List <InitializationMethod>()); } InitializationMethod initializationMethod = new InitializationMethod(customAttribute, methodInfo); if (methodInfo.IsStatic) { initializationMethod.Caller = null; } else { if (!type.IsDerivedFromGenericType(typeof(Singleton <>))) { throw new Exception("Method have to be static or class must inherit Singleton<>"); } PropertyInfo property = type.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); initializationMethod.Caller = property.GetValue(null, new object[0]); } this.m_initializer[customAttribute.Pass].Add(initializationMethod); } } } }
public InitializationMethod(InitializationAttribute attribute, MethodInfo method) { this.Attribute = attribute; this.Method = method; }