Question about Assembly.Load()

  • Thread starter Thread starter iamcs1983
  • Start date Start date
How can I delete the assembly file after loading it?
MyClass.dll is a library file at current directory.
Here is my code for testing:

using System;
using System.IO;
using System.Reflection;

public class TestAssembly
{
public void DoIt()
{
Assembly assembly=Assembly.Load("MyClass");
/*
Type type=assembly.GetType("MyNS.MyClass");

Console.WriteLine(type.FullName);

ConstructorInfo ci=type.GetConstructor(Type.EmptyTypes);
object obj=ci.Invoke(null);

MyNS.MyClass myclass=obj as MyNS.MyClass;

Console.WriteLine(myclass!=null);
*/
}
}

public class Test
{
public static void Main()
{
TestAssembly test = new TestAssembly();
test.DoIt();

File.Delete("MyClass.dll");
}
}


An UnauthorizedAccessException exception is thrown because of the last
line of Main method.
How to delete the file MyClass.dll?
 
How can I delete the assembly file after loading it?

Well, have you tried loading the file into a byte array (using
MemoryStream, probably) and then passing that into Assembly.Load? I
believe that in this case a temporary file will actually be written to
disk anyway, but it might help.

Jon
 
Thank you.
But I'm writing test codes for a class which uses Assembly.Load(String)
to load a assembly, so I want to know how to solve this case.
 
| Thank you.
| But I'm writing test codes for a class which uses Assembly.Load(String)
| to load a assembly, so I want to know how to solve this case.
|

You can create an additional Application domain to load/run your test
assembly. When done with this assembly you can unload the AD, this will
release the assembly and the OS will release the file handle so that you are
free to delete or replace the file.

Willy.
 
Back
Top