Loading libraries run-time

code, silverlight — Szili @ 12:16 pm

I’m working on an application with the ability to upload and run Silverlight 1.1 gadgets. This means my program needs to be able to load assemblies in run-time.

After several tests and an e-mail exchange with an MS Evangelist it seams like it’s impossible.

UPDATE: Here is how I managed to do it. The trick is that the xaml downloads and loads its assembly automatically.
void DownloadXamlFile()
{
GadgetXamlDownloader.Completed +=
new EventHandler(GadgetXamlDownloader_Completed);
GadgetXamlDownloader.Open(
“GET”, new Uri(“./Gadgets/TestGadget/Page.xaml”, UriKind.Relative));
GadgetXamlDownloader.Send();
}


void
GadgetXamlDownloader_Completed(object sender, EventArgs e)
{
ObjectFromXamlString(GadgetXamlDownloader.ResponseText);
}


void
ObjectFromXamlString(string XamlString)
{
DependencyObject Gadget = XamlReader.Load(XamlString,true); //Namescoping is turned on
Gadget.SetValue(
Canvas.TopProperty,this.Height/2-(Gadget as Canvas).Height/2);
Gadget.SetValue(
Canvas.LeftProperty, this.Width / 2 - (Gadget as Canvas).Width / 2);
this.Children.Add(Gadget as Visual);
}

My old failed tests after the jump, they might be useful.

public partial class Page : Canvas{
public void Page_Loaded(object o, EventArgs e){// Required to initialize variablesInitializeComponent();Download(“Temp01.dll”);}
Downloader downloader = new Downloader();private void Download(string uri){downloader.Completed += new EventHandler(downloader_Completed);//specify the location and method used to get the fontdownloader.Open(“GET”, new Uri(uri, UriKind.Relative));//starts downloading the fontdownloader.Send();
}void downloader_Completed(object sender, EventArgs e)
{
System.Reflection.
Assembly GadgetHostAssembly = System.Reflection.Assembly.GetExecutingAssembly();System.Reflection.Assembly SilverlightPluginAssembly = System.Reflection.Assembly.GetCallingAssembly();#region I. Trials with System.Reflection.Assembly.LoadFrom()//A : No: Argument must be a string
//System.Reflection.Assembly GadgetAssembly = System.Reflection.Assembly.LoadFrom(downloader);//B: No: System.MethodAccessException
//System.Reflection.Assembly GadgetAssembly = System.Reflection.Assembly.LoadFrom(”./Test01.dll”);//C: No: System.MethodAccessException
//System.Reflection.Assembly GadgetAssembly = System.Reflection.Assembly.LoadFrom(downloader.ResponseText);#endregion#region
II. Trials with System.Reflection.Assembly.Load()
//A: No: System.IO.FileLoadException — Represents the error that occurs when a Assembly file is found but cannot be loaded.
//System.Reflection.Assembly GadgetAssembly = System.Reflection.Assembly.Load(”./Test01.dll”);//B: No: System.IO.FileNotFoundException
//System.Reflection.Assembly GadgetAssembly = System.Reflection.Assembly.Load(”Test01.dll”);

//C: No: System.IO.FileNotFoundException
//System.Reflection.Assembly GadgetAssembly = System.Reflection.Assembly.Load(downloader.ResponseText);//D: No: System.IO.FileLoadException
//System.Reflection.Assembly GadgetAssembly = System.Reflection.Assembly.Load(”./Test01.dll, Version=1.0.0.0″);
//E: No: System.IO.FileNotFoundException
//System.Reflection.Assembly GadgetAssembly = System.Reflection.Assembly.Load(”Test01, Version=1.0.0.0″);

/* “it seems to me that the only way this can be accomplished
* across browsers is to download the .dll and .xaml files
* separately and then call XamlReader.Load() on the downloaded .xaml file.
* Then somehow Silverlight seems to find the associated .dll both in IE and Firefox.
* I haven’t tested it in Safari but I’m assuming it works fine.”
* http://silverlight.net/forums/p/1344/6946.aspx#6946
*/
/* System.IO.FileLoadException*
* Means the assembly was loaded twice with two different evidence.
* If there would be a problem with security permissions it would
* throw a SecurityPremission - which it does not!
*
* So it might be possible that after the downloader object downloads
* the dll it loads automatically.
*/
#endregion

#region III. Test automatic loading

// A: No: Test object is null

//object Test = GadgetHostAssembly.CreateInstance(”TestType”);// B: No: Test object is null//object Test = GadgetHostAssembly.CreateInstance(”Temp01.TestType”);// C: No: Type object is null
//Type Test = GadgetHostAssembly.GetType(”TestType”);
// D: No: Type object is null
//Type Test = GadgetHostAssembly.GetType(”Temp01.TestType”);
#endregion#regionIV. Testing with AppDomains
// A: No: System.AppDomain.CreateDomain() function is not included in Silverlight CLR
// B: No: System.AppDomainManager.CreateDomain() function is not included in Silverlight CLR

#endregion#region Sample C# assembly loading code
//!Not Silverlight code!
/* Assembly ass = Assembly.LoadFrom(”http://localhost/MDIDLLS/ImageForm.dll”);//Object imgForm = ass.CreateInstance(”Imageform.ImageForm”); Type objType = ass.GetType(”ImageForm.ImageForm”);
Object objForm = Activator.CreateInstance(objType);
BaseForm.BaseForm imgForm = (BaseForm.BaseForm)objForm;
*/#endregion }}

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2010 Szili | powered by WordPress with Barecity