views:

75

answers:

2

Hey,

This is a pretty specific question, but I need to load a Silverlight application library into my ASP.NET MVC Web Application to reflect over the types and build a list for the user to select from. The purpose of the application is to let users upload their custom-built libraries and test it in a "sandboxed" environment.

Anyway, after some struggling, I've achieved file uploads in ASP.NET MVC. Now, when I attempt to load the uploaded assembly for reflection, I get the following error:

{"Cannot resolve dependency to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.":"System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"}

I've added the specified assembly as a reference in the project to no avail.

The code to reflect the assembly looks like this:

Assembly a = Assembly.ReflectionOnlyLoad(_toolBytecode);
        foreach (Type t in a.GetTypes())
        {
            if (typeof(BoardTool).IsAssignableFrom(t))
            {
                if (t.IsSubclassOf(typeof(NetworkTool)))
                {
                    // Loop through each attribute of the class
                    foreach (object attr in t.GetCustomAttributes(false))
                        // Check if the attribute is the one we want
                        if (attr is NetworkToolIDAttribute)
                        {
                            ReflectedTool rt = new ReflectedTool();
                            rt.ToolID = ((NetworkToolIDAttribute)attr).ToolID;
                            rt.ToolName = t.Name;
                            _tools.Add(rt);
                        }
                }
                else
                {
                    ReflectedTool rt = new ReflectedTool();
                    rt.ToolID = Guid.Empty;
                    rt.ToolName = t.Name;
                    _tools.Add(rt);
                }
            }
        }

Any help would be apprecited, or if you know of another way to do this, that would be great too. If all else fails, I'll probably end up writing the sandbox launcher/container as a quick Silverlight application.

Thanks in advance.

A: 

Silverlight is a different CLR than .NET, so the assembles aren't compatible. The "System", "mscorlib", etc. used by Silverlight aren't the same as the ones used by ASP.NET. The IL format is the same AFAIK, so it's just a bit set at the beginning of the assembly plus references to different assemblies, so you may be able to go through the IL directly, but anything in the Assembly class will cause issues.

Robert Fraser
A: 

The real problem here isn't a different CLR but a different set of base libraries (mscorlib, system, etc.). So that in desktop you just can't open it up. You might want to look for a relfection library that isn't dependent on LoadLibrary. I know that Reflector does something like this. But I don't know if any of that code is available.

Shawn Wildermuth