L
LRaiz
Can one of the gurus tell me if it is possible (and how) to do it in
c#?
I have a method that processes web requests using XmlTextReader. There
are many kinds of requests and a fair amount of code is repetitive
(initialization, cleanup, error handling, etc.) The only difference is
in processing of individual XmlElements. Thus I am trying to create a
utility which would accept as an argument a delegate to deal with
specifics of a particular request. Since processing is quite different
in every case it is tempting to have a variable number of arguments.
Here is how I attempt to do it.
void delegate ReadDelegate(XmlTextReader reader, params object[]
args);
void ProcessRequest(ReadDelegate dlg, params obj[] args)
{
....
XmlTextReader reader = ..
while( reader.Read() )
dlg(reader, args);
.....
}
So far so good but the problems comes later when I try to use it with
specific handlers that have non-variable number of arguments.
For example -
void ReadExample( XmlTextReader reader, string str)
{ ..}
The statement below would not complile
ProcessRequest( new ReadDelegate(ReadExample), new object[]
{"something"} )
ReadExample signature does not match ReadDelegate.
On one hand I don't want to repeat the code in ProcessRequest but on
the other hand various handlers must have various signatures with
specific number of arguments. Is there a way to handle the dilemma in
c#?
c#?
I have a method that processes web requests using XmlTextReader. There
are many kinds of requests and a fair amount of code is repetitive
(initialization, cleanup, error handling, etc.) The only difference is
in processing of individual XmlElements. Thus I am trying to create a
utility which would accept as an argument a delegate to deal with
specifics of a particular request. Since processing is quite different
in every case it is tempting to have a variable number of arguments.
Here is how I attempt to do it.
void delegate ReadDelegate(XmlTextReader reader, params object[]
args);
void ProcessRequest(ReadDelegate dlg, params obj[] args)
{
....
XmlTextReader reader = ..
while( reader.Read() )
dlg(reader, args);
.....
}
So far so good but the problems comes later when I try to use it with
specific handlers that have non-variable number of arguments.
For example -
void ReadExample( XmlTextReader reader, string str)
{ ..}
The statement below would not complile
ProcessRequest( new ReadDelegate(ReadExample), new object[]
{"something"} )
ReadExample signature does not match ReadDelegate.
On one hand I don't want to repeat the code in ProcessRequest but on
the other hand various handlers must have various signatures with
specific number of arguments. Is there a way to handle the dilemma in
c#?