Click here to Skip to main content
15,888,113 members
Home / Discussions / C#
   

C#

 
GeneralRe: Creating a Sound(say, a Beep) Pin
Ravi Bhavnani10-Dec-07 11:18
professionalRavi Bhavnani10-Dec-07 11:18 
GeneralRe: Creating a Sound(say, a Beep) Pin
PIEBALDconsult10-Dec-07 12:20
mvePIEBALDconsult10-Dec-07 12:20 
GeneralRe: Creating a Sound(say, a Beep) Pin
JoeRip10-Dec-07 14:22
JoeRip10-Dec-07 14:22 
GeneralRe: Creating a Sound(say, a Beep) Pin
polishprogrammer11-Dec-07 9:18
polishprogrammer11-Dec-07 9:18 
QuestionCasting from a Type object Pin
Leonardo Pelisoli10-Dec-07 10:55
Leonardo Pelisoli10-Dec-07 10:55 
GeneralRe: Casting from a Type object Pin
m@u10-Dec-07 11:54
m@u10-Dec-07 11:54 
GeneralRe: Casting from a Type object [modified] Pin
PIEBALDconsult10-Dec-07 12:25
mvePIEBALDconsult10-Dec-07 12:25 
GeneralRe: Casting from a Type object Pin
PIEBALDconsult10-Dec-07 14:20
mvePIEBALDconsult10-Dec-07 14:20 
OK, here's another way, using Reflection as m@u suggested, but rather than going to Reflection for each incoming message (which is very costly) I do it once in a static constructor and populate a static dictionary (my latest favourite toy). It's still not as clean as I'd like, but it's fun!

C#
namespace ConsoleApplication1
{    
    class BaseClass    {    }    
    class ClassA : BaseClass    {    }    
    class ClassB : BaseClass    {    }    
 
    class Program    
    {
        private static readonly System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodInfo> dic ;
        
        static Program
        (
        )
        {
            System.Reflection.ParameterInfo[] pi ;
            System.Type                       pt ;
             
            dic = new System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodInfo>() ;
 
            foreach 
            ( 
                System.Reflection.MethodInfo mi 
            in 
                typeof(Program).GetMethods()
            )
            {
                if ( ( mi.Name == "TreatClass" ) && ( mi.ReturnType == typeof(void) ) )
                {
                    pi = mi.GetParameters() ;
                    
                    if ( pi.Length == 1 )
                    {
                        pt = pi [ 0 ].ParameterType ;
                        
                        if ( pt.IsSubclassOf ( typeof(BaseClass) ) )
                        {
                            dic.Add
                            (
                                pt
                            ,
                                mi
                            ) ;
                        }
                    }
                }
            }
        }
 
        static void Main(string[] args)
        {            
            BaseClass obj1 = new ClassA();            
            BaseClass obj2 = new ClassB();            
 
            Program prog = new Program();            
 
            prog.TreatAnything(obj1);            
            prog.TreatAnything(obj2);        
        }
        
        public void TreatAnything(BaseClass obj)       
        {            
            if ( dic.ContainsKey ( obj.GetType() ) )
            {
                dic [ obj.GetType() ].Invoke ( this , new object[] { obj } ) ;
            }
        }       
 
        public void TreatClass(ClassA obj)        
        {            
            System.Console.WriteLine ( "A" ) ;
        }        

        public void TreatClass(ClassB obj)        
        {            
            System.Console.WriteLine ( "B" ) ;
        }    
    }
}

GeneralRe: Casting from a Type object Pin
Leonardo Pelisoli11-Dec-07 12:14
Leonardo Pelisoli11-Dec-07 12:14 
GeneralC# Properties - Stack overflow. Pin
gunner_uk200010-Dec-07 10:34
gunner_uk200010-Dec-07 10:34 
GeneralRe: C# Properties - Stack overflow. Pin
Luc Pattyn10-Dec-07 10:43
sitebuilderLuc Pattyn10-Dec-07 10:43 
GeneralRe: C# Properties - Stack overflow. Pin
martin_hughes10-Dec-07 10:45
martin_hughes10-Dec-07 10:45 
GeneralRe: C# Properties - Stack overflow. Pin
hofmadresu10-Dec-07 17:17
hofmadresu10-Dec-07 17:17 
GeneralMouse_Click on User Control class Pin
Fragment10-Dec-07 9:42
Fragment10-Dec-07 9:42 
Generalreal time timer Pin
iman_kh10-Dec-07 8:53
iman_kh10-Dec-07 8:53 
GeneralRe: real time timer Pin
nallanch_srinivas10-Dec-07 9:00
nallanch_srinivas10-Dec-07 9:00 
GeneralRe: real time timer Pin
Luc Pattyn10-Dec-07 9:18
sitebuilderLuc Pattyn10-Dec-07 9:18 
GeneralRe: real time timer Pin
Pete O'Hanlon10-Dec-07 9:39
mvePete O'Hanlon10-Dec-07 9:39 
GeneralRe: real time timer Pin
Luc Pattyn10-Dec-07 9:48
sitebuilderLuc Pattyn10-Dec-07 9:48 
GeneralRe: real time timer Pin
Pete O'Hanlon10-Dec-07 9:55
mvePete O'Hanlon10-Dec-07 9:55 
GeneralRe: real time timer Pin
Luc Pattyn10-Dec-07 10:12
sitebuilderLuc Pattyn10-Dec-07 10:12 
GeneralRe: real time timer Pin
Pete O'Hanlon10-Dec-07 10:18
mvePete O'Hanlon10-Dec-07 10:18 
GeneralRe: real time timer Pin
Luc Pattyn10-Dec-07 10:28
sitebuilderLuc Pattyn10-Dec-07 10:28 
GeneralRe: real time timer Pin
Skippums10-Dec-07 9:20
Skippums10-Dec-07 9:20 
QuestionUnable to debug..Please help Pin
nallanch_srinivas10-Dec-07 8:53
nallanch_srinivas10-Dec-07 8:53 

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.