I recognise that I'm abusing the call context in this specific situation.
The motivation was primarily an R&D one as it appeared to be the quickest
mechanism (without reworking interfaces) to get a notification from any one
of a number of modeless dialogs back to my main test harness dialog.
The modeless dialogs essentially represent worker threads.
Please note that the issue crops up even under a non-remoting context i.e. I
knocked up a helper class which was instantiated after the delegate was
stored in the context and a method invoked on the helper which tried to
retrieve the delegate but I experienced the same result.
Also, the following cast works in both the modeless dialog and the helper
class...
MulticastDelegate eh = (MulticastDelegate)Context.GetData(dss);
another curious fact was that I could see the delegate (target method and
instance) correctly in the debugger.
regards
-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Arlie Davis
Sent: 20 April, 2004 17:25
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Smuggling delegates via the context
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
===================================
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