Mont,
Generally speaking, you always have to use a method delegate to pass a
"method" as a parameter to a function. To make the whole syntax easier,
the C# 2.0 compiler can infer the delegate type you are using in an
expression from the called method signature, in addition to the one of
the method you are passing as a parameter. Here's a short example which
I hope would make it clearer:
// A simple delegate
delegate void TestDelegate(int a);
// A simple method that would match TestDelegate signature
void MethodToPass(int firstParameter)
{
}
// A method that would accept a method with a TestDelegate signature as
its parameter
void TestCompilerInference(TestDelegate myMethod)
{
}
Now suppose you have to call the aforementioned TestCompilerInference()
method, passing a delegate to the MethodToPass() method. You have two
possible solutions for this one; the standard method:
TestCompilerInference(new TestDelegate(MethodToPass));
And the following one:
TestCompilerInference(MethodToPass);
Note that both ways will produce the same IL code; in the second case
the C# 2.0 compiler will infer the right delegate type from the
signatures of the methods you are using.
Hope this helps.
--
Efran Cobisi
http://www.cobisi.com
Mont Rothstein wrote:
Peter, your reply appears to have been cut off.
My question for both you and Alex (thank you both for responding) is how to
do this generically.
Perhaps a little more detail will help.
I found a bit of Microsoft code for Command Management (
http://msdn.microsoft.com/msdnmag/issues/02/10/CommandManagement/).
In it when creating a command you pass in a delegate. Ex:
new Command(
"EditCopy",
new Command.ExecuteHandler(OnCopy),
new Command.UpdateHandler(UpdateCopyCommand))
When doing this over and over again it would be much easier if the above
method call looked like:
new Command("EditCopy", OnCopy, UpdateCopyCommand)
And then I could create the delegate inside the method.
This is what I am trying to figure out how to do.
If this is possible and someone could show me how to declare a method that
would accept methods like the above example I would appreciate it.
Thanks,
-Mont
On 9/1/07, Peter Ritchie <
[EMAIL PROTECTED]> wrote:
Yes, in safe managed code it's with a delegate. You need to keep the
object pointer and the method pointer pair together, which is what a
delegate does for you. You'll have to declare the
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com