reflection.emit

Explicit interface implementation and Reflection.Emit

给你一囗甜甜゛ 提交于 2019-12-04 07:34:57
Does anybody know how to implement an interface's property explicitly using Reflection.Emit? See the MSDN documentation for TypeBuilder.DefineMethodOverride , which includes an example of using Reflection.Emit to generate an explicit interface implementation using that method. Mark Byers 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/questions/1773710/explicit-interface-implementation-and-reflection-emit

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

拈花ヽ惹草 提交于 2019-12-04 06:16:52
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 = CSharpSyntaxTree.ParseText(@" using System; namespace RoslynCompileSample { public class Writer {

Using Br_S OpCode to point to next instruction using Reflection.Emit.Label

别来无恙 提交于 2019-12-04 05:12:19
问题 I am experimenting with parsing IL in order to emit a method. I have gotten the IL code of a method in a string[] where each string is an IL instruction. I am looping over this array and adding OpCodes using an ILGenerator: foreach (string ins in instructions) //string representations of IL { string opCode = ins.Split(':').ElementAt(1); // other conditions omitted if (opCode.Contains("br.s")) { Label targetInstruction = ilGenerator.DefineLabel(); ilGenerator.MarkLabel(targetInstruction);

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

痴心易碎 提交于 2019-12-04 03:52:44
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 'first' was not given an explicit offset." To me this implies that the CreateType method is looking for

Emitting delegate function call

懵懂的女人 提交于 2019-12-04 03:45:24
I have the following C# code: public static double f2(Func<double, double> f, double x) { return f(x); } And here it's IL code: .method public hidebysig static float64 f2 ( class [mscorlib]System.Func`2<float64, float64> f, float64 x ) cil managed { // Method begins at RVA 0x20bd // Code size 8 (0x8) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldarg.1 IL_0002: callvirt instance !1 class [mscorlib]System.Func`2<float64, float64>::Invoke(!0) IL_0007: ret } How can I to emit callvirt instance !1 class [mscorlib]System.Func`2<float64, float64>::Invoke(!0) insturction through the System.Reflection.Emit

Emit local variable and assign a value to it

醉酒当歌 提交于 2019-12-04 02:37:49
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; 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(OpCodes.Ldc_I4, 6); // Store "6" ... ilGen.Emit(OpCodes.Stloc, b); // ... in "b". ilGen.Emit(OpCodes.Ldloc,

Creating WCF Service at runtime

此生再无相见时 提交于 2019-12-03 21:57:02
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 operation Message Process(Message) accepting everything. We still want to expose the service as a

NHibernate / Fluent NHibernate Dynamic Column Mapping

喜欢而已 提交于 2019-12-03 20:07:50
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 modification (e.g. a "DynamicTable" prefix). The only solution I could think of for this situation is using

transferring one object properties values to another one

我怕爱的太早我们不能终老 提交于 2019-12-03 16:35:47
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 target) { var sourceType = source.GetType(); var targetType = target.GetType(); var sourceProps =

generics with IL?

限于喜欢 提交于 2019-12-03 15:48:29
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 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 other methods, except that you can make local variables of the generic types. Here is an example of