public static void EmitNew(ILGenerator il, ConstructorInfo ci) { Require.NotNull(ci, "ci"); Require.That(!ci.DeclaringType.ContainsGenericParameters, "Illegal new generic params"); il.Emit(OpCodes.Newobj, ci); }
/// <summary> /// Emits an array of values of count size. The items are emitted via the callback /// which is provided with the current item index to emit. /// </summary> public static void EmitArray(ILGenerator il, Type elementType, int count, Action <int> emit) { Require.NotNull(elementType, "elementType"); Require.NotNull(emit, "emit"); Require.That(count >= 0, "Cannot be negative", "count"); EmitInt(il, count); il.Emit(OpCodes.Newarr, elementType); for (int i = 0; i < count; i++) { il.Emit(OpCodes.Dup); EmitInt(il, i); emit(i); EmitStoreElement(il, elementType); } }
/// <summary> /// Emits an array construction code. /// The code assumes that bounds for all dimensions /// are already emitted. /// </summary> private static void EmitArray(ILGenerator il, Type arrayType) { Require.NotNull(arrayType, "arrayType"); Require.That(arrayType.IsArray, "Must be array", "arrayType"); int rank = arrayType.GetArrayRank(); if (rank == 1) { il.Emit(OpCodes.Newarr, arrayType.GetElementType()); } else { Type[] types = new Type[rank]; for (int i = 0; i < rank; i++) { types[i] = typeof(int); } EmitNew(il, arrayType, types); } }
/// <summary> /// Initializes a new instance of the <see cref="Import"/> class with /// the specified namespace and nested imports. /// </summary> /// <param name="ns">The namespace the type must be imported at.</param> /// <param name="imports">The imports that are nested under this import.</param> public Import(string ns, params Import[] imports) : this(ns, null, imports) { Require.NotNull(imports, "imports"); Require.That(imports.Length > 0, "At least one import is required in a namespace", "imports"); }