.NET NUnit test - Assembly.GetEntryAssembly() is null

前端 未结 2 2041
时光说笑
时光说笑 2020-12-01 10:31

When class used Assembly.GetEntryAssembly() run in unit test, the Assembly.GetEntryAssembly() is null.

Is there some option h

2条回答
  •  死守一世寂寞
    2020-12-01 11:05

    Implement the SetEntryAssembly(Assembly assembly) method given in

    http://frostwave.googlecode.com/svn-history/r75/trunk/F2DUnitTests/Code/AssemblyUtilities.cs

    to your unit test project.

             /// 
            /// Use as first line in ad hoc tests (needed by XNA specifically)
            /// 
            public static void SetEntryAssembly()
            {
                SetEntryAssembly(Assembly.GetCallingAssembly());
            }
    
            /// 
            /// Allows setting the Entry Assembly when needed. 
            /// Use AssemblyUtilities.SetEntryAssembly() as first line in XNA ad hoc tests
            /// 
            /// Assembly to set as entry assembly
            public static void SetEntryAssembly(Assembly assembly)
            {
                AppDomainManager manager = new AppDomainManager();
                FieldInfo entryAssemblyfield = manager.GetType().GetField("m_entryAssembly", BindingFlags.Instance | BindingFlags.NonPublic);
                entryAssemblyfield.SetValue(manager, assembly);
    
                AppDomain domain = AppDomain.CurrentDomain;
                FieldInfo domainManagerField = domain.GetType().GetField("_domainManager", BindingFlags.Instance | BindingFlags.NonPublic);
                domainManagerField.SetValue(domain, manager);
            }
    

提交回复
热议问题