Dynamically Downloading Classes at Runtime in Silverlight
Through the WebClient and AssemblyPart.Load method, Silverlight lets you download DLLs dynamically at runtime from the server to the client. This lets you defer dragging the DLL to the client until you know you need it.
To use the DLL you'll download at runtime, you still need to add a reference to the DLL in order to get IntelliSense support at design time (and also to get your application to compile). But because you're going to download the class through code at runtime, you don't need to include that DLL in your project. So, after adding a reference to the DLL, you can set its Copy Local to False.
Using the DLL in your Silverlight application is a little awkward. While you instantiate the class with the New keyword, as usual, you must do it in a dedicated method decorated with MethodImpl attribute, passing the NoInlining parameter. Something like this, for instance:
Private Function CreateMyObject() As Object
Return New MyDynamicClass
But you can't actually use the resulting object in that method. Instead, to make Silverlight's type checking happy, you need to defer using that class to a completely separate method:
Private Sub UseMyObject(dc As Object)
Dim dc As MyDynamicClass = CType(dc, MyDynamicClass)
Attempting to instantiate and use the class in the same method will just generate some bewildering error messages.
Posted by Peter Vogel on 09/28/2011 at 1:16 PM