Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I' having tough time with handling generics in C#, following is the code snippet that i'm using
 
public delegate void SyncEventDelegate<TEntity>(TEntity entity, SyncInfo requestInfo);
 
i need to add this delegate to a Dictionary
 
private Dictionary<Type, SyncEventDelegate<ISynchronizeableEntity>> _delegateCollection;
 
the method that passes the the delegate to register has the following signature.
 
public void Register<TEntity>( SyncEventDelegate<ISynchronizeableEntity> syncEventDelegate) where TEntity : ISynchronizeableEntity
 
it's working fine for the moment , but I have a doubt if i change the register method signature to the following format , i get a compiler error.
 
public void Register<TEntity>( SyncEventDelegate<TEntity> syncEventDelegate) where TEntity : ISynchronizeableEntity
 
I thought it should work since TEntity is derived from ISynchronizeableEntity, but it doesn't.
 
I really appreciate if someone can explain this.
thanks in advance.
Posted 3-Nov-12 3:13am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Where exactly do you get the error?
I created a delegate and a method register with the signature you say that causes the error, and it works.
 
I think your problem is that you want a Register that receives a SyncEventDelegate<TEntity> and tries to register it in a dictionary that is still of type SyncEventDelegate<ISynchronizeableEntity>
 
In this case, is like you want to put an Action of string inside an Action of object. There is no valid variance for that, as if you receive the Action of object you can pass any object as parameter, when only strings should be valid parameters.
I think you should also change the type of the dictionary or, if that's not possible, you will need to always receive the parameter as an action of the interface. If needed, the method called by the delegate should do a cast.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 543
1 OriginalGriff 384
2 George Jonsson 258
3 Abdul Samad KP 135
4 Animesh Datta 130
0 OriginalGriff 6,099
1 Sergey Alexandrovich Kryukov 5,411
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100