views:

106

answers:

0

I have a visual studio (2008) addin which creates a new appdomain.
Calling "CreateInstanceAndUnwrap" does not throw an exception (and the "AssemblyResolve" event does not fire), and I get an object.
That object, however, is flawed. :)
Calling "GetType" on it returns a "MarshalByRefObject", and not the concrete object I created.
This does not, happen, obviously, on regular applications, only in the hosted environment. I assume this has something to do with the "DomainManager" object on the VS appdomain, but I have no freaking idea how to go about this.

--Update---
If I don't set the application base folder, and throw my assembly in the VS private assemblies path - this works. I don't know whether to feel happy or sad.
No, actually I do - I feel sad.
--/update--

Repro code is below. (throw it in the addin autogenerated code; nothing more done)

public class Connect : IDTExtensibility2, IDTCommandTarget
{

//...

    public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
    {
        handled = false;
        if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
        {
            if(commandName == "MyAddin2.Connect.MyAddin2")
            {
                handled = true;
                AAA.DO();
                return;
            }
        }
    }
}

public class AAA
{
    public static void DO() {

        AppDomainSetup setup = new AppDomainSetup
        {
            ApplicationBase = new System.IO.FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName
        };
        var appd = AppDomain.CreateDomain("AAA", null, setup);

        object newObj = appd.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(BBB).FullName);

        MethodInfo mi =  newObj.GetType().GetMethod("MIAO");
        mi.Invoke(newObj, new object[] { "CAT" });   
    }
}

public class BBB :MarshalByRefObject {

    public void MIAO(string a) { }
}

related questions