reflection.emit

generics with IL?

淺唱寂寞╮ 提交于 2019-12-21 05:01:26
问题 Is it possible to use generics with the IL Generator? DynamicMethod method = new DynamicMethod( "GetStuff", typeof(int), new Type[] { typeof(object) }); ILGenerator il = method.GetILGenerator(); ... etc 回答1: Yes, it is possible, but not with the DynamicMethod class. If you are restricted to using this class, you're out of luck. If you can instead use a MethodBuilder object, read on. Emitting the body of a generic method is, for most intents and purposes, no different from emitting the body of

Dynamic C#.NET Webservice

▼魔方 西西 提交于 2019-12-20 10:25:24
问题 I am using a class in a C# ASP.NET project to allow a script written in some random scripting language to expose webservice methods dynamically - in other words, the script should be able to expose a method of any name with any signature (as long as it's valid, anyway) to the outside world through this SOAP interface (able to add and remove them at will, without needing a hard code change), and as such I need to be able to create a webservice class in C# while being able to dynamically add

How to emit a Type in .NET Core

穿精又带淫゛_ 提交于 2019-12-20 08:57:53
问题 In C#, how do I emit a new Type at runtime with .NET Core? All of the examples I can find for .NET 6 don't seem to work in .NET core (they all begin with getting the current AppDomain, which doesn't exist in .NET core any more). If possible I would appreciate an example that involves creating a Type and adding a property to the Type. 回答1: Here is SO post about creating a dynamic type in .NET 4. How to dynamically create a class in C#? And in the accepted answer is just one use of AppDomain .

What should I pin when working on arrays?

久未见 提交于 2019-12-20 02:46:14
问题 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

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

a 夏天 提交于 2019-12-20 01:41:30
问题 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. 回答1: In order to emit the setter

IL Emit for invoking a delegate instance?

廉价感情. 提交于 2019-12-19 07:45:27
问题 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

Java Equivalent of Reflection.Emit

浪尽此生 提交于 2019-12-18 03:06:16
问题 As far as I can tell, Java has no such equivalent of C#'s Reflection.Emit stuff. Are there any additional libraries for Java that provide similar functionality? What are the differences (to reflection emit )? 回答1: Besides Darin's excellent answer (+1), ASM is worth checking out too. 回答2: The Byte Code Engineering Library (BCEL) 来源: https://stackoverflow.com/questions/2259323/java-equivalent-of-reflection-emit

Reflection.Emit vs CodeDOM

邮差的信 提交于 2019-12-17 08:26:00
问题 What are some pros/cons for using the Reflection.Emit library versus CodeDOM for dynamically generating code at runtime? I am trying to generate some (relatively complicated) dynamic classes in a system based on metadata available at runtime in XML form. I will be generating classes that extend existing classes in the application assembly, implementing additional interfaces, adding methods, and overriding virtual and abstract members. I want to make sure I select the appropriate technique

Reflection.Emit vs CodeDOM

落花浮王杯 提交于 2019-12-17 08:24:47
问题 What are some pros/cons for using the Reflection.Emit library versus CodeDOM for dynamically generating code at runtime? I am trying to generate some (relatively complicated) dynamic classes in a system based on metadata available at runtime in XML form. I will be generating classes that extend existing classes in the application assembly, implementing additional interfaces, adding methods, and overriding virtual and abstract members. I want to make sure I select the appropriate technique

OpCodes.Castclass. Is it necessary?

旧时模样 提交于 2019-12-13 15:03:07
问题 Is it necessary to emit OpCode.CastClass(typeof(A)) when you having a reference to instance of (B) on top of stack, where B is class, derived from A, when preparing for a call to method with argument of type A? Addition: interface IFoo { void IFoo(); } public class A:IFoo { public void IFoo() { } } public class B:A,IFoo { new public void IFoo() { } } var b = new B(); (b as IFoo).Foo(); ((b as A) as IFoo).Foo(); 回答1: I guess you have something like this: class A { public void Foo() { } } class