There are so many different ways to do this, and many of them seem
preferable to (ab)using the remoting infrastructure.  Can you describe what
you are trying to accomplish, and why you believe it requires using the call
context?

-- arlie


-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Ross Diesel
Sent: Tuesday, April 20, 2004 7:01 AM
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] Smuggling delegates via the context

Hi

I'm trying to smuggle a form event handler to another form using the
context, and I'm getting a cast error when I try to retrieve the delegate
from the data slot.

DelegateDef.cs
==============

namespace X
{
        public delegate void DaEventHandler(object sender, EventArgs e); }


Form1.cs
========

using System.Runtime.Remoting.Contexts;

namespace X
{
        public class frm1 : Form
        {
                :
                :
                public void frm1EventHandler(object sender, EventArgs args)
                {
                }
                :
                private void btnStart_Click(object sender, System.EventArgs
e)
                {
                        LocalDataStoreSlot dss =
Context.AllocateNamedDataSlot("slotName");
                        Context.SetData(dss, new
DaEventHandler(this.frm1EventHandler));
                        :
                        //cast works here, but nowhere else!
                        DaEventHandler eh =
(DaEventHandler)(Context.GetData(dss));
                }
        }
}


Form2.cs
========

using System.Runtime.Remoting.Contexts;

namespace X
{
        public class frm2 : Form
        {
                :
                :
                public event DaEventHandler DaEvent;
                :
                public frm2()
                {
                        //
                        // Required for Windows Form Designer support
                        //
                        InitializeComponent();

                        LocalDataStoreSlot dss =
Context.GetNamedDataSlot("slotName");

                        // => raises an invalid cast exception
                        DaEvent += (DaEventHandler)(Context.GetData(dss));
                }
        }
}

Also, frm2's ctor is being invoked on the same thread as the thread on which
frm1 lives - so there don't appear to be remoting/marshalling issues?

I also get the same exception if I just use a helper class (which tries to
retrieve the delegate  from the context) directly invoked from frm1.

I realise I could use COM+ loosely coupled events to resolve this, but I'm
curious as to the underlying cause and the implications for smuggling out of
bandwidth references via the Context.

thanks

===================================
This list is hosted by DevelopMentorR  http://www.develop.com Some .NET
courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to