0

I am trying to create and pass a Delegate from one AppDomain to another.

But it throws Error only when passing Delegates. If objects like String are passed, it works fine.

Also If the delegate is removed from setting through a constructor and set using a method, I am still unable to pass it to the object.

namespace TESTNS1 {
    class Program {
        static void Main(string[] args) {
            try {
                Program obj = new Program();
                NS22.testappdom.MYDelT tt = new NS22.testappdom.MYDelT(obj.testingg);

                NS22.testappdom obb = new NS22.testappdom(tt);
                obb.invokeT();


                AppDomain app1 = AppDomain.CreateDomain("newdom");
                NS22.testappdom ob2 = (NS22.testappdom)app1.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, typeof(NS22.testappdom).FullName,
                    false, BindingFlags.Default, null, new object[] { tt }, null, null, null);
                ob2.invokeT();
            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }

        public bool testingg (Object obj) {
            Console.WriteLine("testingg called..");
            return false;
        }
    }
}

namespace NS22 {
    class testappdom : MarshalByRefObject {

        public delegate bool MYDelT(Object obj);
        public MYDelT delee;

        public testappdom() {
            Console.WriteLine("Init.");
        }

        public testappdom(MYDelT dd) {
            Console.WriteLine("Init1.");
            this.delee = dd;
        }

        public void invokeT() {
            this.delee(new Object());
        }
    }
}
Santron Manibharathi
  • 628
  • 5
  • 12
  • 26
  • Check http://stackoverflow.com/questions/2008691/pass-and-execute-delegate-in-separate-appdomain – Amit Feb 09 '15 at 05:47
  • @Amit thanks for the link. But it seems that it is impossible to send a delegate to another appdomain. I will try to figure out a workaround without using delegate for the same functionality. Thanks! – Santron Manibharathi Feb 09 '15 at 12:25
  • Check this one http://thevalerios.net/matt/2008/06/run-anonymous-methods-in-another-appdomain-part-2/ – Amit Feb 09 '15 at 12:43
  • @Amit thanks for the link. I only wanted to find whether the delegate can be passed directly. As it is confirmed not, I have worked around the way using a handler class for the operations i needed to. Thanks! – Santron Manibharathi Feb 11 '15 at 10:17

0 Answers0