reflection.emit

Exception while dynamically creating assemblies using DefineDynamicAssembly on non-current AppDomain

拟墨画扇 提交于 2019-12-23 10:18:43
问题 I want to dynamically create assemblies in the integration tests for the purpose of testing some assembly manipulation classes. If I use the following code to create the testing assemblies: var domain = AppDomain.CurrentDomain; var builder = domain.DefineDynamicAssembly( new AssemblyName(assemblyName), AssemblyBuilderAccess.Save, directory); builder.Save(fileName); then everything runs ok, the assemblies are created in the required location, but as part of this they are also loaded to the

Reflection.Emit.ILGenerator Exception Handling “Leave” instruction

穿精又带淫゛_ 提交于 2019-12-23 07:48:33
问题 First, some background info: I am making a compiler for a school project. It is already working, and I'm expending a lot of effort to bug fix and/or optimize it. I've recently run into a problem with is that I discovered that the ILGenerator object generates an extra leave instruction when you call any of the following member methods: BeginCatchBlock() BeginExceptFilterBlock() BeginFaultBlock() BeginFinallyBlock() EndExceptionBlock() So, you start a try statement with a call to

Attaching an existing method to a dynamic assembly instead of generating IL for it

微笑、不失礼 提交于 2019-12-23 06:20:09
问题 I am new to the Reflection.Emit API and have generated a bare minimum assembly with an entry point that returns an exit code based on a few simple I/O checks. The reason for creating an external assembly is that a piece of code needs to run outside the current process space and will be transient itself. It will eventually be deleted by the creating app. Having said that, the code to be generated is quite complex and I would rather avoid creating a separate project for a transient assembly. I

Is there any way to instrument System.Reflection.Emit?

旧时模样 提交于 2019-12-23 00:41:07
问题 I'm having some serious trouble with a compiler that does its code generation with the classes found in System.Reflection.Emit . It takes code that should be perfectly valid, and that passes all the compiler's internal checks, and tries to codegen it, but TypeBuilder.CreateType() throws the following completely unhelpful exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) The problem lies somewhere in

C# Reflection: Emitting classes to existing assemblies

偶尔善良 提交于 2019-12-22 09:53:55
问题 Is it possible to use Reflection.Emit to create types in an existing assembly, or do you need to define a new dynamic assembly to be able to contain dynamic types? Basically, I intend to read in one XML definition file which defines a class that is then instantiated multiple times and populated with the data from several other XML files. Rinse and repeat over several folders (each with a different definition file). I also intend to use this data to dynamically build the interface to my app as

Can Roslyn be used to generate dynamic method similar to DynamicMethod IL generation

断了今生、忘了曾经 提交于 2019-12-21 12:30:57
问题 I have been using DynamiMethod to generate the IL using method.GetILGenerator(); This works well but is of course very hard to use since you generally don't want to work with low level IL in a high level language like C#. Now since there is Roslyn I though I can use that instead. I have tried to figure out how to use Roslyn to do similar thing: generate a dynamic method and then create a delegate for it. The only way I was able to do that is to have full class like this SyntaxTree syntaxTree

Calling varargs method via DynamicMethod

一曲冷凌霜 提交于 2019-12-21 12:21:46
问题 I'm trying to call unmanaged printf-like function using DynamicMethod. At runtime I get a BadImageFormatException:Index not found. (Exception from HRESULT: 0x80131124) Is this a limitation of runtime or my emited code is wrong? public class Program { [DllImport("msvcrt40.dll",CallingConvention = CallingConvention.Cdecl)] public static extern int printf(string format, __arglist); static void Main(string[] args) { var method = new DynamicMethod("printf", typeof(void), new Type[0], true); var il

Creating dynamic type from TypeBuilder with a base class and additional fields generates an exception

匆匆过客 提交于 2019-12-21 10:43:39
问题 I am trying to create a dynamic type based on an existing type that contains only public fields. The new dynamic type must also inherit from a different base type which only has a fully implemented method. I create the TypeBuilder specifying the base type then I add the public fields to it and finally I call CreateType() . The resulting error message is: "Could not load type 'InternalType' from assembly 'MyDynamicAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because field

Creating WCF Service at runtime

*爱你&永不变心* 提交于 2019-12-21 06:07:27
问题 We are going to build a web service from metadata read at runtime. I mean the entire web service: the signatures, contracts and implementation. There are two main paths I see from here. The first path is that you generate code. Either you generate C# code in strings and compile it on the fly or more elegantly (and complicatedly), you emit MSIL code. This way you have WCF code and WCF will take care of generating the WSDL from it. The second path is to use a generic service. A service with an

Creating WCF Service at runtime

谁说我不能喝 提交于 2019-12-21 06:07:17
问题 We are going to build a web service from metadata read at runtime. I mean the entire web service: the signatures, contracts and implementation. There are two main paths I see from here. The first path is that you generate code. Either you generate C# code in strings and compile it on the fly or more elegantly (and complicatedly), you emit MSIL code. This way you have WCF code and WCF will take care of generating the WSDL from it. The second path is to use a generic service. A service with an