reflection.emit

Emit local variable and assign a value to it

僤鯓⒐⒋嵵緔 提交于 2019-12-05 16:56:50
问题 I'm initializing an integer variable like this: LocalBuilder a = ilGen.DeclareLocal(typeof(Int32)); How can I access it and assign a value to it? I want to do something like this: int a, b; a = 5; b = 6; return a + b; 回答1: Use the Ldloc and Stloc opcodes to read and write local variables: LocalBuilder a = ilGen.DeclareLocal(typeof(Int32)); LocalBuilder b = ilGen.DeclareLocal(typeof(Int32)); ilGen.Emit(OpCodes.Ldc_I4, 5); // Store "5" ... ilGen.Emit(OpCodes.Stloc, a); // ... in "a". ilGen.Emit

MethodBuilder.CreateMethodBody() problem in Dynamic Type creation

删除回忆录丶 提交于 2019-12-05 13:57:01
For an experiment, i am trying to read the method body (using GetILAsByteArray() ) from source type and adding it to the new Type (Using CreateMethodBody() ). My source class is simply this public class FullClass { public string Test(string data) { return data; } public string Test2(string data) { return data; } public string Test5(string data, string data1) { return data + data1; } } The IL generated for this code (taken using reflector) .method public hidebysig instance string Test(string data) cil managed { .maxstack 1 .locals init ( [0] string CS$1$0000) L_0000: nop L_0001: ldarg.1 L_0002:

Get property value from another object using Reflection.Emit

若如初见. 提交于 2019-12-05 10:27:45
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 property getter method being private and hidden. But how do I work around this using a MethodBuilder ?

How can I emit a dynamic method returning a ref?

冷暖自知 提交于 2019-12-05 06:58:50
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. Apologies, I'm less familiar with System.Reflection.Emit . WidgetMeasurer GetMeasurerC() { FieldInfo

Expression.DebugInfo How Do I Tag Expressions?

拈花ヽ惹草 提交于 2019-12-05 04:59:22
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"), System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave); var mod = asm.DefineDynamicModule("mymod",

NHibernate / Fluent NHibernate Dynamic Column Mapping

≯℡__Kan透↙ 提交于 2019-12-05 02:40:56
问题 I have a table that, some of its columns are unknown at compile time. Such columns could either be of an integer value, or some Enum value. There is a table that holds all the names of such dynamic columns and also holds the column's type. This "metatable" has the following columns: DynamicColumnId (Pk) Name TypeId (Integer / Enum, as Fk from a separate table) Integer columns have the Name from this table, whereas Enum columns are Fk columns from a table that has that Name , with some

Is it possible to invoke internal method from a dynamic method in .NET?

只谈情不闲聊 提交于 2019-12-05 02:16:46
I am trying to invoke an internal method from a dynamically generated one. The il code is simple: ldarg_0, callvirt, ret. Executing the method fails with TypeLoadException saying it cannot load the type on which the internal method is defined. When I think of it, this seems logical, because the dynamic method host assembly is not a friend of the method's declaring type assembly. However, I have expected the dynamic method still to work, just like Delegate.CreateDelegate works. After all, I did manage to get the MethodInfo of the internal method, so the permissions barrier are behind me. Anyway

transferring one object properties values to another one

时光毁灭记忆、已成空白 提交于 2019-12-05 02:04:05
问题 Before all, I know about AutoMapper , and I don't want to use it. Because I'm learning C# and I want to receive a deep view of it. So I'm trying to do this issue (explained below) myself. However, I'm trying to create a property copier to cope values of one type's properties to another one, if the property has the same name and type and is readable from source and writable in target. I'm using type.GetProperties() method. Sampled method is here: static void Transfer(object source, object

Linking a .NET Expression Tree into a new assembly

允我心安 提交于 2019-12-05 01:28:09
问题 I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies. The problem is, I can't figure out how to actually build these assemblies. The MethodBuilder class only accepts raw MSIL method bodies, so I have to get the

Creating a class for an interface at runtime, in C#

情到浓时终转凉″ 提交于 2019-12-04 18:32:10
问题 I'm looking at taking a set of objects, let's say there's 3 objects alive at the moment, which all implement a common interface, and then wrap those objects inside a fourth object, also implementing the same interface. The fourth object's implementations of methods and properties would simply call the relevant bits on those 3 underlying objects. I know that there will be cases here where it won't make sense to do that, but this is for a service multicast architecture so there's already a good