Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. [assembly: PreApplicationStartMethod(typeof(MyAssembly.Initializer), "Initialize")]
  2.  
  3. namespace MyAssembly
  4. {
  5. public static class Initializer
  6. {
  7. public static void Initialize()
  8. {
  9. TranslationKeys.Initialize();
  10. }
  11. }
  12. }
  13.  
  14. using System;
  15. using System.Web;
  16. using System.Reflection;
  17.  
  18. public class TestClass {
  19. public static void TestPreStartInitMethodLocation(Assembly assembly) {
  20. var attributes = (PreApplicationStartMethodAttribute[])assembly.GetCustomAttributes(typeof(PreApplicationStartMethodAttribute), inherit: true);
  21.  
  22. if (attributes != null && attributes.Length != 0) {
  23. PreApplicationStartMethodAttribute attribute = attributes[0];
  24.  
  25. MethodInfo method = null;
  26. // They must be in the same assembly!
  27. if (attribute.Type != null && !String.IsNullOrEmpty(attribute.MethodName) && attribute.Type.Assembly == assembly) {
  28. method = FindPreStartInitMethod(attribute.Type, attribute.MethodName);
  29. }
  30.  
  31. if (method == null) {
  32. throw new HttpException("Couldn't find attribute");
  33. }
  34. }
  35. }
  36.  
  37. public static MethodInfo FindPreStartInitMethod(Type type, string methodName) {
  38. MethodInfo method = null;
  39. if (type.IsPublic) {
  40. method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase,
  41. binder: null,
  42. types: Type.EmptyTypes,
  43. modifiers: null);
  44. }
  45. return method;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement