Click here to Skip to main content
15,885,767 members
Home / Discussions / C#
   

C#

 
AnswerRe: Downloading an image from web Pin
V.26-Mar-12 23:51
professionalV.26-Mar-12 23:51 
QuestionMessenger Class Problem Pin
Kevin Marois26-Mar-12 8:08
professionalKevin Marois26-Mar-12 8:08 
AnswerRe: Messenger Class Problem Pin
SledgeHammer0126-Mar-12 8:23
SledgeHammer0126-Mar-12 8:23 
GeneralRe: Messenger Class Problem Pin
Kevin Marois26-Mar-12 10:34
professionalKevin Marois26-Mar-12 10:34 
GeneralRe: Messenger Class Problem Pin
SledgeHammer0126-Mar-12 10:48
SledgeHammer0126-Mar-12 10:48 
GeneralRe: Messenger Class Problem Pin
Kevin Marois26-Mar-12 10:50
professionalKevin Marois26-Mar-12 10:50 
GeneralRe: Messenger Class Problem Pin
SledgeHammer0126-Mar-12 11:06
SledgeHammer0126-Mar-12 11:06 
AnswerRe: Messenger Class Problem Pin
BobJanova27-Mar-12 4:58
BobJanova27-Mar-12 4:58 
Just to second Sledgehammer's point, your main problem here is that you are trying to store a whole load of different Action<T> handlers (i.e. handlers for different object types) in the same list. If you think about how you'd try to actually read handlers back out of this list again (in order to fire them, for example) you'll realise that with your current design you can't possibly know which type of handler you have.

If you actually need a central registry of different handler types, I think you need to do something like

class MainStorageClass {
 Dictionary<Type, BaseRegistry> registries;

 public void Register<T>(Action<T> callback) {
    Type messageType = typeof(T);
    Registry<T> registry;
 
    if (registries == null)
    {
        registries = new Dictionary<Type, BaseRegistry>();
    }
 
    if (!registries.ContainsKey(messageType))
    {
        registry = new Registry<T>();
        internalList.Add(messageType, registry);
    }
    else
    {
        registry = internalList[messageType];
    }
 
    registry.Actions.Add(callback);
 }
   

 private abstract class BaseRegistry {}
 private class Registry<T> : BaseRegistry {
  internal List<Action<T>> Actions { get; private set; }

  internal Registry() { Actions = new List<Action<T>>(); }
 }
}


You could technically do it with Delegate as the base type for Action, but List<Delegate> doesn't superclass List<Action<T>>, so it's less ugly this way, I think. Note that BaseRegistry doesn't actually do anything apart from act as a superclass you can declare the dictionary as using; you could use Dictionary<Type, object> and it would work the same, but I think that is unclear and permits you to make bad mistakes.

And now you can look up like

public List<Action<T>> Find<T>(){
 if(registries == null) return null;
 BaseRegistry registry;
 if(!registries.TryGetValue(typeof(T), out registry)) return null;
 return (registry as Registry<T>).Actions;
}

... which I think is quite clean.

Edit: Changed Find slightly, I don't think the original would compile and you need a cast.

modified 27-Mar-12 11:28am.

QuestionIdentify which Element fired event handler code-behind. Pin
cknight72526-Mar-12 6:28
cknight72526-Mar-12 6:28 
AnswerRe: Identify which Element fired event handler code-behind. PinPopular
Luc Pattyn26-Mar-12 6:40
sitebuilderLuc Pattyn26-Mar-12 6:40 
GeneralRe: Identify which Element fired event handler code-behind. Pin
cknight72527-Mar-12 7:23
cknight72527-Mar-12 7:23 
GeneralRe: Identify which Element fired event handler code-behind. Pin
Luc Pattyn27-Mar-12 7:39
sitebuilderLuc Pattyn27-Mar-12 7:39 
QuestionDoes anyone have a method that read raw bits or bytes? Pin
turbosupramk326-Mar-12 5:43
turbosupramk326-Mar-12 5:43 
AnswerRe: Does anyone have a method that read raw bits or bytes? Pin
PIEBALDconsult26-Mar-12 5:58
mvePIEBALDconsult26-Mar-12 5:58 
GeneralRe: Does anyone have a method that read raw bits or bytes? Pin
turbosupramk326-Mar-12 8:16
turbosupramk326-Mar-12 8:16 
GeneralRe: Does anyone have a method that read raw bits or bytes? Pin
harold aptroot26-Mar-12 6:59
harold aptroot26-Mar-12 6:59 
GeneralPrevious: Can read 64bit Reg_Sz, but not 64bit D_Word [modified] Pin
Eddy Vluggen26-Mar-12 7:30
professionalEddy Vluggen26-Mar-12 7:30 
GeneralRe: Previous: Can read 64bit Reg_Sz, but not 64bit D_Word [modified] Pin
harold aptroot26-Mar-12 7:48
harold aptroot26-Mar-12 7:48 
GeneralRe: Previous: Can read 64bit Reg_Sz, but not 64bit D_Word [modified] Pin
turbosupramk326-Mar-12 8:17
turbosupramk326-Mar-12 8:17 
GeneralRe: Previous: Can read 64bit Reg_Sz, but not 64bit D_Word [modified] Pin
Eddy Vluggen26-Mar-12 8:21
professionalEddy Vluggen26-Mar-12 8:21 
GeneralRe: Previous: Can read 64bit Reg_Sz, but not 64bit D_Word [modified] Pin
turbosupramk326-Mar-12 8:25
turbosupramk326-Mar-12 8:25 
GeneralRe: Previous: Can read 64bit Reg_Sz, but not 64bit D_Word [modified] Pin
Eddy Vluggen26-Mar-12 8:43
professionalEddy Vluggen26-Mar-12 8:43 
GeneralRe: Previous: Can read 64bit Reg_Sz, but not 64bit D_Word [modified] Pin
turbosupramk326-Mar-12 8:50
turbosupramk326-Mar-12 8:50 
GeneralRe: Previous: Can read 64bit Reg_Sz, but not 64bit D_Word [modified] Pin
Eddy Vluggen26-Mar-12 9:16
professionalEddy Vluggen26-Mar-12 9:16 
Questionconverting to different pixel formats. Pin
Shailesh H26-Mar-12 0:11
Shailesh H26-Mar-12 0:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.