I have a general event handler function. and has a general object passed to it, but I have to use a "is" or "as" to decode the type, is it possible that the base class can detect the type and call the function for that type I don't what to use methodinvoke because it is so slowwwwww. Another apporach would be to anonymous delegates would help but I don't like putting several delegates in one method to handle each type.. and I would have to use continutions to continue the event pump... (ie see robots framework for an example)
And I don't want to declare delegates for each type because this means more work for the programmer...
void handler(baseobject ev){
if (ev is MyEvent)
{
MyEvent evmy= (MyEvent)ev);
}
else if ...
}
better would be
void handler( MyEvent ev)
{
}
void handler(FEvent ev)
{
}

is as Keyword
Spoofer
I have a base class doing the dispatching, drived classes can queue drived datatypes, I want to delcare a function with the drived data types and have the dispatcher map to the function that matches the data type. of course I can use reflection to get the functions, but the problem is how to call the function.
This way, programmers can have event handlers and be called based on the data type they attached to, the dispatching loop in Windows uses EventHander which is ok, if you know the type, but I want to have strong typed dispatching and not having to rely on decoding the object...
for example base dispatcher
void queueMsg(object msg)
{
queue.add(msg);
}
void dispatcher()
{
queue.waitany();
object msg = queue.dequeue();
dispatchToFunction(msg);
}
derived class...
void handler (evt msg) { }
void handler(evtMouse msg){}
dr.grumbles
That is where the issue is, I have used a simple abstract class before, but the function arugments must be the same and that is where the issue is, I want to register callbacks based on types, thus makes in simple to handle multiple types per event. Also, the issue with the delegate is you need to create the type. so... from your example the function
ht.Add("EventName",delegate); The user has to create a delegate type ie declare a delegate type then do Delegate.CreateDelegate in this function so the user code looks like this( shown below) or you have to create an anonymous delegate to then call the your function..ie ht.Add("EventName", delegate(object parm) { handleEvent((type1)parm);}); and let the dispatcher call which delegate based on that type which is in the dispatch queue. This is very simlar to the CCR in the Robitics SDK. The problem is I wnat a little more code reuse and have it in functions instead of having anonymous delegates per state...for example
action() { blockOnTypes( delegate( Execption ex){ ...}, delegate(int i){....}, delegate(string oo){....}) } What this does is register delegates with the dispatcher and waits on the types, which is great but the syntax is a little hard to understand.. plus within the delegates, the code maybe the same, so it needs to call a function.. but the code is a little messy, but maybe this is the best way... instead if LCG, and slow method.invoke..
delegate void func1(type1 parm);
delegate void func1(type1 parm2);
class userClass
{
void handleEvent(type1 parm){}
void handleEvent2(type2 parm){}
void registerfunctions()
{
ht.Add("func1", Delegate.CreateDelegate(func1, methodInfo.....); or ht.Add("fun1", new func1(handleEvent));
}
What I want is not to have the programmer create delegate types and use reflection to do something simalr to methed.invoke, but with the speed of delegates, there are methods like LCG (Light weight code generation) to create a stub to the function.
for example declare the userClass, use reflection to get the methodInfo, create a methed invoke using LGC so when the data is queued the type of the queed data then class the LCG method to call the correct funfion, just like method.invoke but with out a lot of overhead. But I don't want to use LCG
class userClass{
{RegisterHandler]
public void handler1(type2 parm){}
[(RegisterHandler]
public void handler2(type2 parm{}
}
Louisd
RJMPhD
AlucardHellSing
That is where the issue is, I have used a simple abstract class before, but the function arugments must be the same and that is where the issue is, I want to register callbacks based on types, thus makes in simple to handle multiple types per event. Also, the issue with the delegate is you need to create the type. so... from your example the function
ht.Add("EventName",delegate); The user has to create a delegate type ie declare a delegate type then do Delegate.CreateDelegate in this function so the user code looks like this( shown below) or you have to create an anonymous delegate to then call the your function..ie ht.Add("EventName", delegate(object parm) { handleEvent((type1)parm);}); and let the dispatcher call which delegate based on that type which is in the dispatch queue. This is very simlar to the CCR in the Robitics SDK. The problem is I wnat a little more code reuse and have it in functions instead of having anonymous delegates per state...for example
action() { blockOnTypes( delegate( Execption ex){ ...}, delegate(int i){....}, delegate(string oo){....}) } What this does is register delegates with the dispatcher and waits on the types, which is great but the syntax is a little hard to understand.. plus within the delegates, the code maybe the same, so it needs to call a function.. but the code is a little messy, but maybe this is the best way... instead if LCG, and slow method.invoke..
delegate void func1(type1 parm);
delegate void func1(type1 parm2);
class userClass
{
void handleEvent(type1 parm){}
void handleEvent2(type2 parm){}
void registerfunctions()
{
ht.Add("func1", Delegate.CreateDelegate(func1, methodInfo.....); or ht.Add("fun1", new func1(handleEvent));
}
What I want is not to have the programmer create delegate types and use reflection to do something simalr to methed.invoke, but with the speed of delegates, there are methods like LCG (Light weight code generation) to create a stub to the function.
for example declare the userClass, use reflection to get the methodInfo, create a methed invoke using LGC so when the data is queued the type of the queed data then class the LCG method to call the correct funfion, just like method.invoke but with out a lot of overhead. But I don't want to use LCG
class userClass{
{RegisterHandler]
public void handler1(type2 parm){}
[(RegisterHandler]
public void handler2(type2 parm{}
}
Andreia M
outcast1881
here is a link to the design pattern...
http://www.dofactory.com/Patterns/PatternTemplate.aspx
im not sure whether i got ur problem correctly..
i faced a similar situation where i used a hashtable to map function to a event( in my case a
service request ID)
something like
Hashtable ht;
ht.Add("EventName",delegate);
ht.Add("EventName",delegate);
ht.Add("EventName",delegate);
ht.Add("EventName",delegate);
so in dispatch i would use
ht["EventName"] and call the delegate.. ..
but if the number of different kinds of events wont change very frequenly, then u use the
base class with all handlers declared virtuals and derived class will override the handlers
...i think function call is faster than a delegate call
Lucas Pasquali
The normal way to do this would be to have a virtual method in the base class that you can call to get whatever behaviour you want from the derived classes.
Why do you need to know the exact type