Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Assembly.Load vs Assembly.ReflectionOnlyLoad

I'm trying to understand the differences between Assembly.Load and Assembly.ReflectionOnlyLoad.

In the code below I am attempting to find all of the objects in a given assembly that inherit from a given interface:

var myTypes = new List<Type>();  var assembly = Assembly.Load("MyProject.Components");  foreach (var type in assembly.GetTypes()) {    if (type.GetInterfaces().Contains(typeof(ISuperInterface)))    {       myTypes.Add(type);    } } 

This code works fine for me, but I was doing some research into other possibly better alternatives and came across Assembly.ReflectionOnlyLoad() method.

I assumed that since I'm not loading or executing any of the objects, essentially just querying on their definitions that I could use ReflectionOnlyLoad for a slight performance increase...

But it turns out that when I change Assembly.Load to Assembly.ReflectionOnlyLoad I get the following error when it calls assembly.GetTypes():

System.Reflection.ReflectionTypeLoadException: 

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

I assumed that the above code was JUST doing reflection and "looking at" the library... but is this some sort of instance of the Heisenberg Uncertainty Principle whereby looking at the library and the objects in it is actually attempting to instantiate them in some way?

Thanks, Max

like image 857
Max Schilling Avatar asked Nov 20 '08 16:11

Max Schilling


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

As per Jon's reply, it would be helpful to know what's in LoaderExceptions. In lieu of this information, I think I can hazard a guess. From MSDN:

If the assembly has dependencies, the ReflectionOnlyLoad method does not load them. If you need to examine them, you must load them yourself.

You need to attach a handler to AppDomain.ReflectionOnlyAssemblyResolve to help the CLR load any dependencies of the assembly you're loading. Have you done this?

like image 106
Kent Boogaart Avatar answered Sep 21 '22 15:09

Kent Boogaart


The ReflectionOnly methods are the only way you can load a specific Assembly on disk to examine without going via the usual Load/LoadFrom rules. For example, you can load a disk-based assembly with the same identity as one in the GAC. If you tried this with LoadFrom or LoadFile, the GAC assembly is ALWAYS loaded.

Additionally, you may not call GetCustomAttributes(...) on the return Assembly instance since this will attempt to instantiate the Attributes on the assembly, which are ReflectionOnly. You must use CustomAttributeData class's static methods for this.

No types in an assembly loaded via ReflectionOnly may be instantiated.

like image 27
x0n Avatar answered Sep 19 '22 15:09

x0n