88034

C# Load Assembly w/ Common References

Question:

I've run into a slight issue - I'm writing a program that loads DLLs, each of which contain a class which inherits from a class existing in a library referenced by both the loaded DLL and the "host" program that is loading the DLL. The issue here is that, when I try to load and cast to the superclass:

var assembly = Assembly.LoadFrom(dllPath); var type = assembly.GetTypes().FirstOrDefault(x => x.IsSubclassOf(typeof (MySuperclass))); ...

Although both are referencing the class containing MySuperclass, since the dll is referencing the built class library (a separate file from the class library file the loading program is referencing) IsSubclassOf never returns true, because it considers the two classes to be different as they come from two different assemblies.

Furthermore, if you try casting a created instance of the loaded class to the superclass type, it can't do it since they're not the same (different assemblies).

So, my question is: <strong>how do you handle loading assemblies which reference common code, so that c# recognizes that you're loading a class which inherits from a common superclass?</strong>

Answer1:

You simply must use the same assembly files (even if they are identical) if you want programs to work together using common code. Here's how I solved the issue:

The program is loading a DLL from a subdirectory of its own.

Folder structure:

MyApp Folder --> MyProgram.exe CommonDependency.dll Submodules -> MySubmodule.dll

To get MySubmodule to use CommonDependency.dll within the next folder up, it's quite simple. Configure Visual Studio to not copy these dll dependencies to the build folder. Next, create an App.config in Visual Studio and add the following:

<configuration> <runtime> <assemblyBinding> <probing privatePath="../"/> </assemblyBinding> </runtime> </configuration>

This will tell the system to search for the assemblies in the parent folder ../ - if you want to have multiple folders, perhaps a separate dependency folder (relative to the location of the .dll) you can use ; as the delimiter - ../;../bin/; - the program will search these locations for the dependencies.

Answer2:

The ancestor class (superclass) must come from the same assembly for any chance for the loading code to work with descendants as a superclass reference.

Recommend

  • Reflection not working on assembly that is loaded using Assembly.LoadFrom
  • Detect Universal App at Runtime in PCL
  • Get Enum Description somehow “generically” over WCF
  • ControllerFactory having controllers separated in a different project
  • Asp.net MVC 5.2.2 on Azure
  • PDO to connect to web sql from localhost?
  • AppDomain doesn't make shadow files
  • Creating an object without knowing the class name at design time
  • Change text of “Cancel” button on a UISearchBar using Monotouch?
  • Linq to SQL and SQL Injection
  • How to access the database from the api controller?
  • How can I force LINQ to SQL to perform an INNER JOIN on a nullable foreign key?
  • Aspect advising other aspects
  • Extracting individual digits from a float
  • Copying rows in a database when rows have children
  • Available space left on drive - WinAPI - Windows CE
  • jquery validation - waiting for remote check to complete
  • Translating C# to PowerShell in InterIMAP
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • How can I replace the server in Web Component Tester
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • Very simple C++ DLL that can be called from .net
  • ilmerge with a PFX file
  • Fetching methods from BroadcastReceiver to update UI
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Fill an image in a square container while keeping aspect ratio
  • Build own AppleScript numerical error handling
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF