3

I have a number of assemblies which I treat as libraries in my Unity game. When the game starts, I want to quickly read just the manifest of all files - there may be some 50+ at some point in the future - and make a list in memory.

I have packed some useful info in the copyright and product information fields which are sufficient to read, but I certainly do not want to load the whole assembly into my app domain or any other app domain.

AssemblyName.GetAssemblyName seems to be the most light weight

AssemblyName myAssemblyName = AssemblyName.GetAssemblyName("E:/Code/Projects/MyUtilities/bin/Debug/MyUtilities.dll");

But does not help as it only gives the assembly name, version and culture info. These cannot have custom strings anyway(the assembly fails to compile if I force some custom strings in there using the code in AssemblyInfo.cs)

So that brings me to the last option which is Assembly.ReflectionOnlyLoadFrom

Assembly myAssembly = Assembly.ReflectionOnlyLoadFrom("E:/Code/Projects/MyUtilities/bin/Debug/MyUtilities.dll");

object[] map = myAssembly.GetCustomAttributes(false);

Does this actually load the whole file? That would be too much for me as I only want the attributes.

If it does then I am left with no options but to prepend some extra bytes to the assembly file myself and remove the bytes before I hand the byte array to

Assembly.Load(bytes);
Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
safe_malloc
  • 824
  • 2
  • 12
  • 29
  • Measure, measure, measure. Then measure some more, this depends primarily on the file system on which the assembly is stored and how well its content is cached. Highly unpredictable. None of the Assembly.LoadXxx() methods actually "load the file", they merely create a memory-mapped file view for the assembly. File content is paged into RAM only when needed, you don't pay for what you don't use. ReflectionOnlyXxx() is useful to avoid nasty accidents with module and static constructors, making retrieving attributes faster was not the primary design goal. – Hans Passant Mar 04 '17 at 09:34
  • Thank you for the detailed answer. May I know a bit more about these issues with module and static constructors for which ReflectionOnly was designed? – safe_malloc Mar 04 '17 at 13:56

0 Answers0