Click here to Skip to main content
15,915,019 members
Home / Discussions / C#
   

C#

 
AnswerRe: Generic event, or raise event based on generic type. Pin
S. Senthil Kumar12-Jan-09 9:55
S. Senthil Kumar12-Jan-09 9:55 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6912-Jan-09 10:07
professionalDaveyM6912-Jan-09 10:07 
GeneralRe: Generic event, or raise event based on generic type. Pin
Mark Churchill12-Jan-09 16:45
Mark Churchill12-Jan-09 16:45 
GeneralRe: Generic event, or raise event based on generic type. Pin
S. Senthil Kumar12-Jan-09 18:40
S. Senthil Kumar12-Jan-09 18:40 
GeneralRe: Generic event, or raise event based on generic type. Pin
Mark Churchill12-Jan-09 19:01
Mark Churchill12-Jan-09 19:01 
QuestionRe: Generic event, or raise event based on generic type. Pin
Wendelius12-Jan-09 11:16
mentorWendelius12-Jan-09 11:16 
AnswerRe: Generic event, or raise event based on generic type. [modified] Pin
DaveyM6912-Jan-09 22:58
professionalDaveyM6912-Jan-09 22:58 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius13-Jan-09 9:47
mentorWendelius13-Jan-09 9:47 
I'm really loosing some hair over here... Can't figure out how to do this fully type safe without boxing. However, I changed the concept a little bit. Instead of using events, I decided to declare a delegate and use a subscribe method. So far I've done the following. In your opinion, are we getting any further?

So far I think the singleton isn't aware of the type anymore. The subsriber is and it subscribes an 'event' based on a type. Also when the 'event' is raised using Notify, the instance is defined for the type. This could be taken furher by adding a reason for an event and that could be delivered to the subscriber or the subscriber could define that it want's only certain event reasons.

static class Program {
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main() {
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);

   int testVariable = 23;
   Singleton a = Singleton.Instance;
   Tester b = new Tester();
   a.Notify(testVariable);
   ...
}

public delegate void NotifyDelegate<T>(T instance);

public class Tester {
   public Tester() {
      Singleton.Instance.Subscribe<int>(this.MethodToCall);
   }
   public void MethodToCall(int value) {
      System.Windows.Forms.MessageBox.Show(value.ToString());
   }
}

public class Singleton {
   private class Subscriber {
      private System.Type _type;
      private object _notifyDelegate;
      internal Subscriber(System.Type type, object notifyDelegate) {
         this._type = type;
         this._notifyDelegate = notifyDelegate;
      }
      internal System.Type Type {
         get {
            return this._type;
         }
      }
      internal NotifyDelegate<T> GetNotifyDelegate<t>() {
         return (NotifyDelegate<T> )this._notifyDelegate;
      }
   }
   private static Singleton _instance;
   private System.Collections.Generic.List<Subscriber> _subsribers = new List<Subscriber>();

   private Singleton() {
      _instance = this;
   }

   public void Subscribe<T>(NotifyDelegate<T> delegateToAdd) {
      this._subsribers.Add(new Subscriber(typeof(T), delegateToAdd));
   }
   public void Notify<T>(T instance) {
      foreach (Subscriber item in this._subsribers) {
         if (item.Type.Equals(typeof(T))) {
            item.GetNotifyDelegate<T>()(instance);
         }
      }
   }

   public static Singleton Instance {
      get {
         if (_instance == null) {
            _instance = new Singleton();
         }
         return _instance;
      }
   }
}</t>


The need to optimize rises from a bad design.My articles[^]

GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6914-Jan-09 0:23
professionalDaveyM6914-Jan-09 0:23 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius14-Jan-09 2:30
mentorWendelius14-Jan-09 2:30 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6914-Jan-09 3:25
professionalDaveyM6914-Jan-09 3:25 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius14-Jan-09 3:42
mentorWendelius14-Jan-09 3:42 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6914-Jan-09 11:41
professionalDaveyM6914-Jan-09 11:41 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius14-Jan-09 11:59
mentorWendelius14-Jan-09 11:59 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6915-Jan-09 3:13
professionalDaveyM6915-Jan-09 3:13 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius15-Jan-09 3:19
mentorWendelius15-Jan-09 3:19 
GeneralRe: Generic event, or raise event based on generic type. Pin
DaveyM6915-Jan-09 4:01
professionalDaveyM6915-Jan-09 4:01 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius15-Jan-09 10:27
mentorWendelius15-Jan-09 10:27 
GeneralRe: Generic event, or raise event based on generic type. [modified] Pin
DaveyM6915-Jan-09 23:08
professionalDaveyM6915-Jan-09 23:08 
GeneralRe: Generic event, or raise event based on generic type. Pin
Wendelius15-Jan-09 23:25
mentorWendelius15-Jan-09 23:25 
QuestionKeep current view centered on Zoom Pin
Richard Blythe12-Jan-09 7:16
Richard Blythe12-Jan-09 7:16 
AnswerRe: Keep current view centered on Zoom [modified] Pin
Luc Pattyn12-Jan-09 8:01
sitebuilderLuc Pattyn12-Jan-09 8:01 
GeneralRe: Keep current view centered on Zoom Pin
Richard Blythe12-Jan-09 9:10
Richard Blythe12-Jan-09 9:10 
QuestionAddress Pin
boiDev12-Jan-09 6:56
boiDev12-Jan-09 6:56 
AnswerRe: Address Pin
EliottA12-Jan-09 7:21
EliottA12-Jan-09 7:21 

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.