.Net에서 동적으로 어셈블리를 만들려고합니다. 그러나 CodeBase 속성에서 값을 반환하는 방법을 알아낼 수 없습니다. 다음은 그 예입니다.동적으로 생성 된 어셈블리에서 CodeBase에 액세스 할 수 없습니다.
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()
내가 잘못하고있는 것을 누구든지 볼 수 있습니까?
설명 런타임시 테스트 메소드를 생성하는 addin으로 NUnit을 확장하려고합니다. 이것은 액션 델리게이트의 배열을 취함으로써이를 수행합니다. 안타깝게도 Reflection은 프레임 워크 깊숙이 구워져 있으므로 각 Action Delegate에 대한 메서드로 클래스를 내 보내야했습니다. NUnitTestMethod 클래스를 사용할 때만 제대로 작동하지만 NUnitTestFixture를 사용할 때 어셈블리에서 CodeBase를 읽으므로 실패합니다. 나는 물리적 인 어셈블리를 만들고 싶지 않았지만,이 프로젝트는 다른 프로젝트와 절충안이었습니다.
나는 이것을 두려워했다. 나는이 일을해야만 할 수도있다. 그런 다음 리플렉션에 대한 의존도가 심한 데미지를 줄이기 위해 NUnit을 더 깊이 파헤쳐 라. –