0

I just get an error each time I run this code:

        AppDomain newDomain = AppDomain.CreateDomain("newDomain");
        AssemblyName assemblyName = AssemblyName.GetAssemblyName(path);

        Command cmd = (Command)newDomain.CreateInstanceAndUnwrap(assemblyName.FullName, typename);
        cmd.Execute();

Where path is the path of the Dll and typename is "NWT_Projekt.TestClass"

My command class:

using System;

namespace NWT_Projekt
{
    public interface Command
    {
        void Execute();
    }
}

and this is the source code of the DLL

using System;
using System.Collections;
using System.Xml;
using System.IO;
using System.Windows.Forms;


namespace NWT_Projekt
{
    public class TestClass : NWT_Projekt.Command
    {
        public MainForm f;

        public TestClass()
        {
            f = Form.ActiveForm as MainForm;
        }
        public void Execute()
        {
            //do something
        }
    }
}

ERROR (google translator :D)

An exception of type "System.Runtime.Serialization.SerializationException 'occurred in NWT PRojekt.exe.

Additional information: The type "NWT_Projekt.TestClass' in Assembly 'scripts, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null' is not marked as serializable.

EDIT2:

With the [Serializable] it works now, but after I run the code one time and then I want to create a second dll it gives me an IO error, because the file is in use! How can I fix this?

coolerfarmer
  • 216
  • 1
  • 4
  • 12

1 Answers1

1

If you want your command to run on its own appDomain, you should use MarshalByRefObject.

using System;
using System.Collections;
using System.Xml;
using System.IO;
using System.Windows.Forms;


namespace NWT_Projekt
{
    public class TestClass : MarshalByRefObject, NWT_Projekt.Command
    {
        public MainForm f;

        public TestClass()
        {
            f = Form.ActiveForm as MainForm;
        }
        public void Execute()
        {
            //do something
        }
    }
}

I advice you to prefix your interfaces : ICommand

Guillaume
  • 12,824
  • 3
  • 40
  • 48