| | | 1 | | // |
| | | 2 | | // Copyright 2010 Ekon Benefits |
| | | 3 | | // |
| | | 4 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | | 5 | | // you may not use this file except in compliance with the License. |
| | | 6 | | // You may obtain a copy of the License at |
| | | 7 | | // |
| | | 8 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | | 9 | | // |
| | | 10 | | // Unless required by applicable law or agreed to in writing, software |
| | | 11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | | 12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | 13 | | // See the License for the specific language governing permissions and |
| | | 14 | | // limitations under the License. |
| | | 15 | | |
| | | 16 | | using System.Linq.Expressions; |
| | | 17 | | |
| | | 18 | | namespace ImpromptuInterface.Build |
| | | 19 | | { |
| | | 20 | | using System; |
| | | 21 | | using System.Collections.Generic; |
| | | 22 | | using System.Linq; |
| | | 23 | | using System.Reflection; |
| | | 24 | | using System.Reflection.Emit; |
| | | 25 | | using System.Runtime.CompilerServices; |
| | | 26 | | using Microsoft.CSharp.RuntimeBinder; |
| | | 27 | | using Binder = Microsoft.CSharp.RuntimeBinder.Binder; |
| | | 28 | | |
| | | 29 | | ///<summary> |
| | | 30 | | /// Extension Methods that make emiting code easier and easier to read |
| | | 31 | | ///</summary> |
| | | 32 | | public static class EmitExtensions |
| | | 33 | | { |
| | | 34 | | ///<summary> |
| | | 35 | | /// Used to automatically create label on dispose |
| | | 36 | | ///</summary> |
| | | 37 | | public class BranchTrueOverBlock : IDisposable |
| | | 38 | | { |
| | | 39 | | private readonly ILGenerator _generator; |
| | | 40 | | private readonly Label _label; |
| | | 41 | | |
| | | 42 | | ///<summary> |
| | | 43 | | /// Constructor |
| | | 44 | | ///</summary> |
| | | 45 | | ///<param name="generator"></param> |
| | 1657 | 46 | | public BranchTrueOverBlock(ILGenerator generator) |
| | | 47 | | { |
| | 1657 | 48 | | _generator = generator; |
| | 1657 | 49 | | _label = generator.DefineLabel(); |
| | 1657 | 50 | | _generator.Emit(OpCodes.Brtrue, _label); |
| | 1657 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Finishes block |
| | | 55 | | /// </summary> |
| | | 56 | | public void Dispose() |
| | | 57 | | { |
| | | 58 | | //_generator.Emit(OpCodes.Br_S, _label); |
| | 1657 | 59 | | _generator.MarkLabel(_label); |
| | 1657 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public class BranchFalseOverBlock : IDisposable |
| | | 64 | | { |
| | | 65 | | private readonly ILGenerator _generator; |
| | | 66 | | private readonly Label _label; |
| | | 67 | | |
| | | 68 | | ///<summary> |
| | | 69 | | /// Constructor |
| | | 70 | | ///</summary> |
| | | 71 | | ///<param name="generator"></param> |
| | 227 | 72 | | public BranchFalseOverBlock(ILGenerator generator) |
| | | 73 | | { |
| | 227 | 74 | | _generator = generator; |
| | 227 | 75 | | _label = generator.DefineLabel(); |
| | 227 | 76 | | _generator.Emit(OpCodes.Brfalse, _label); |
| | 227 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Finishes block |
| | | 81 | | /// </summary> |
| | | 82 | | public void Dispose() |
| | | 83 | | { |
| | | 84 | | //_generator.Emit(OpCodes.Br_S, _label); |
| | 227 | 85 | | _generator.MarkLabel(_label); |
| | 227 | 86 | | } |
| | | 87 | | } |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the field info even if generic type parameter. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="type">The type.</param> |
| | | 92 | | /// <param name="fieldName">Name of the field.</param> |
| | | 93 | | /// <returns></returns> |
| | | 94 | | public static FieldInfo GetFieldEvenIfGeneric(this Type type, string fieldName) |
| | | 95 | | { |
| | 2843 | 96 | | if (type is TypeBuilder |
| | 2843 | 97 | | || type.GetType().Name.Contains("TypeBuilder") |
| | 2843 | 98 | | || type.GetType().Name.Contains("MonoGenericClass") |
| | 2843 | 99 | | ) |
| | | 100 | | { |
| | 602 | 101 | | var tGenDef = type.GetGenericTypeDefinition(); |
| | 602 | 102 | | var tField = tGenDef.GetField(fieldName); |
| | 602 | 103 | | if (tField == null) |
| | 0 | 104 | | return null; |
| | 602 | 105 | | return TypeBuilder.GetField(type, tField); |
| | | 106 | | } |
| | 2241 | 107 | | return type.GetField(fieldName); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Gets the method info even if generic type parameter. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="type">The type.</param> |
| | | 116 | | /// <param name="methodName">Name of the method.</param> |
| | | 117 | | /// <param name="argTypes">The arg types.</param> |
| | | 118 | | /// <returns></returns> |
| | | 119 | | public static MethodInfo GetMethodEvenIfGeneric(this Type type, string methodName, Type[] argTypes) |
| | | 120 | | { |
| | 1420 | 121 | | if (type is TypeBuilder |
| | 1420 | 122 | | || type.GetType().Name.Contains("TypeBuilder") |
| | 1420 | 123 | | || type.GetType().Name.Contains("MonoGenericClass") |
| | 1420 | 124 | | |
| | 1420 | 125 | | ) |
| | | 126 | | { |
| | 284 | 127 | | var tGenDef = type.GetGenericTypeDefinition(); |
| | 284 | 128 | | var tMethodInfo = tGenDef.GetMethod(methodName, argTypes); |
| | 284 | 129 | | return TypeBuilder.GetMethod(type, tMethodInfo); |
| | | 130 | | } |
| | 1136 | 131 | | return type.GetMethod(methodName, argTypes); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Gets the method info even if generic type parameter. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="type">The type.</param> |
| | | 140 | | /// <param name="methodName">Name of the method.</param> |
| | | 141 | | /// <returns></returns> |
| | | 142 | | public static MethodInfo GetMethodEvenIfGeneric(this Type type, string methodName) |
| | | 143 | | { |
| | 1420 | 144 | | if (type is TypeBuilder |
| | 1420 | 145 | | || type.GetType().Name.Contains("TypeBuilder") |
| | 1420 | 146 | | || type.GetType().Name.Contains("MonoGenericClass") |
| | 1420 | 147 | | |
| | 1420 | 148 | | ) |
| | | 149 | | { |
| | 284 | 150 | | var tGenDef = type.GetGenericTypeDefinition(); |
| | 284 | 151 | | var tMethodInfo = tGenDef.GetMethod(methodName); |
| | 284 | 152 | | return TypeBuilder.GetMethod(type, tMethodInfo); |
| | | 153 | | } |
| | 1136 | 154 | | return type.GetMethod(methodName); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Emits branch true. expects using keyword. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <param name="generator">The generator.</param> |
| | | 163 | | /// <param name="condition">The condition.</param> |
| | | 164 | | /// <returns></returns> |
| | | 165 | | /// <example> |
| | | 166 | | /// Using keyword allows you to set the emit code you are branching over and then automatically emits label when |
| | | 167 | | /// <code> |
| | | 168 | | /// <![CDATA[ |
| | | 169 | | /// using (tIlGen.EmitBranchTrue(g=>g.Emit(OpCodes.Ldsfld, tConvertField))) |
| | | 170 | | /// { |
| | | 171 | | /// tIlGen.EmitDynamicConvertBinder(CSharpBinderFlags.None, returnType, contextType); |
| | | 172 | | /// tIlGen.EmitCallsiteCreate(convertFuncType); |
| | | 173 | | /// tIlGen.Emit(OpCodes.Stsfld, tConvertField); |
| | | 174 | | /// } |
| | | 175 | | /// ]]> |
| | | 176 | | /// </code> |
| | | 177 | | /// </example> |
| | | 178 | | public static BranchTrueOverBlock EmitBranchTrue(this ILGenerator generator, Action<ILGenerator> condition) |
| | | 179 | | { |
| | 1657 | 180 | | condition(generator); |
| | 1657 | 181 | | return new BranchTrueOverBlock(generator); |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | public static BranchFalseOverBlock EmitBranchFalse(this ILGenerator generator, Action<ILGenerator> condition) |
| | | 185 | | { |
| | 227 | 186 | | condition(generator); |
| | 227 | 187 | | return new BranchFalseOverBlock(generator); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Emits the call. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <param name="generator">The generator.</param> |
| | | 194 | | /// <param name="target">The target.</param> |
| | | 195 | | /// <param name="call">The call.</param> |
| | | 196 | | /// <param name="parameters">The parameters.</param> |
| | | 197 | | public static void EmitInvocation( |
| | | 198 | | this ILGenerator generator, |
| | | 199 | | Action<ILGenerator> target, |
| | | 200 | | Action<ILGenerator> call, |
| | | 201 | | params Action<ILGenerator>[] parameters |
| | | 202 | | ) |
| | | 203 | | { |
| | 30 | 204 | | target(generator); |
| | 100 | 205 | | foreach (var tParameter in parameters) |
| | | 206 | | { |
| | 20 | 207 | | tParameter(generator); |
| | | 208 | | } |
| | 30 | 209 | | call(generator); |
| | 30 | 210 | | } |
| | | 211 | | |
| | | 212 | | |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Emits creating the callsite. |
| | | 216 | | /// </summary> |
| | | 217 | | /// <param name="generator">The generator.</param> |
| | | 218 | | /// <param name="funcType">Type of the func.</param> |
| | | 219 | | public static void EmitCallsiteCreate(this ILGenerator generator, Type funcType) |
| | | 220 | | { |
| | 1420 | 221 | | generator.Emit(OpCodes.Call, typeof(CallSite<>).MakeGenericType(funcType).GetMethodEvenIfGeneric("Create", n |
| | 1420 | 222 | | } |
| | | 223 | | |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Emits the call invoke delegate. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="generator">The generator.</param> |
| | | 229 | | /// <param name="funcType">Type of the func.</param> |
| | | 230 | | public static void EmitCallInvokeFunc(this ILGenerator generator, Type funcType) |
| | | 231 | | { |
| | 1420 | 232 | | generator.Emit(OpCodes.Callvirt, funcType.GetMethodEvenIfGeneric("Invoke")); |
| | 1420 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <summary> |
| | | 236 | | /// Emits an array. |
| | | 237 | | /// </summary> |
| | | 238 | | /// <param name="generator">The generator.</param> |
| | | 239 | | /// <param name="arrayType">Type of the array.</param> |
| | | 240 | | /// <param name="emitElements">The emit elements.</param> |
| | | 241 | | public static void EmitArray(this ILGenerator generator, Type arrayType, IList<Action<ILGenerator>> emitElements |
| | | 242 | | { |
| | 908 | 243 | | var tLocal = generator.DeclareLocal(arrayType.MakeArrayType()); |
| | 908 | 244 | | generator.Emit(OpCodes.Ldc_I4, emitElements.Count); |
| | 908 | 245 | | generator.Emit(OpCodes.Newarr, arrayType); |
| | 908 | 246 | | generator.EmitStoreLocation(tLocal.LocalIndex); |
| | | 247 | | |
| | 5334 | 248 | | for (var i = 0; i < emitElements.Count; i++) |
| | | 249 | | { |
| | 1759 | 250 | | generator.EmitLoadLocation(tLocal.LocalIndex); |
| | 1759 | 251 | | generator.Emit(OpCodes.Ldc_I4, i); |
| | 1759 | 252 | | emitElements[i](generator); |
| | 1759 | 253 | | generator.Emit(OpCodes.Stelem_Ref); |
| | | 254 | | } |
| | 908 | 255 | | generator.EmitLoadLocation(tLocal.LocalIndex); |
| | 908 | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Emits the store location. |
| | | 260 | | /// </summary> |
| | | 261 | | /// <param name="generator">The generator.</param> |
| | | 262 | | /// <param name="location">The location.</param> |
| | | 263 | | public static void EmitStoreLocation(this ILGenerator generator, int location) |
| | | 264 | | { |
| | | 265 | | switch (location) |
| | | 266 | | { |
| | | 267 | | case 0: |
| | 719 | 268 | | generator.Emit(OpCodes.Stloc_0); |
| | 719 | 269 | | return; |
| | | 270 | | case 1: |
| | 361 | 271 | | generator.Emit(OpCodes.Stloc_1); |
| | 361 | 272 | | return; |
| | | 273 | | case 2: |
| | 131 | 274 | | generator.Emit(OpCodes.Stloc_2); |
| | 131 | 275 | | return; |
| | | 276 | | case 3: |
| | 10 | 277 | | generator.Emit(OpCodes.Stloc_3); |
| | 10 | 278 | | return; |
| | | 279 | | default: |
| | 0 | 280 | | generator.Emit(OpCodes.Stloc, location); |
| | 0 | 281 | | return; |
| | | 282 | | } |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Emits the load argument. |
| | | 288 | | /// </summary> |
| | | 289 | | /// <param name="generator">The generator.</param> |
| | | 290 | | /// <param name="location">The location.</param> |
| | | 291 | | public static void EmitLoadArgument(this ILGenerator generator, int location) |
| | | 292 | | { |
| | | 293 | | switch (location) |
| | | 294 | | { |
| | | 295 | | case 0: |
| | 0 | 296 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 297 | | return; |
| | | 298 | | case 1: |
| | 525 | 299 | | generator.Emit(OpCodes.Ldarg_1); |
| | 525 | 300 | | return; |
| | | 301 | | case 2: |
| | 132 | 302 | | generator.Emit(OpCodes.Ldarg_2); |
| | 132 | 303 | | return; |
| | | 304 | | case 3: |
| | 45 | 305 | | generator.Emit(OpCodes.Ldarg_3); |
| | 45 | 306 | | return; |
| | | 307 | | default: |
| | 28 | 308 | | generator.Emit(OpCodes.Ldarg, location); |
| | 28 | 309 | | return; |
| | | 310 | | } |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | /// <summary> |
| | | 314 | | /// Emits the load location. |
| | | 315 | | /// </summary> |
| | | 316 | | /// <param name="generator">The generator.</param> |
| | | 317 | | /// <param name="location">The location.</param> |
| | | 318 | | public static void EmitLoadLocation(this ILGenerator generator, int location) |
| | | 319 | | { |
| | | 320 | | switch (location) |
| | | 321 | | { |
| | | 322 | | case 0: |
| | 1868 | 323 | | generator.Emit(OpCodes.Ldloc_0); |
| | 1868 | 324 | | return; |
| | | 325 | | case 1: |
| | 1259 | 326 | | generator.Emit(OpCodes.Ldloc_1); |
| | 1259 | 327 | | return; |
| | | 328 | | case 2: |
| | 504 | 329 | | generator.Emit(OpCodes.Ldloc_2); |
| | 504 | 330 | | return; |
| | | 331 | | case 3: |
| | 30 | 332 | | generator.Emit(OpCodes.Ldloc_3); |
| | 30 | 333 | | return; |
| | | 334 | | default: |
| | 0 | 335 | | generator.Emit(OpCodes.Ldloc, location); |
| | 0 | 336 | | return; |
| | | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | |
| | | 341 | | /// <summary> |
| | | 342 | | /// Emits the dynamic method invoke binder. |
| | | 343 | | /// </summary> |
| | | 344 | | /// <param name="generator">The generator.</param> |
| | | 345 | | /// <param name="flag">The binding flags.</param> |
| | | 346 | | /// <param name="name">The name.</param> |
| | | 347 | | /// <param name="context">The context.</param> |
| | | 348 | | /// <param name="argInfo">The arg info.</param> |
| | | 349 | | /// <param name="argNames">The arg names.</param> |
| | | 350 | | public static void EmitDynamicMethodInvokeBinder(this ILGenerator generator, CSharpBinderFlags flag, string name |
| | | 351 | | { |
| | 597 | 352 | | if (genericParms != null && !genericParms.Any()) |
| | 438 | 353 | | genericParms = null; |
| | | 354 | | |
| | 597 | 355 | | generator.Emit(OpCodes.Ldc_I4, (int)flag); |
| | 597 | 356 | | generator.Emit(OpCodes.Ldstr, name); |
| | 597 | 357 | | if (genericParms == null) |
| | | 358 | | { |
| | 438 | 359 | | generator.Emit(OpCodes.Ldnull); |
| | | 360 | | } |
| | | 361 | | else |
| | | 362 | | { |
| | 159 | 363 | | generator.EmitArray(typeof(Type), genericParms.Select(arg => (Action<ILGenerator>)(gen => gen.EmitTypeOf |
| | | 364 | | } |
| | 597 | 365 | | generator.EmitTypeOf(context); |
| | 597 | 366 | | var tList = new List<Action<ILGenerator>> { gen => gen.EmitCreateCSharpArgumentInfo(CSharpArgumentInfoFlags. |
| | | 367 | | |
| | | 368 | | |
| | | 369 | | |
| | 597 | 370 | | tList.AddRange(argInfo.Zip(argNames,(p,n)=>new{p,n}).Select(arg => (Action<ILGenerator>)(gen => |
| | 597 | 371 | | { |
| | 597 | 372 | | var tStart = CSharpArgumentInfoFlags. |
| | 597 | 373 | | UseCompileTimeType; |
| | 597 | 374 | | |
| | 597 | 375 | | if (arg.p.IsDefined(typeof(DynamicAttrib |
| | 597 | 376 | | { |
| | 597 | 377 | | tStart = CSharpArgumentInfoFlags.Non |
| | 597 | 378 | | } |
| | 597 | 379 | | |
| | 597 | 380 | | if (arg.p.IsOut) |
| | 597 | 381 | | { |
| | 597 | 382 | | tStart |= |
| | 597 | 383 | | CSharpArgumentInfoFlags.IsOut; |
| | 597 | 384 | | } |
| | 597 | 385 | | else if(arg.p.ParameterType.IsByRef) |
| | 597 | 386 | | { |
| | 597 | 387 | | tStart |= |
| | 597 | 388 | | CSharpArgumentInfoFlags.IsRef; |
| | 597 | 389 | | } |
| | 597 | 390 | | |
| | 597 | 391 | | if (!String.IsNullOrEmpty(arg.n)) |
| | 597 | 392 | | { |
| | 597 | 393 | | tStart |= |
| | 597 | 394 | | CSharpArgumentInfoFlags.NamedArgume |
| | 597 | 395 | | } |
| | 597 | 396 | | |
| | 597 | 397 | | gen.EmitCreateCSharpArgumentInfo(tStart, |
| | 597 | 398 | | return; |
| | 597 | 399 | | }))); |
| | 597 | 400 | | generator.EmitArray(typeof(CSharpArgumentInfo), tList); |
| | 597 | 401 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("InvokeMember", new[] { typeof(CSharpBinderFlags), typ |
| | 597 | 402 | | } |
| | | 403 | | |
| | | 404 | | |
| | | 405 | | |
| | | 406 | | /// <summary> |
| | | 407 | | /// Emits the dynamic set binder. |
| | | 408 | | /// </summary> |
| | | 409 | | /// <param name="generator">The generator.</param> |
| | | 410 | | /// <param name="flag">The binding flags.</param> |
| | | 411 | | /// <param name="name">The name.</param> |
| | | 412 | | /// <param name="context">The context.</param> |
| | | 413 | | /// <param name="argTypes">The arg types.</param> |
| | | 414 | | public static void EmitDynamicSetBinder(this ILGenerator generator, CSharpBinderFlags flag, string name, Type co |
| | | 415 | | { |
| | 30 | 416 | | generator.Emit(OpCodes.Ldc_I4, (int)flag); |
| | 30 | 417 | | if (argTypes.Length == 1) |
| | 26 | 418 | | generator.Emit(OpCodes.Ldstr, name); |
| | 30 | 419 | | generator.EmitTypeOf(context); |
| | 30 | 420 | | var tList = new List<Action<ILGenerator>> { gen => gen.EmitCreateCSharpArgumentInfo(CSharpArgumentInfoFlags. |
| | 30 | 421 | | tList.AddRange(argTypes.Select(tArg => (Action<ILGenerator>)(gen => gen.EmitCreateCSharpArgumentInfo(CSharpA |
| | 30 | 422 | | generator.EmitArray(typeof(CSharpArgumentInfo), tList); |
| | | 423 | | |
| | 30 | 424 | | if (argTypes.Length == 1) |
| | 26 | 425 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("SetMember", new[] { typeof(CSharpBinderFlags), ty |
| | | 426 | | else |
| | 4 | 427 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("SetIndex", new[] { typeof(CSharpBinderFlags), typ |
| | | 428 | | |
| | | 429 | | |
| | 4 | 430 | | } |
| | | 431 | | |
| | | 432 | | /// <summary> |
| | | 433 | | /// Emits the dynamic set binder dynamic params. |
| | | 434 | | /// </summary> |
| | | 435 | | /// <param name="generator">The generator.</param> |
| | | 436 | | /// <param name="flag">The flag.</param> |
| | | 437 | | /// <param name="name">The name.</param> |
| | | 438 | | /// <param name="context">The context.</param> |
| | | 439 | | /// <param name="argTypes">The arg types.</param> |
| | | 440 | | public static void EmitDynamicSetBinderDynamicParams(this ILGenerator generator, CSharpBinderFlags flag, string |
| | | 441 | | { |
| | 10 | 442 | | generator.Emit(OpCodes.Ldc_I4, (int)flag); |
| | 10 | 443 | | if (argTypes.Length == 1) |
| | 10 | 444 | | generator.Emit(OpCodes.Ldstr, name); |
| | 10 | 445 | | generator.EmitTypeOf(context); |
| | 10 | 446 | | var tList = new List<Action<ILGenerator>> { gen => gen.EmitCreateCSharpArgumentInfo(CSharpArgumentInfoFlags. |
| | 10 | 447 | | tList.AddRange(argTypes.Select(tArg => (Action<ILGenerator>)(gen => gen.EmitCreateCSharpArgumentInfo(CSharpA |
| | 10 | 448 | | generator.EmitArray(typeof(CSharpArgumentInfo), tList); |
| | | 449 | | |
| | 10 | 450 | | if (argTypes.Length == 1) |
| | 10 | 451 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("SetMember", new[] { typeof(CSharpBinderFlags), ty |
| | | 452 | | else |
| | 0 | 453 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("SetIndex", new[] { typeof(CSharpBinderFlags), typ |
| | | 454 | | |
| | | 455 | | |
| | 0 | 456 | | } |
| | | 457 | | |
| | | 458 | | /// <summary> |
| | | 459 | | /// Emits the dynamic binary op binder. |
| | | 460 | | /// </summary> |
| | | 461 | | /// <param name="generator">The generator.</param> |
| | | 462 | | /// <param name="flag">The flag.</param> |
| | | 463 | | /// <param name="exprType">Type of the expr.</param> |
| | | 464 | | /// <param name="context">The context.</param> |
| | | 465 | | /// <param name="argTypes">The arg types.</param> |
| | | 466 | | public static void EmitDynamicBinaryOpBinder(this ILGenerator generator, CSharpBinderFlags flag, ExpressionType |
| | | 467 | | exprType, Type context, params Type[] argTypes) |
| | | 468 | | { |
| | 10 | 469 | | generator.Emit(OpCodes.Ldc_I4, (int)flag); |
| | 10 | 470 | | generator.Emit(OpCodes.Ldc_I4, (int)exprType); |
| | 10 | 471 | | generator.EmitTypeOf(context); |
| | 10 | 472 | | var tList = new List<Action<ILGenerator>> { gen => gen.EmitCreateCSharpArgumentInfo(CSharpArgumentInfoFlags. |
| | 10 | 473 | | tList.AddRange(argTypes.Select(tArg => (Action<ILGenerator>)(gen => gen.EmitCreateCSharpArgumentInfo(CSharpA |
| | 10 | 474 | | generator.EmitArray(typeof(CSharpArgumentInfo), tList); |
| | | 475 | | |
| | 10 | 476 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("BinaryOperation", new[] { typeof(CSharpBinderFlags), |
| | | 477 | | |
| | 10 | 478 | | } |
| | | 479 | | |
| | | 480 | | /// <summary> |
| | | 481 | | /// Emits the dynamic get binder. |
| | | 482 | | /// </summary> |
| | | 483 | | /// <param name="generator">The generator.</param> |
| | | 484 | | /// <param name="flag">The binding flags.</param> |
| | | 485 | | /// <param name="name">The name.</param> |
| | | 486 | | /// <param name="context">The context.</param> |
| | | 487 | | /// <param name="argTypes">The arg types.</param> |
| | | 488 | | public static void EmitDynamicGetBinder(this ILGenerator generator, CSharpBinderFlags flag, string name, Type co |
| | | 489 | | { |
| | 102 | 490 | | generator.Emit(OpCodes.Ldc_I4, (int)flag); |
| | 102 | 491 | | if (!argTypes.Any()) |
| | 98 | 492 | | generator.Emit(OpCodes.Ldstr, name); |
| | 102 | 493 | | generator.EmitTypeOf(context); |
| | 102 | 494 | | var tList = new List<Action<ILGenerator>> { gen => gen.EmitCreateCSharpArgumentInfo(CSharpArgumentInfoFlags. |
| | 102 | 495 | | tList.AddRange(argTypes.Select(tArg => (Action<ILGenerator>)(gen => gen.EmitCreateCSharpArgumentInfo(CSharpA |
| | 102 | 496 | | generator.EmitArray(typeof(CSharpArgumentInfo), tList); |
| | 102 | 497 | | if (!argTypes.Any()) |
| | 98 | 498 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("GetMember", new[] { typeof(CSharpBinderFlags), ty |
| | | 499 | | else |
| | 4 | 500 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("GetIndex", new[] { typeof(CSharpBinderFlags), typ |
| | 4 | 501 | | } |
| | | 502 | | |
| | | 503 | | |
| | | 504 | | |
| | | 505 | | /// <summary> |
| | | 506 | | /// Emits creating the <see cref="CSharpArgumentInfo"></see> |
| | | 507 | | /// </summary> |
| | | 508 | | /// <param name="generator">The generator.</param> |
| | | 509 | | /// <param name="flag">The flag.</param> |
| | | 510 | | /// <param name="name">The name.</param> |
| | | 511 | | public static void EmitCreateCSharpArgumentInfo(this ILGenerator generator, CSharpArgumentInfoFlags flag, string |
| | | 512 | | { |
| | 1504 | 513 | | generator.Emit(OpCodes.Ldc_I4, (int)flag); |
| | 1504 | 514 | | if (String.IsNullOrEmpty(name)) |
| | 1501 | 515 | | generator.Emit(OpCodes.Ldnull); |
| | | 516 | | else |
| | 3 | 517 | | generator.Emit(OpCodes.Ldstr, name); |
| | 1504 | 518 | | generator.Emit(OpCodes.Call, typeof(CSharpArgumentInfo).GetMethod("Create", new[] { typeof(CSharpArgumentInf |
| | 1504 | 519 | | } |
| | | 520 | | |
| | | 521 | | |
| | | 522 | | /// <summary> |
| | | 523 | | /// Emits the dynamic convert binder. |
| | | 524 | | /// </summary> |
| | | 525 | | /// <param name="generator">The generator.</param> |
| | | 526 | | /// <param name="flag">The binding flag.</param> |
| | | 527 | | /// <param name="returnType">Type of the return.</param> |
| | | 528 | | /// <param name="context">The context.</param> |
| | | 529 | | public static void EmitDynamicConvertBinder(this ILGenerator generator, CSharpBinderFlags flag, Type returnType, |
| | | 530 | | { |
| | 661 | 531 | | generator.Emit(OpCodes.Ldc_I4, (int)flag); |
| | 661 | 532 | | generator.EmitTypeOf(returnType); |
| | 661 | 533 | | generator.EmitTypeOf(context); |
| | 661 | 534 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("Convert", new[] { typeof(CSharpBinderFlags), typeof(T |
| | 661 | 535 | | } |
| | | 536 | | |
| | | 537 | | |
| | | 538 | | public static void EmitDynamicIsEventBinder(this ILGenerator generator, CSharpBinderFlags flag, string name, Typ |
| | | 539 | | { |
| | 10 | 540 | | generator.Emit(OpCodes.Ldc_I4, (int)flag); |
| | 10 | 541 | | generator.Emit(OpCodes.Ldstr, name); |
| | 10 | 542 | | generator.EmitTypeOf(context); |
| | 10 | 543 | | generator.Emit(OpCodes.Call, typeof(Binder).GetMethod("IsEvent", new[] { typeof(CSharpBinderFlags), typeof(s |
| | 10 | 544 | | } |
| | | 545 | | |
| | | 546 | | |
| | | 547 | | /// <summary> |
| | | 548 | | /// Emits the typeof(Type) |
| | | 549 | | /// </summary> |
| | | 550 | | /// <param name="generator">The generator.</param> |
| | | 551 | | /// <param name="type">The type.</param> |
| | | 552 | | public static void EmitTypeOf(this ILGenerator generator, Type type) |
| | | 553 | | { |
| | | 554 | | |
| | 2336 | 555 | | generator.Emit(OpCodes.Ldtoken, type); |
| | 2336 | 556 | | var tTypeMeth = typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) }); |
| | 2336 | 557 | | generator.Emit(OpCodes.Call, tTypeMeth); |
| | 2336 | 558 | | } |
| | | 559 | | |
| | | 560 | | |
| | | 561 | | |
| | | 562 | | } |
| | | 563 | | } |