reflection.emit

What should I pin when working on arrays?

送分小仙女□ 提交于 2019-12-01 22:29:58
I'm trying to write a DynamicMethod to wrap the cpblk IL opcode . I need to copy chunks of byte arrays and on x64 platforms, this is supposedly the fastest way to do it. Array.Copy and Buffer.BlockCopy both work, but I'd like to explore all options. My goal is to copy managed memory from one byte array to a new managed byte array. My concern is how do I know how to correctly "pin" memory location. I don't want the garbage collector to move the arrays and break everything. SO far it works but I'm not sure how to test if this is GC safe. // copying 'count' bytes from offset 'index' in 'source'

emit class with a property of the same type as declaring emitted type

∥☆過路亽.° 提交于 2019-12-01 21:41:06
I have found how to emit a class, like this: class MyClass { String MyProperty { get; set; } } Cannot figure out how to emit the following: class MyClass { MyClass MyProperty { get; set; } } The problem I faced is: In order to emit the setter and getter, I need a variable representing the final compiled/emitted Type. In order to emit this Type, I need to emit setter/getter first. I guess it is possible, because the language allows that in static compilation. In order to emit the setter and getter, I need a variable representing the final compiled/emitted Type Actually, you don't: you just need

Reference 'this' in dynamic event handler

北慕城南 提交于 2019-12-01 14:36:37
In my 'myClass' class, I am using Reflection.Emit to dynamically write an event handler for one of the myClass class' members. I have done this successfully. Now, I want to modify the event handler to call one of the instance methods in the myClass class. However, I cannot figure out how to push a reference to 'this' onto the MSIL stack using Reflection.Emit. Within the event handler, Ldarg_0 is not a reference to 'this', but rather the first parameter of the event handler. Does anyone know how to push a reference to 'this' on the stack so that I can call an instance method. For example, this

Reference 'this' in dynamic event handler

天涯浪子 提交于 2019-12-01 12:28:19
问题 In my 'myClass' class, I am using Reflection.Emit to dynamically write an event handler for one of the myClass class' members. I have done this successfully. Now, I want to modify the event handler to call one of the instance methods in the myClass class. However, I cannot figure out how to push a reference to 'this' onto the MSIL stack using Reflection.Emit. Within the event handler, Ldarg_0 is not a reference to 'this', but rather the first parameter of the event handler. Does anyone know

Feeding an object literal to ILGenerator

≡放荡痞女 提交于 2019-12-01 05:40:31
Food obj = ...; ILGenerator gen = (...).GetILGenerator(); gen.Emit( ?? obj ?? ); // replace this gen.Emit(OpCodes.Call, typeof(Person).GetMethod("Eat")); It's apparently not possible to cleanly push obj onto the evaluation stack, but I am open to ugly hacks which might compromise e.g. portability. ModuleBuilder.DefineInitializedData allows one to store a System.Byte[] in the .sdata. Any ideas? Edit: the generated method is being emitted as part of a new assembly. object o = ...; Func<object> sneaky = () => o; gen.Emit(OpCodes.Call, sneaky.Method); On a side note, make sure you can't use System

IL Emit for invoking a delegate instance?

 ̄綄美尐妖づ 提交于 2019-12-01 04:58:51
Basically, I'm accepting an event name as a string, to get the EventInfo . Then, I'm discovering the event handler type and event argument type using reflection, creating a new delegate of that type ( myEventHandler ), and hooking it up with the event. When ever myEventHandler is invoked, I need to downcast and pass the arguments to the handler. My code is as below. The 'handler' needs to be invoked via myEventHandler , when ever 'd' is invoked. I need to have some Reflection emit code there where I put ???. Any thoughts? EventHandler handler = delegate(object sender, EventArgs eventArgs) { /

Overriding property definitions with Reflection.Emit

*爱你&永不变心* 提交于 2019-12-01 04:32:00
I'm trying to implement this pattern using Reflection.Emit (TypeBuilder): public class ClassToBeProxied { public virtual object Property1 { get; set; } } public class Proxy : ClassToBeProxied { [AttributeToBeAdded] public override object Property1 { get { //do something else to return the object - i.e get it from the database return null; //stub } set { //do something else to set the object - i.e, save it to a database } } } If all I were doing was intercepting the get and set methods, then this works: PropertyInfo info = typeof(ClassToBeProxied).GetProperty("Property1", BindingFlags.Public |

ILGenerator: How to add boolean to the stack

北慕城南 提交于 2019-12-01 04:13:07
Here is the way I can put float value to the stack(in C#): ILGenerator gen = method.GetILGenerator(); gen.Emit(OpCodes.Ldc_R4, (float)12.5); How can I put boolean value to the stack by using Emit method? There is no representation of a boolean value on the evaluation stack. The bool, char, byte, ushort, uint, and their signed variants are all represented as a 4-byte signed integer (i4). True: ldc.i4.1 False: ldc.i4.0 来源: https://stackoverflow.com/questions/1387010/ilgenerator-how-to-add-boolean-to-the-stack

Overriding property definitions with Reflection.Emit

≡放荡痞女 提交于 2019-12-01 02:16:35
问题 I'm trying to implement this pattern using Reflection.Emit (TypeBuilder): public class ClassToBeProxied { public virtual object Property1 { get; set; } } public class Proxy : ClassToBeProxied { [AttributeToBeAdded] public override object Property1 { get { //do something else to return the object - i.e get it from the database return null; //stub } set { //do something else to set the object - i.e, save it to a database } } } If all I were doing was intercepting the get and set methods, then

Howto emit a delegate or lambda expression

你。 提交于 2019-11-30 15:55:37
I want to emit a method that returns a Func<>. Inside this method I have to create a delegate or a lambda expression which exactly serves the return type. Altogether it should look like this: // I have a resolve method that will be called inside my missing method // This is it's signature: object Resolve( params object[] args); // This is how I use it: var barFactory = ( Func<IBar> )MissingMethod( typeof( IBar ) ); var bar = barFactory.Invoke(); // or - with one string argument: var fooFactory = ( Func<string, IFoo> )MissingMethod( typeof( IFoo ), typeof( string ) ); var foo = fooFactory