コード例 #1
0
 public static void WalkAssembly(Assembly assembly)
 {
     foreach (Type type in ReflectUtil.GetLoadedTypes(assembly))
     {
         foreach (AssemblyWalkAttribute walkAttribute in AllWalkAttributes.Keys)
         {
             if (!string.IsNullOrEmpty(walkAttribute.StaticRegisterMethod))
             {
                 Type managerType           = AllWalkAttributes[walkAttribute];
                 var  managerRegisterMethod = managerType.GetMethod(walkAttribute.StaticRegisterMethod, BindingFlags.Static | BindingFlags.Public);
                 if (managerRegisterMethod != null)
                 {
                     if (walkAttribute.AttributeType != null)
                     {
                         var attr = type.GetCustomAttributes(walkAttribute.AttributeType, false).FirstOrDefault();
                         if (attr != null)
                         {
                             if (walkAttribute.InterfaceType != null)
                             {
                                 bool isInterface = !type.IsInterface && type.GetInterfaces().Contains(walkAttribute.InterfaceType);
                                 if (isInterface)
                                 {
                                     managerRegisterMethod.Invoke(null, new[] { attr, type });
                                     walkAttribute.LoadedCount++;
                                 }
                                 else
                                 {
                                     string message = string.Format("Attribute {0} found on type {1}, but type does not implement the required interface {2}",
                                                                    walkAttribute.AttributeType.Name,
                                                                    type.Name,
                                                                    walkAttribute.InterfaceType.Name);
                                     SafeHouse.Logger.LogError(message);
                                     Debug.AddNagMessage(Debug.NagType.NAGONCE, message);
                                 }
                             }
                             else if (walkAttribute.InherritedType != null)
                             {
                                 if (walkAttribute.InherritedType.IsAssignableFrom(type))
                                 {
                                     managerRegisterMethod.Invoke(null, new[] { attr, type });
                                     walkAttribute.LoadedCount++;
                                 }
                                 else
                                 {
                                     string message = string.Format("Attribute {0} found on type {1}, but type does not inherrit from the required type {2}",
                                                                    walkAttribute.AttributeType.Name,
                                                                    type.Name,
                                                                    walkAttribute.InherritedType.Name);
                                     SafeHouse.Logger.LogError(message);
                                 }
                             }
                             else
                             {
                                 managerRegisterMethod.Invoke(null, new[] { attr, type });
                                 walkAttribute.LoadedCount++;
                             }
                         }
                     }
                     else if (walkAttribute.InterfaceType != null)
                     {
                         bool isInterface = !type.IsInterface && type.GetInterfaces().Contains(walkAttribute.InterfaceType);
                         if (isInterface)
                         {
                             managerRegisterMethod.Invoke(null, new[] { type });
                             walkAttribute.LoadedCount++;
                         }
                     }
                     else if (walkAttribute.InherritedType != null)
                     {
                         if (walkAttribute.InherritedType.IsAssignableFrom(type))
                         {
                             managerRegisterMethod.Invoke(null, new[] { type });
                             walkAttribute.LoadedCount++;
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #2
0
 public static void LoadWalkAttribute(Assembly assembly)
 {
     foreach (Type type in ReflectUtil.GetLoadedTypes(assembly))
     {
         var attr = type.GetCustomAttributes(typeof(AssemblyWalkAttribute), true).FirstOrDefault() as AssemblyWalkAttribute;
         if (attr != null)
         {
             SafeHouse.Logger.LogWarning(string.Format("Found attribute on type {0}", type.Name));
             if (!string.IsNullOrEmpty(attr.StaticRegisterMethod))
             {
                 if (attr.AttributeType != null)
                 {
                     // the register method should support parameters of <AttributeType> and Type
                     if (CheckMethodParameters(type, attr.StaticRegisterMethod, attr.AttributeType, typeof(Type)))
                     {
                         AllWalkAttributes.Add(attr, type);
                         SafeHouse.Logger.SuperVerbose(string.Format("Add attribute on type {0}", type.Name));
                     }
                     else
                     {
                         string message = string.Format("Found AssemblyWalkAttribute on type {0} but the specified StaticRegisterMethod does not accept parameters of types {1} and Type",
                                                        type.Name,
                                                        attr.AttributeType.Name);
                         SafeHouse.Logger.LogError(message);
                         Debug.AddNagMessage(Debug.NagType.NAGFOREVER, message);
                     }
                 }
                 else if (attr.InherritedType != null || attr.InterfaceType != null)
                 {
                     // the register method should support only a parameter of Type
                     if (CheckMethodParameters(type, attr.StaticRegisterMethod, typeof(Type)))
                     {
                         AllWalkAttributes.Add(attr, type);
                         SafeHouse.Logger.SuperVerbose(string.Format("Add attribute on type {0}", type.Name));
                     }
                     else
                     {
                         string message = string.Format("Found AssemblyWalkAttribute on type {0} but the specified StaticRegisterMethod does not accept parameters of type Type",
                                                        type.Name);
                         SafeHouse.Logger.LogError(message);
                         Debug.AddNagMessage(Debug.NagType.NAGFOREVER, message);
                     }
                 }
             }
             else if (!string.IsNullOrEmpty(attr.StaticWalkMethod))
             {
                 // the static walk method should accept no parameters
                 if (CheckMethodParameters(type, attr.StaticWalkMethod))
                 {
                     AllWalkAttributes.Add(attr, type);
                     SafeHouse.Logger.SuperVerbose(string.Format("Add attribute on type {0}", type.Name));
                 }
                 else
                 {
                     string message = string.Format("Found AssemblyWalkAttribute on type {0} but the specified StaticWalkMethod does not accept zero parameters",
                                                    type.Name,
                                                    attr.AttributeType.Name);
                     SafeHouse.Logger.LogError(message);
                     Debug.AddNagMessage(Debug.NagType.NAGFOREVER, message);
                 }
             }
             else
             {
                 string message = string.Format("Found AssemblyWalkAttribute on type {0} but neither StaticRegisterMethod nor StaticWalkMethod are specified",
                                                type.Name);
                 SafeHouse.Logger.LogError(message);
                 Debug.AddNagMessage(Debug.NagType.NAGFOREVER, message);
             }
         }
     }
 }