public ILGenerator GetILGenerator(int streamSize) { if (this.m_ilGenerator == null) { byte[] signature = SignatureHelper.GetMethodSigHelper(null, this.CallingConvention, this.ReturnType, null, null, this.m_parameterTypes, null, null).GetSignature(true); this.m_ilGenerator = new DynamicILGenerator(this, signature, streamSize); } return(this.m_ilGenerator); }
internal DynamicResolver(DynamicILGenerator ilGenerator) { this.m_stackSize = ilGenerator.GetMaxStackSize(); this.m_exceptions = ilGenerator.GetExceptions(); this.m_code = ilGenerator.BakeByteArray(); this.m_localSignature = ilGenerator.m_localSignature.InternalGetSignatureArray(); this.m_scope = ilGenerator.m_scope; this.m_method = (DynamicMethod) ilGenerator.m_methodBuilder; this.m_method.m_resolver = this; }
internal DynamicResolver(DynamicILGenerator ilGenerator) { this.m_stackSize = ilGenerator.GetMaxStackSize(); this.m_exceptions = ilGenerator.GetExceptions(); this.m_code = ilGenerator.BakeByteArray(); this.m_localSignature = ilGenerator.m_localSignature.InternalGetSignatureArray(); this.m_scope = ilGenerator.m_scope; this.m_method = (DynamicMethod)ilGenerator.m_methodBuilder; this.m_method.m_resolver = this; }
[System.Security.SecuritySafeCritical] // auto-generated public ILGenerator GetILGenerator(int streamSize) { if (m_ilGenerator == null) { byte[] methodSignature = SignatureHelper.GetMethodSigHelper( null, CallingConvention, ReturnType, null, null, m_parameterTypes, null, null).GetSignature(true); m_ilGenerator = new DynamicILGenerator(this, methodSignature, streamSize); } return m_ilGenerator; }
[System.Security.SecurityCritical] // auto-generated private unsafe void Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, bool skipVisibility, bool transparentMethod, ref StackCrawlMark stackMark) { DynamicMethod.CheckConsistency(attributes, callingConvention); // check and store the signature if (signature != null) { m_parameterTypes = new RuntimeType[signature.Length]; for (int i = 0; i < signature.Length; i++) { if (signature[i] == null) throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType; if ( m_parameterTypes[i] == null || !(m_parameterTypes[i] is RuntimeType) || m_parameterTypes[i] == (RuntimeType)typeof(void) ) throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } } else { m_parameterTypes = Array.Empty<RuntimeType>(); } // check and store the return value m_returnType = (returnType == null) ? (RuntimeType)typeof(void) : returnType.UnderlyingSystemType as RuntimeType; if ( (m_returnType == null) || !(m_returnType is RuntimeType) || m_returnType.IsByRef ) throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType")); if (transparentMethod) { Contract.Assert(owner == null && m == null, "owner and m cannot be set for transparent methods"); m_module = GetDynamicMethodsModule(); if (skipVisibility) { m_restrictedSkipVisibility = true; } #if FEATURE_COMPRESSEDSTACK m_creationContext = CompressedStack.Capture(); #endif // FEATURE_COMPRESSEDSTACK } else { Contract.Assert(m != null || owner != null, "PerformSecurityCheck should ensure that either m or owner is set"); Contract.Assert(m == null || !m.Equals(s_anonymouslyHostedDynamicMethodsModule), "The user cannot explicitly use this assembly"); Contract.Assert(m == null || owner == null, "m and owner cannot both be set"); if (m != null) m_module = m.ModuleHandle.GetRuntimeModule(); // this returns the underlying module for all RuntimeModule and ModuleBuilder objects. else { RuntimeType rtOwner = null; if (owner != null) rtOwner = owner.UnderlyingSystemType as RuntimeType; if (rtOwner != null) { if (rtOwner.HasElementType || rtOwner.ContainsGenericParameters || rtOwner.IsGenericParameter || rtOwner.IsInterface) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod")); m_typeOwner = rtOwner; m_module = rtOwner.GetRuntimeModule(); } } m_skipVisibility = skipVisibility; } // initialize remaining fields m_ilGenerator = null; m_fInitLocals = true; m_methodHandle = null; if (name == null) throw new ArgumentNullException("name"); #if FEATURE_APPX if (AppDomain.ProfileAPICheck) { if (m_creatorAssembly == null) m_creatorAssembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark); if (m_creatorAssembly != null && !m_creatorAssembly.IsFrameworkAssembly()) m_profileAPICheck = true; } #endif // FEATURE_APPX m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention); }
[System.Security.SecurityCritical] // auto-generated private unsafe void Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, bool skipVisibility, bool transparentMethod, ref StackCrawlMark stackMark) { DynamicMethod.CheckConsistency(attributes, callingConvention); // check and store the signature if (signature != null) { m_parameterTypes = new RuntimeType[signature.Length]; for (int i = 0; i < signature.Length; i++) { if (signature[i] == null) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType; if (m_parameterTypes[i] == null || !(m_parameterTypes[i] is RuntimeType) || m_parameterTypes[i] == (RuntimeType)typeof(void)) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } } } else { m_parameterTypes = Array.Empty <RuntimeType>(); } // check and store the return value m_returnType = (returnType == null) ? (RuntimeType)typeof(void) : returnType.UnderlyingSystemType as RuntimeType; if ((m_returnType == null) || !(m_returnType is RuntimeType) || m_returnType.IsByRef) { throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType")); } if (transparentMethod) { Contract.Assert(owner == null && m == null, "owner and m cannot be set for transparent methods"); m_module = GetDynamicMethodsModule(); if (skipVisibility) { m_restrictedSkipVisibility = true; } #if FEATURE_COMPRESSEDSTACK m_creationContext = CompressedStack.Capture(); #endif // FEATURE_COMPRESSEDSTACK } else { Contract.Assert(m != null || owner != null, "PerformSecurityCheck should ensure that either m or owner is set"); Contract.Assert(m == null || !m.Equals(s_anonymouslyHostedDynamicMethodsModule), "The user cannot explicitly use this assembly"); Contract.Assert(m == null || owner == null, "m and owner cannot both be set"); if (m != null) { m_module = m.ModuleHandle.GetRuntimeModule(); // this returns the underlying module for all RuntimeModule and ModuleBuilder objects. } else { RuntimeType rtOwner = null; if (owner != null) { rtOwner = owner.UnderlyingSystemType as RuntimeType; } if (rtOwner != null) { if (rtOwner.HasElementType || rtOwner.ContainsGenericParameters || rtOwner.IsGenericParameter || rtOwner.IsInterface) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod")); } m_typeOwner = rtOwner; m_module = rtOwner.GetRuntimeModule(); } } m_skipVisibility = skipVisibility; } // initialize remaining fields m_ilGenerator = null; m_fInitLocals = true; m_methodHandle = null; if (name == null) { throw new ArgumentNullException("name"); } #if FEATURE_APPX if (AppDomain.ProfileAPICheck) { if (m_creatorAssembly == null) { m_creatorAssembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark); } if (m_creatorAssembly != null && !m_creatorAssembly.IsFrameworkAssembly()) { m_profileAPICheck = true; } } #endif // FEATURE_APPX m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention); }
private void Init(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, System.Reflection.Module m, bool skipVisibility, bool transparentMethod) { CheckConsistency(attributes, callingConvention); if (signature != null) { this.m_parameterTypes = new RuntimeType[signature.Length]; for (int i = 0; i < signature.Length; i++) { if (signature[i] == null) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } this.m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType; if (((this.m_parameterTypes[i] == null) || !this.m_parameterTypes[i].IsRuntimeType) || (this.m_parameterTypes[i] == typeof(void))) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } } } else { this.m_parameterTypes = new RuntimeType[0]; } this.m_returnType = (returnType == null) ? ((RuntimeType) typeof(void)) : (returnType.UnderlyingSystemType as RuntimeType); if (((this.m_returnType == null) || !this.m_returnType.IsRuntimeType) || this.m_returnType.IsByRef) { throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType")); } if (transparentMethod) { this.m_module = GetDynamicMethodsModule(); if (skipVisibility) { this.m_restrictedSkipVisibility = true; this.m_creationContext = CompressedStack.Capture(); } } else { if (m != null) { this.m_module = m.ModuleHandle.GetRuntimeModule(); } else if (((owner != null) && (owner.UnderlyingSystemType != null)) && owner.UnderlyingSystemType.IsRuntimeType) { this.m_typeOwner = owner.UnderlyingSystemType.TypeHandle.GetRuntimeType(); if ((this.m_typeOwner.HasElementType || this.m_typeOwner.ContainsGenericParameters) || (this.m_typeOwner.IsGenericParameter || this.m_typeOwner.IsInterface)) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod")); } this.m_module = (RuntimeModule) this.m_typeOwner.Module; } this.m_skipVisibility = skipVisibility; } this.m_ilGenerator = null; this.m_fInitLocals = true; this.m_methodHandle = null; if (name == null) { throw new ArgumentNullException("name"); } this.m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention); }
private unsafe void Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, bool skipVisibility, bool transparentMethod) { DynamicMethod.CheckConsistency(attributes, callingConvention); // check and store the signature if (signature != null) { m_parameterTypes = new RuntimeType[signature.Length]; for (int i = 0; i < signature.Length; i++) { if (signature[i] == null) { throw new ArgumentException(SR.Arg_InvalidTypeInSignature); } m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType; if (m_parameterTypes[i] == null || m_parameterTypes[i] == (RuntimeType)typeof(void)) { throw new ArgumentException(SR.Arg_InvalidTypeInSignature); } } } else { m_parameterTypes = Array.Empty <RuntimeType>(); } // check and store the return value m_returnType = (returnType == null) ? (RuntimeType)typeof(void) : returnType.UnderlyingSystemType as RuntimeType; if (m_returnType == null) { throw new NotSupportedException(SR.Arg_InvalidTypeInRetType); } if (transparentMethod) { Debug.Assert(owner == null && m == null, "owner and m cannot be set for transparent methods"); m_module = GetDynamicMethodsModule(); if (skipVisibility) { m_restrictedSkipVisibility = true; } } else { Debug.Assert(m != null || owner != null, "Constructor should ensure that either m or owner is set"); Debug.Assert(m == null || !m.Equals(s_anonymouslyHostedDynamicMethodsModule), "The user cannot explicitly use this assembly"); Debug.Assert(m == null || owner == null, "m and owner cannot both be set"); if (m != null) { m_module = m.ModuleHandle.GetRuntimeModule(); // this returns the underlying module for all RuntimeModule and ModuleBuilder objects. } else { RuntimeType rtOwner = null; if (owner != null) { rtOwner = owner.UnderlyingSystemType as RuntimeType; } if (rtOwner != null) { if (rtOwner.HasElementType || rtOwner.ContainsGenericParameters || rtOwner.IsGenericParameter || rtOwner.IsInterface) { throw new ArgumentException(SR.Argument_InvalidTypeForDynamicMethod); } m_typeOwner = rtOwner; m_module = rtOwner.GetRuntimeModule(); } } m_skipVisibility = skipVisibility; } // initialize remaining fields m_ilGenerator = null; m_fInitLocals = true; m_methodHandle = null; if (name == null) { throw new ArgumentNullException(nameof(name)); } m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention); }
private void GenerateMethod(TypeBuilder type, MethodMetadata methodMetadata) { if(type == null) throw new ArgumentNullException("type"); if(methodMetadata == null) throw new ArgumentNullException("methodMetadata"); var attrs = MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig; var callingConvenction = CallingConventions.HasThis; var parameters = methodMetadata.Parameters.Select(x => x.Type).ToArray(); var method = type.DefineMethod(methodMetadata.Name, attrs, callingConvenction, methodMetadata.ReturnType, parameters); var il = method.GetILGenerator(); var g = new DynamicILGenerator(il); foreach(var parameter in methodMetadata.Parameters) method.DefineParameter(parameter.Position, ParameterAttributes.None, parameter.Name); foreach(var local in methodMetadata.Block.Locals) g.DeclareLocal(local); foreach(var line in methodMetadata.Block.Lines) this.GenerateLine(g, line); }
private void GenerateLine(DynamicILGenerator g, LineMetadata line) { this.lineGenerator.Value.Emit(g, line); }
private void Init(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, System.Reflection.Module m, bool skipVisibility, bool transparentMethod) { CheckConsistency(attributes, callingConvention); if (signature != null) { this.m_parameterTypes = new RuntimeType[signature.Length]; for (int i = 0; i < signature.Length; i++) { if (signature[i] == null) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } this.m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType; if (((this.m_parameterTypes[i] == null) || !this.m_parameterTypes[i].IsRuntimeType) || (this.m_parameterTypes[i] == typeof(void))) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } } } else { this.m_parameterTypes = new RuntimeType[0]; } this.m_returnType = (returnType == null) ? ((RuntimeType)typeof(void)) : (returnType.UnderlyingSystemType as RuntimeType); if (((this.m_returnType == null) || !this.m_returnType.IsRuntimeType) || this.m_returnType.IsByRef) { throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType")); } if (transparentMethod) { this.m_module = GetDynamicMethodsModule(); if (skipVisibility) { this.m_restrictedSkipVisibility = true; this.m_creationContext = CompressedStack.Capture(); } } else { if (m != null) { this.m_module = m.ModuleHandle.GetRuntimeModule(); } else if (((owner != null) && (owner.UnderlyingSystemType != null)) && owner.UnderlyingSystemType.IsRuntimeType) { this.m_typeOwner = owner.UnderlyingSystemType.TypeHandle.GetRuntimeType(); if ((this.m_typeOwner.HasElementType || this.m_typeOwner.ContainsGenericParameters) || (this.m_typeOwner.IsGenericParameter || this.m_typeOwner.IsInterface)) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod")); } this.m_module = (RuntimeModule)this.m_typeOwner.Module; } this.m_skipVisibility = skipVisibility; } this.m_ilGenerator = null; this.m_fInitLocals = true; this.m_methodHandle = null; if (name == null) { throw new ArgumentNullException("name"); } this.m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention); }
private unsafe void Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, bool skipVisibility) { DynamicMethod.CheckConsistency(attributes, callingConvention); // check and store the signature if (signature != null) { m_parameterTypes = new RuntimeType[signature.Length]; for (int i = 0; i < signature.Length; i++) { if (signature[i] == null) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType; if (m_parameterTypes[i] == null || m_parameterTypes[i] == typeof(void)) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } } } else { m_parameterTypes = new RuntimeType[0]; } // check and store the return value m_returnType = (returnType == null) ? ((RuntimeType)typeof(void)) : (returnType.UnderlyingSystemType as RuntimeType); if (m_returnType == null || m_returnType.IsByRef) { throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType")); } m_typeOwner = (owner != null) ? owner.UnderlyingSystemType as RuntimeType : null; if (m_typeOwner != null) { if (m_typeOwner.HasElementType || m_typeOwner.ContainsGenericParameters || m_typeOwner.IsGenericParameter || m_typeOwner.IsInterface) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod")); } } m_module = (m != null) ? m.ModuleHandle : m_typeOwner.Module.ModuleHandle; // initialize remaining fields m_ilGenerator = null; m_fInitLocals = true; m_method = new RuntimeMethodHandle(null); m_skipVisibility = skipVisibility; if (name == null) { throw new ArgumentNullException("name"); } m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention); }
private unsafe void Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, bool skipVisibility) { DynamicMethod.CheckConsistency(attributes, callingConvention); // check and store the signature if (signature != null) { m_parameterTypes = new RuntimeType[signature.Length]; for (int i = 0; i < signature.Length; i++) { if (signature[i] == null) throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType; if (m_parameterTypes[i] == null || m_parameterTypes[i] == typeof(void)) throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } } else m_parameterTypes = new RuntimeType[0]; // check and store the return value m_returnType = (returnType == null) ? ((RuntimeType)typeof(void)) : (returnType.UnderlyingSystemType as RuntimeType); if (m_returnType == null || m_returnType.IsByRef) throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType")); m_typeOwner = (owner != null) ? owner.UnderlyingSystemType as RuntimeType : null; if (m_typeOwner != null) if (m_typeOwner.HasElementType || m_typeOwner.ContainsGenericParameters || m_typeOwner.IsGenericParameter || m_typeOwner.IsInterface) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod")); m_module = (m != null) ? m.ModuleHandle : m_typeOwner.Module.ModuleHandle; // initialize remaining fields m_ilGenerator = null; m_fInitLocals = true; m_method = new RuntimeMethodHandle(null); m_skipVisibility = skipVisibility; if (name == null) throw new ArgumentNullException("name"); m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention); }