@@ -67,17 +67,16 @@ public ObserverGrain()
6767 if ( ! sortList . Contains ( item ) )
6868 sortList . Add ( item ) ;
6969 }
70+ var declare_1 = ilGen . DeclareLocal ( typeof ( Task ) ) ;
7071 var defaultLabel = ilGen . DefineLabel ( ) ;
7172 var lastLable = ilGen . DefineLabel ( ) ;
72- var declare_1 = ilGen . DeclareLocal ( typeof ( Task ) ) ;
73- var isShort = sortList . Count < 12 ;
7473 foreach ( var item in sortList )
7574 {
7675 ilGen . Emit ( OpCodes . Ldarg_1 ) ;
7776 ilGen . Emit ( OpCodes . Isinst , item . CaseType ) ;
7877 if ( item . Index > 3 )
7978 {
80- if ( isShort )
79+ if ( item . DeclareLocal . LocalIndex > 0 && item . DeclareLocal . LocalIndex <= 255 )
8180 {
8281 ilGen . Emit ( OpCodes . Stloc_S , item . DeclareLocal ) ;
8382 ilGen . Emit ( OpCodes . Ldloc_S , item . DeclareLocal ) ;
@@ -111,10 +110,12 @@ public ObserverGrain()
111110 ilGen . Emit ( OpCodes . Ldloc_3 ) ;
112111 }
113112 }
114-
115- ilGen . Emit ( OpCodes . Brtrue , item . Lable ) ;
113+ if ( item . DeclareLocal . LocalIndex > 0 && item . DeclareLocal . LocalIndex < 255 )
114+ ilGen . Emit ( OpCodes . Brtrue_S , item . Lable ) ;
115+ else
116+ ilGen . Emit ( OpCodes . Brtrue , item . Lable ) ;
116117 }
117- if ( isShort )
118+ if ( declare_1 . LocalIndex > 0 && declare_1 . LocalIndex <= 255 )
118119 ilGen . Emit ( OpCodes . Br_S , defaultLabel ) ;
119120 else
120121 ilGen . Emit ( OpCodes . Br , defaultLabel ) ;
@@ -124,34 +125,36 @@ public ObserverGrain()
124125 ilGen . Emit ( OpCodes . Ldarg_0 ) ;
125126 //加载第一个参数
126127 if ( item . Parameters [ 0 ] . ParameterType == item . CaseType )
127- LdEventArgs ( item , ilGen , isShort ) ;
128+ LdEventArgs ( item , ilGen ) ;
128129 else if ( item . Parameters [ 0 ] . ParameterType == typeof ( EventBase ) )
129130 ilGen . Emit ( OpCodes . Ldarg_2 ) ;
130131 //加载第二个参数
131132 if ( item . Parameters . Length == 2 )
132133 {
133134 if ( item . Parameters [ 1 ] . ParameterType == item . CaseType )
134- LdEventArgs ( item , ilGen , isShort ) ;
135+ LdEventArgs ( item , ilGen ) ;
135136 else if ( item . Parameters [ 1 ] . ParameterType == typeof ( EventBase ) )
136137 ilGen . Emit ( OpCodes . Ldarg_2 ) ;
137138 }
138139 ilGen . Emit ( OpCodes . Call , item . Mehod ) ;
139- if ( isShort )
140+ if ( item . DeclareLocal . LocalIndex > 0 && item . DeclareLocal . LocalIndex <= 255 )
140141 {
141142 ilGen . Emit ( OpCodes . Stloc_S , declare_1 ) ;
142- ilGen . Emit ( OpCodes . Br_S , lastLable ) ;
143143 }
144144 else
145145 {
146146 ilGen . Emit ( OpCodes . Stloc , declare_1 ) ;
147- ilGen . Emit ( OpCodes . Br , lastLable ) ;
148147 }
148+ if ( declare_1 . LocalIndex > 0 && declare_1 . LocalIndex <= 255 )
149+ ilGen . Emit ( OpCodes . Br_S , lastLable ) ;
150+ else
151+ ilGen . Emit ( OpCodes . Br , lastLable ) ;
149152 }
150153 ilGen . MarkLabel ( defaultLabel ) ;
151154 ilGen . Emit ( OpCodes . Ldarg_0 ) ;
152155 ilGen . Emit ( OpCodes . Ldarg_1 ) ;
153156 ilGen . Emit ( OpCodes . Call , GrainType . GetMethod ( nameof ( DefaultHandler ) ) ) ;
154- if ( isShort )
157+ if ( declare_1 . LocalIndex > 0 && declare_1 . LocalIndex <= 255 )
155158 {
156159 ilGen . Emit ( OpCodes . Stloc_S , declare_1 ) ;
157160 ilGen . Emit ( OpCodes . Br_S , lastLable ) ;
@@ -163,18 +166,18 @@ public ObserverGrain()
163166 }
164167 //last
165168 ilGen . MarkLabel ( lastLable ) ;
166- if ( isShort )
169+ if ( declare_1 . LocalIndex > 0 && declare_1 . LocalIndex <= 255 )
167170 ilGen . Emit ( OpCodes . Ldloc_S , declare_1 ) ;
168171 else
169172 ilGen . Emit ( OpCodes . Ldloc , declare_1 ) ;
170173 ilGen . Emit ( OpCodes . Ret ) ;
171174 handlerInvokeFunc = ( Func < object , IEvent , EventBase , Task > ) dynamicMethod . CreateDelegate ( typeof ( Func < object , IEvent , EventBase , Task > ) ) ;
172175 //加载Event参数
173- static void LdEventArgs ( SwitchMethodEmit item , ILGenerator gen , bool isShort )
176+ static void LdEventArgs ( SwitchMethodEmit item , ILGenerator gen )
174177 {
175178 if ( item . Index > 3 )
176179 {
177- if ( isShort )
180+ if ( item . DeclareLocal . LocalIndex > 0 && item . DeclareLocal . LocalIndex <= 255 )
178181 gen . Emit ( OpCodes . Ldloc_S , item . DeclareLocal ) ;
179182 else
180183 gen . Emit ( OpCodes . Ldloc , item . DeclareLocal ) ;
0 commit comments