I’ve got code to read an XAP and reflect a DLL inside. This works in Silverlight but not in Windows Phone 7. Is there a way to do this, because AssemblyPart.Load is not in the Windows Phone DLL.
Thanks, code is:
var client = new System.Net.WebClient();
client.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)
{
StreamResourceInfo resourceInfoDLL = Application.GetResourceStream(
new StreamResourceInfo(e.Result, null),
new Uri("Some.DLL", UriKind.Relative));
AssemblyPart assemblyPart = new AssemblyPart();
Assembly assembly = assemblyPart.Load(resourceInfoDLL.Stream);
.
.
};
client.OpenReadAsync(new Uri("Some.xap", UriKind.Relative));
Remember that a XAP is a standard ZIP package, so as long as you can unzip it, you can access its internal contents.
This won’t give you much in the long run for the task you are trying to accomplish, because the internal Reflection capabilities are limited to static libraries that are loaded with the application or are accessible from the GAC (Global Assembly Cache). The reasoning is not as much to prevent hacking, because reflecting .NET assemblies won’t hurt the OS in most cases (read this document to better understand the security model). It is to prevent dynamic code loading that will perform actions outside the declared capabilities when the application is published.