Unable to access CodeBase from a dynamically created assembly

I am trying to create an assembly dynamically in .Net. However, I cannot figure out how to get the CodeBase property to return a value. Here is an example:

var assemblyName = new AssemblyName { Name = "Whatever", CodeBase = Directory.GetCurrentDirectory() }; var assemblyBuilder = AppDomain.CurrentDomain .DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); var moduleBuilder = assemblyBuilder.DefineDynamicModule("WhateverModule", "Whatever.dll"); var typeBuilder = moduleBuilder.DefineType("WhateverType", TypeAttributes.Public); var type = typeBuilder.CreateType(); assemblyBuilder.Save("Whatever.dll"); var codeBase = type.Assembly.CodeBase; // throws the below exception System.NotSupportedException was unhandled Message=The invoked member is not supported in a dynamic assembly. Source=mscorlib StackTrace: at System.Reflection.Emit.InternalAssemblyBuilder.get_CodeBase() at Stupid.Program.Main(String[] args) in C:\Users\Walking Disaster\Documents\Visual Studio 10\Projects\Lingual.Proxy\Stupid\Program.cs:line 25 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() 

Can anyone see what I'm doing wrong?

Explanation: I am trying to extend NUnit with addin, which generates runtime testing methods. He does this by accepting an array of Action delegates. Unfortunately, Reflection is pretty much baked in the structure, so I had to publish classes using the method for each Action delegate. This works fine when I use only the NUnitTestMethod classes, but when I use the NUnitTestFixture, it fails because it is trying to read the CodeBase from the assembly. I did not want to create a physical assembly, but this project was one compromise after another.

+4
source share
2 answers

I think the reason for the exception is that CodeBase is pointless for dynamic assembly. From MSDN:

Assembly location as originally indicated

If you reload the assembly, it will not throw an exception:

 var assemblyName = new AssemblyName { Name = "Whatever", CodeBase = Directory.GetCurrentDirectory() }; var assemblyBuilder = AppDomain.CurrentDomain .DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); var moduleBuilder = assemblyBuilder.DefineDynamicModule( "WhateverModule", "Whatever.dll"); var typeBuilder = moduleBuilder.DefineType("WhateverType", TypeAttributes.Public); var type = typeBuilder.CreateType(); assemblyBuilder.Save("Whatever.dll"); var assembly = Assembly.LoadFrom("Whatever.dll"); var codeBase = assembly.CodeBase; // this won't throw exception 
+2
source

Is the Location assembly valid? Could you redo NUnit and use this instead of referencing the damage: P

I spent several hours studying the history of the NUnit extension and cannot imagine ever choosing it on xUnit.net - but I think that how many tests you have will solve this ...

+1
source

Source: https://habr.com/ru/post/1306580/


All Articles