So I\'ve found a bunch of threads on this topic but I don\'t think I\'ve found one that applies yet.
Basically my .exe loads a .dll (MyAssembly) file which does the
I'd like to build on Sean Ed-Man's answer, which is good, but doesn't work in my case.
If you can instantiate the class but the BinaryFormatter can't resolve it, this may work for you.
In my case, the calling assembly (PluginAssembly for this example) is being run as a plugin from the executable, in the form of a zip file. For some reason I can directly resolve the class (from NeededAssembly) when instantiating, but the BinaryFormatter can't resolve it. NeededAssembly is, of course, included as a reference to the PluginAssembly project, which is why I can instantiate. I don't know why the BinaryFormatter is different.
Regardless, this is what worked for me:
public class PluginAssembly
{
// (class code here)
private sealed class CustomizedBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
Type returntype = null;
if (typeName.StartsWith("NeededAssembly.RemoteClient.MessagePayload"))
{
returntype = typeof(MessagePayload);
}
else if (typeName.StartsWith("NeededAssembly.RemoteClient.ResultsPayload"))
{
returntype = typeof(ResultsPayload);
}
else if (typeName.Equals("System.Collections.Generic.List`1[[NeededAssembly.ShortResult, NeededAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"))
{
returntype = typeof(List);
}
else
{
returntype =
Type.GetType(String.Format("{0}, {1}",
typeName, assemblyName));
}
return returntype;
}
public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
base.BindToName(serializedType, out assemblyName, out typeName);
if (serializedType.ToString().Contains("NeededAssembly"))
{
assemblyName = typeof(MessagePayload).Assembly.FullName;
}
}
}
}
Of course, don't forget to use it:
BinaryFormatter bf = new BinaryFormatter();
bf.Binder = new CustomizedBinder();
Basically I simply get a typeof for the needed class, which works.