reflection.emit

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

邮差的信 提交于 2019-12-07 23:46:26
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 the way it's transforming generators into state machines in a specific edge case, but if I take the

I'm attempting to write a .NET compiler using System.Reflection.Emit how do I do type resolution?

社会主义新天地 提交于 2019-12-07 15:53:55
问题 I've got a strategy for resolving types from referenced dlls. I'm stuck on trying to resolve types that are defined in the assembly that is being compiled. I'm using the System.Reflection.Emit apis with no 3rd party libraries. For instance: class A {} class B { public A AnInstanceOfA {get; private set;} } What's the best way to resolve B's reference of A? What about this: class A { B AnInstanceOfB {get; set;} } class B { A AnInstanceOfA {get; set;} } where the classes contain instances of

Get property value from another object using Reflection.Emit

点点圈 提交于 2019-12-07 06:04:41
问题 This is my first foray into using Reflection.Emit. I'm dynamically building a proxy for a provided object. The proxy passes any public property accesses through to the provided object. The error I'm receiving is: Property accessor 'AccessorName' on object 'ProxyObject' threw the following exception: Attempt by method 'ProxyObject.get_AccessorName()' to access method 'NS.CoreObject.get_AccessorName() failed. From what I can assume and gather, this would be due to the automatically-generated

Generating a constructor that takes an argument and just calls base(argument) using TypeBuilder

穿精又带淫゛_ 提交于 2019-12-07 05:19:19
问题 I am trying to dynamically create an empty constructor that takes an argument and simply calls base(argument) using TypeBuilder. My code is something like: (...) // Create a class derived from this class TypeBuilder typeBuilder = moduleBuilder.DefineType("NewClass", TypeAttributes.Class, this.GetType()); ConstructorInfo baseCtor = this.GetType().GetConstructor(new[] { typeof(int) }); // Define new ctor taking int ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor

How can I emit a dynamic method returning a ref?

筅森魡賤 提交于 2019-12-07 03:10:00
问题 I'm navigating the ins and outs of ref returns, and am having trouble emitting a dynamic method which returns by ref. Handcrafted lambdas and existing methods work as expected: class Widget { public int Length; } delegate ref int WidgetMeasurer(Widget w); WidgetMeasurer GetMeasurerA() { return w => ref w.Length; } static ref int MeasureWidget(Widget w) => ref w.Length; WidgetMeasurer GetMeasurerB() { return MeasureWidget; } But emitting a dynamic method fails. Note : I'm using Sigil here.

Expression.DebugInfo How Do I Tag Expressions?

夙愿已清 提交于 2019-12-07 00:39:37
问题 So I know what Expression.DebugInfo is used for, and I have an Debug expression created, but how to I tag other expressions with this debug info? Here's what I'm trying as a really basic test: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Reflection; namespace ExpressionDebugTest { class Program { static void Main(string[] args) { var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("foo"),

Signature of the body and declaration in a method implementation do not match

安稳与你 提交于 2019-12-06 06:33:10
问题 I want to implement an interface at runtime, however when i execute return Activator.CreateInstance(typeBuilder.CreateTypeInfo().AsType(), new RPCRequestProxy()); in RPCClientFactory.cs it throws System.TypeLoadException: Signature of the body and declaration in a method implementation do not match . I use dotnet core 1.0.4. The entry point Program.cs : namespace RPC { interface IRCPClient { Task Foo([UrlParam]int b, [UrlParam]string a, [UrlParam] DateTime c); } class Program { static void

Explicit interface implementation and Reflection.Emit

£可爱£侵袭症+ 提交于 2019-12-06 02:24:15
问题 Does anybody know how to implement an interface's property explicitly using Reflection.Emit? 回答1: See the MSDN documentation for TypeBuilder.DefineMethodOverride, which includes an example of using Reflection.Emit to generate an explicit interface implementation using that method. 回答2: This Reflector Addin should help you. It translates the IL code of a given method into the C# code that would be needed to generate the same IL code using System.Reflection.Emit. 来源: https://stackoverflow.com

C# Reflection: Emitting classes to existing assemblies

[亡魂溺海] 提交于 2019-12-05 19:09:16
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 well as define how the data is formatted when re-saving the data to the XML files. When using

Is it possible to emit a type deriving from a generic type while specifying itself as the generic type parameter?

跟風遠走 提交于 2019-12-05 17:16:29
Imagine the following perfectly legal type hierarchy: class A<T> where T : A<T> { } class B : A<B> { public B():base(){} } My question is given a statically compiled definition of A<> is it possible to emit the type B dynamically? The problem is how to specify the parent type in ModuleBuilder.DefineType . Or maybe there is another way to produce such a type, other than using the aforementioned method using CodeDom (which is much like creating a temporary file and passing it to csc.exe :-)) EDIT: The type B should have explicit public default constructor invoking the default constructor