0

I have a method that performs a search in both the local assembly and in the current directory. It is looking for a class based on the name provided (reflection). However I now want to only load the classes/dlls that I am looking for into memory as opposed to loading them all and selecting the one that I want from it. I have been told that marshalbyrefobject could be used to do this. [Below is the code I am currently using]

The solution would be to create 2 app domains and have one load all the assembiles and do the checks then unload on of the app domains, though im not sure how to go about doing that.

user1348463
  • 87
  • 1
  • 1
  • 8
  • I thought Garbage collector does almost what we need involving memory management? – King King Dec 06 '13 at 13:51
  • Well ive been given the task to not load them into memory in the first place, only to load into memory the ones that I am looking for – user1348463 Dec 06 '13 at 13:53
  • 1
    No idea why you think MBRO does anything useful here. The .NET Framework already works this way, you only ever pay for classes that you actually use. Unused types don't take any resources at all. You are trying to solve a problem that you don't have. – Hans Passant Dec 06 '13 at 15:06
  • added an edit to my question – user1348463 Dec 06 '13 at 21:03

1 Answers1

0

To my limited knowledge, you can avoid loading the assemblies by querying them beforehand using the Assembly class.

A full working model would look like this:

1 - Collect information through the Assembly class

Assembly File:

Assembly assembly = Assembly.ReflectionOnlyLoadFrom(fileName);

Local assembly:

Assembly myAssembly = Assembly.GetExecutingAssembly();

2 - Iterate through the classes present on each Assembly reference

Assembly mscorlib = typeof(string).Assembly;

foreach (Type type in mscorlib.GetTypes())
{
    Console.WriteLine(type.FullName);
}

3 - After choosing which assembly to use, Load it into your Application Domain

Assembly assembly = Assembly.Load(fullAssemblyName);

or

Assembly assembly = Assembly.LoadFrom(fileName);

Examples copied from the following post, so feel free to upvote the responses there if you feel they contributed to solve your issue!

C#: List All Classes in Assembly

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46