Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading unknown types
    text
    copied!<p>I have classes that I load dynamically (using mef), they are some sort of handlers (These work fine).</p> <p>These handlers can receive (there's a method that receives a packet and returns one) packets, that all implement the same interface (let's say IPacket) and return an answer (an IPacket as well).</p> <p>I receive these packets (through a tcp connection) and my program is not familiar with the specific class (although it is of a known interface - IPacket, but a different implementation).</p> <p>So when I try to deserialize a packet (to hand it over to the handler), I get an exception.</p> <ul> <li>I Serialize into bytes a deserialize back into an object (using Binary serializer) *</li> </ul> <p>The only way I can access the packet implementations should be dynamic, as the dlls are stored in a folder I can access.</p> <p>I thought that I could just use Assembly.LoadFrom in order to familiarize my program with the packets, because I don't even need to instatiate them, just deserialize (get an instance of the interface) and hand over to the handler, which in would then return an answer and I'd send it again.</p> <p>But it didn't work..</p> <p>I assume that I need to find a way to add a reference to these dlls during runtime and then my program will recognize them.. (I thought that maybe using the Export(typeof()..) on the pack classes would help, would it?)</p> <p>The exception I am getting when trying to deserialize is that the <em>class name</em> is not found..</p> <p>*I have edited the topic and I hope it is a little clearer, thank you =]</p> <hr> <ul> <li>Edit:</li> </ul> <p>I am not saying this is solvable with mef, I just though that it could be. It is definitely solvable with reflection. I have the folder that contains all the classes I want my program to recognize during runtime, I just need to make it "load" them during runtime, as if I'd added a reference to the dlls in that same folder.</p> <p>So basically what I need to do is:</p> <p>Load all the implementations of a certain interface (IPacket in this example) from a folder. I do not need to instantiate them, but only to receive them as variables without getting an exception that such type isn't in my project.</p> <hr> <p>So I have found this snippet:</p> <pre><code>static constructor() { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); </code></pre> <p>}</p> <pre><code>static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { Assembly ayResult = null; string sShortAssemblyName = args.Name.Split(',')[0]; Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly ayAssembly in ayAssemblies) { if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0]) { ayResult = ayAssembly; break; } } return ayResult; } </code></pre> <p>It seems to be close to what I am looking for but I don't actually understand this. Is there a way to midify this so that it would load only the dlls in a certain folder? Will my program then be familiar with the dlls?</p> <p>Also, an explanation of the code would be much appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload