方法是否转换为委托?
Do methods are transformed into delegates?
我对委托的理解是委托是一个特殊的对象,它包含指向方法的指针序列(每个方法应该具有相同的return类型和参数)。
考虑一下我们是这样定义委托的
delegate int MyDelegate(string name, int age);
我们以后可以像那样在代码中使用它
// creating the instance of delegate
MyDelegate objDelegate = null;
// initialising that instance
objDelegate += (string name, int age) =>
{
Console.WriteLine("Anonymous method is starting to work");
return age + 2;
};
// execution of some method whith delegate as last parameter
objProgram.PrintBaseInfo("example", -2, objDelegate);
但为什么我可以在不为它们创建委托的情况下传递方法?
objProgram.PrintBaseInfo("Alexandr", 23, anotherClass.MyMethod);
在上面的示例中,我只是传递了一些 class 的方法(不是委托)。我的假设是编译器为它创建委托并用这个方法初始化它,但我不确定。
PS 只是为了消除对问题的误解,这里是 PrintBaseInfo 的定义:
public void PrintBaseInfo(string name, int age, MyDelegate InnerFunc){some code}
你是对的,编译器会为你创建委托。
using System;
public class SomeClass
{
public void TakesAction(Action action)
{
}
public void CallsTakesAction()
{
TakesAction(MethodName);
}
public static void MethodName()
{
}
}
编译为:
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
public class SomeClass
{
public void TakesAction(Action action)
{
}
public void CallsTakesAction()
{
TakesAction(new Action(MethodName));
}
public static void MethodName()
{
}
}
我对委托的理解是委托是一个特殊的对象,它包含指向方法的指针序列(每个方法应该具有相同的return类型和参数)。
考虑一下我们是这样定义委托的
delegate int MyDelegate(string name, int age);
我们以后可以像那样在代码中使用它
// creating the instance of delegate
MyDelegate objDelegate = null;
// initialising that instance
objDelegate += (string name, int age) =>
{
Console.WriteLine("Anonymous method is starting to work");
return age + 2;
};
// execution of some method whith delegate as last parameter
objProgram.PrintBaseInfo("example", -2, objDelegate);
但为什么我可以在不为它们创建委托的情况下传递方法?
objProgram.PrintBaseInfo("Alexandr", 23, anotherClass.MyMethod);
在上面的示例中,我只是传递了一些 class 的方法(不是委托)。我的假设是编译器为它创建委托并用这个方法初始化它,但我不确定。
PS 只是为了消除对问题的误解,这里是 PrintBaseInfo 的定义:
public void PrintBaseInfo(string name, int age, MyDelegate InnerFunc){some code}
你是对的,编译器会为你创建委托。
using System;
public class SomeClass
{
public void TakesAction(Action action)
{
}
public void CallsTakesAction()
{
TakesAction(MethodName);
}
public static void MethodName()
{
}
}
编译为:
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
public class SomeClass
{
public void TakesAction(Action action)
{
}
public void CallsTakesAction()
{
TakesAction(new Action(MethodName));
}
public static void MethodName()
{
}
}