Strange, we often use this function in the ASP.NET team and do not encounter this. To help debug this, can you try running the following code that does something similar to what ASP.NET does to define a method?
The best way to run this is to create a console application and put this code there. Then just call him, passing him the assembly where you see the problem. Then you will want to debug it and track it carefully to find out what is going on.
By the way, before that double check that you are putting the attribute in the same assembly that contains the class. that is, it cannot point to a type in another assembly.
Here is the code to try:
using System; using System.Web; using System.Reflection; public class TestClass { public static void TestPreStartInitMethodLocation(Assembly assembly) { var attributes = (PreApplicationStartMethodAttribute[])assembly.GetCustomAttributes(typeof(PreApplicationStartMethodAttribute), inherit: true); if (attributes != null && attributes.Length != 0) { PreApplicationStartMethodAttribute attribute = attributes[0]; MethodInfo method = null;
source share