Can't figure out this issue with Generics

  • Thread starter Thread starter Steve K.
  • Start date Start date
S

Steve K.

I am in over my head I think, I've got my brain running in circles trying to
figure this out.

I'm using remoting with a client and a server. I need the server to raise
events on the client and after doing quite a bit of reading I found what I
think is the correct way. The article I read encourages shims to pass the
delegate to server and back so that the client process doesn't need to be
passed. I barely understand it, I will understand more when I get it
working.

Anyway, these "Shim" classes in the example were tied to a specific delegate
and I wanted to create something a little more generic and reusable.

Here is the code I'm having trouble with:
<code>
// From the client
EventShimWithArgs<int[]>.Create(new
EventHandler<EventArgs<int[]>>(OnSomething));

// EventArgs <T>
public class EventArgs<T> : EventArgs
{
private T _data;

public EventArgs(T data)
{
_data = data;
}

public T Data
{
get { return _data; }
}
}



// On the server
public class EventShimWithArgs<T> : MarshalByRefObject
{
private EventHandler<EventArgs<T>> target;

private EventShimWithArgs(EventHandler<EventArgs<T>> target)
{
this.target += target;
}

public void Rar<T>(object sender, EventArgs<T> eventArgs)
{
target(this, eventArgs);
}

public static EventHandler<EventArgs<T>>
Create<T>(EventHandler<EventArgs<T>> target)
{
EventShimWithArgs<T> shim = new EventShimWithArgs<T>(target);
return new EventHandler<EventArgs<T>>( shim.Rar<T> );
}
}
</code>

The line that is giving me my last compiler error is:
target(this, eventArgs);

The error is:
<error>
cannot convert from

'PMD.OfficeStudio.Infrastructure.Interface.EventArgs<T>
[c:\PMDRepository\Tools\OfficeStudio\Source\Infrastructure\Infrastructure.Interface\bin\Debug\Infrastructure.Interface.dll]'

to

'PMD.OfficeStudio.Infrastructure.Interface.EventArgs<T>
[c:\PMDRepository\Tools\OfficeStudio\Source\Infrastructure\Infrastructure.Interface\bin\Debug\Infrastructure.Interface.dll]'

C:\PMDRepository\Tools\OfficeStudio\Server\OfficeStudioServerInterfaces\EventShimWithArgs.cs
OfficeStudioServer.Interface 23 26
</error>

Now... it's complaining about not being able to convert by the types it says
it can't covert are the SAME!

I'm stumped, anyone see the problem?

Thanks for any help!
Steve
 
the types it says it can't covert are the SAME!
I'm stumped, anyone see the problem?

Different T... errors are one thing, but also look at the *warnings*:
Warning 1 Type parameter 'T' has the same name as the type parameter
from outer type 'EventShimWithArgs<T>' d:\dump\wf
\ConsoleApplication2\ConsoleApplication2\Program.cs 60 21
ConsoleApplication2
Warning 2 Type parameter 'T' has the same name as the type parameter
from outer type 'EventShimWithArgs<T>' d:\dump\wf
\ConsoleApplication2\ConsoleApplication2\Program.cs 67 8
ConsoleApplication2

So:
Rar<T> and Create<T> should perhaps not be generic methods, but just
members of the generic class?
i.e. Rar(object sender, EventArgs<T> eventArgs) etc

If you *need* them as generic methods inside the generic class, then
pick a different name for the arg, i.e.
i.e. Rar<TSomethingElse>(...)
(pick a meaningful name...)

Marc
 
Hi Marc,

Great help, thanks so much. The warnings held the key! I've also modifed
the design per your suggestions and it seems to be working nicely now and I
learned something, always a good thing.

Take care,
Steve
 
Back
Top