tags:

views:

64

answers:

3

In Java you can do something like:

byte[] code = ReadFromClassFile("SomethingSomething.class");
SendAcrossNetwork(code);

And on the other end:

byte[] code = ReadFromNetwork();
Class marshalledCode = CustomClassLoader.defineClass(code, 0, code.length);
Object obj = marshalledCode.newInstance();  //Hey look, I've marshalled a class over the network!

The above leaves out 90% of the details, but you should get the idea.

My question is, what is the .NET equivalent?
I can find all sorts of references on Remoting, but nothing on this sort of class loading.


I've kind of stuck myself with byte[] as the lowest level transport level (Sockets, basically).

To be clear, the Type's involved start out on one machine and need to be marshaled to other machines that have nothing but a common Interface defined in a common Assembly. The Type being marshaled does not exist on the receiving machine.

Remote invocation is not acceptable in this case, I need objects to receive method calls only from the process that created them. Network traffic is very strictly controlled; I'm basically sending local behaviors across the network as needed, not distributing computing in anyway.

+1  A: 

Check out Serialization and either Remoting or Sockets for transport.

Serialization is the process of converting a properly marked object into a binary or XML (or other formats for which there exists a formatter) format.

Remoting and Sockets serve similar purposes, but where Sockets are very low-level data transport primitives, Remoting as a system is rather high-level - the tradeoff is between complete control (Sockets) and ease of use (Remoting).

Edit: Ahh, I understand. Remoting is what you'll want to use, then. If the types you want to use are all derived from the same base, and both client and server are aware of the base, then Remoting will get you the rest of the way.

For instantiating types on the Client that exist on the Server, see this page and its references - http://msdn.microsoft.com/en-us/library/cbzcxy2s%28VS.71%29.aspx

Erik Forbes
I'm really looking for code that takes TypeA on machine #1 and sends it to machine #2 that doesn't have TypeA in an assembly somewhere.byte[] is kind of implied as the transport medium. I'll update the question.
Kevin Montrose
Gotcha. Answer updated - see edit.
Erik Forbes
Remoting looks like it lets one process invoke and methods on an object in another process. That's not what I'm trying to do here; the objects need to be instantiated within the process on the receiving end.If I'm misunderstanding, I'll need some example code; cause that's all I see atm.
Kevin Montrose
+1  A: 

Have a look at Reflection.Emit. It allows you to dynamically generate types and assemblies. You'll have to find a way to serialize the type definition, send that across, then generate a new type / assembly on the second machine.

Sorry if that's a bit vague, but hopefully its enough to get you started.

Nader Shirazie
A: 

From the type you can get the containing assembly, the assembly can be serialized or you can get its underlying bits, and sent across the wire, the receiver can then materialize the assembly and instantiate the type. It is possible, but not out of the box, ie. you have to write the plumbing. Also you need to take care of the dependencies and security implications.

Remus Rusanu
Marshalling the whole assembly is serious overkill. Especially if the common interface is defined in the assembly, as you couldn't load two such assemblies without a naming conflict.
Kevin Montrose
@Kevin: my thinking was at a generic solution when you don't actually know nor control the assembly
Remus Rusanu