编译器需要保存您的参数 (someParam) 值,因为您稍后会在本地函数中递增它。所以在这种情况下它不能使用单例。
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using System.Threading;
[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 LocalFunctionTest
{
[CompilerGenerated]
private sealed class <>c__DisplayClass1_0
{
public int someParam;
private void <UnsubscribeSubscribe>g__EventHandlerWithClosure|1(object sender, EventArgs args)
{
someParam++;
}
}
[Serializable]
[CompilerGenerated]
private sealed class <>c
{
public static readonly <>c <>9 = new <>c();
private void <UnsubscribeSubscribe>g__EventHandlerWithoutClosure|1_0(object sender, EventArgs args)
{
}
}
[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private EventHandler m_EventWithoutClosure;
[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private EventHandler m_EventWithClosure;
private event EventHandler EventWithoutClosure
{
[CompilerGenerated]
add
{
EventHandler eventHandler = this.m_EventWithoutClosure;
EventHandler eventHandler2;
do
{
eventHandler2 = eventHandler;
EventHandler value2 = (EventHandler)Delegate.Combine(eventHandler2, value);
eventHandler = Interlocked.CompareExchange(ref this.m_EventWithoutClosure, value2, eventHandler2);
}
while ((object)eventHandler != eventHandler2);
}
[CompilerGenerated]
remove
{
EventHandler eventHandler = this.m_EventWithoutClosure;
EventHandler eventHandler2;
do
{
eventHandler2 = eventHandler;
EventHandler value2 = (EventHandler)Delegate.Remove(eventHandler2, value);
eventHandler = Interlocked.CompareExchange(ref this.m_EventWithoutClosure, value2, eventHandler2);
}
while ((object)eventHandler != eventHandler2);
}
}
private event EventHandler EventWithClosure
{
[CompilerGenerated]
add
{
EventHandler eventHandler = this.m_EventWithClosure;
EventHandler eventHandler2;
do
{
eventHandler2 = eventHandler;
EventHandler value2 = (EventHandler)Delegate.Combine(eventHandler2, value);
eventHandler = Interlocked.CompareExchange(ref this.m_EventWithClosure, value2, eventHandler2);
}
while ((object)eventHandler != eventHandler2);
}
[CompilerGenerated]
remove
{
EventHandler eventHandler = this.m_EventWithClosure;
EventHandler eventHandler2;
do
{
eventHandler2 = eventHandler;
EventHandler value2 = (EventHandler)Delegate.Remove(eventHandler2, value);
eventHandler = Interlocked.CompareExchange(ref this.m_EventWithClosure, value2, eventHandler2);
}
while ((object)eventHandler != eventHandler2);
}
}
public static void Main(string[] args)
{
LocalFunctionTest localFunctionTest = new LocalFunctionTest();
localFunctionTest.UnsubscribeSubscribe(1);
localFunctionTest.UnsubscribeSubscribe(10);
localFunctionTest.UnsubscribeSubscribe(100);
EventHandler eventWithoutClosure = localFunctionTest.m_EventWithoutClosure;
Console.WriteLine((eventWithoutClosure != null) ? eventWithoutClosure.GetInvocationList().Length : 0);
EventHandler eventWithClosure = localFunctionTest.m_EventWithClosure;
Console.WriteLine((eventWithClosure != null) ? eventWithClosure.GetInvocationList().Length : 0);
}
private void UnsubscribeSubscribe(int someParam)
{
<>c__DisplayClass1_0 <>c__DisplayClass1_ = new <>c__DisplayClass1_0();
<>c__DisplayClass1_.someParam = someParam;
EventWithoutClosure -= new EventHandler(<>c.<>9.<UnsubscribeSubscribe>g__EventHandlerWithoutClosure|1_0);
EventWithoutClosure += new EventHandler(<>c.<>9.<UnsubscribeSubscribe>g__EventHandlerWithoutClosure|1_0);
EventWithClosure -= new EventHandler(<>c__DisplayClass1_.<UnsubscribeSubscribe>g__EventHandlerWithClosure|1);
EventWithClosure += new EventHandler(<>c__DisplayClass1_.<UnsubscribeSubscribe>g__EventHandlerWithClosure|1);
}
}