reflection.emit

Reference a collection from IL constructed method

旧街凉风 提交于 2019-12-13 02:49:49
问题 I am building a dynamic method using reflection. Most tutorials and documentation (e.g. How to: Define and Execute Dynamic Methods or Creating method dynamically, and executing it) show a very simple example. I trying to find a way to reference another assembly from the dynamic assembly. For example, I would like to be able to construct the following function by using Reflection.Emit . public static void f(int n) { int[] arr = new arr[n]; return arr.Max(); } What would be the common way of

System.Reflection.TargetInvocationException: dynamically define constructor with parameters

旧城冷巷雨未停 提交于 2019-12-13 02:38:50
问题 We want to dynamically create a class, the constructor as below: public JsRF1013Wrapper(ScriptEngine scriptEngine, string jsFileFullPath) { this.ScriptEngine = scriptEngine; var jsFileContent = File.ReadAllText(jsFileFullPath); this.ScriptEngine.Execute(jsFileContent); } we got the IL: .method public hidebysig specialname rtspecialname instance void .ctor(class [ClearScript]Microsoft.ClearScript.ScriptEngine scriptEngine, string jsFileFullPath) cil managed { // Code size 37 (0x25) .maxstack 2

Using Reflection.Emit to implement a interface

扶醉桌前 提交于 2019-12-12 21:13:36
问题 Let's say that I have the following interface: public interface IMyService { void SimpleMethod(int id); int Hello(string temp); } And want to generate a class that looks like this (using reflection emit). public class MyServiceProxy : IMyService { IChannel _channel; public MyServiceProxy(IChannel channel) { _channel = channel; } public void SimpleMethod(int id) { _channel.Send(GetType(), "SimpleMethod", new object[]{id}); } public int Hello(string temp) { return (int)_channel.Request(temp); }

Reflection.Emit throws BadImageFormatException

こ雲淡風輕ζ 提交于 2019-12-12 18:35:03
问题 Im trying to generate a new class/object at runtime. After reading How to create a private property using PropertyBuilder, i've managed to get everyting implemented and everything is like i need it. But as soon as im trying to instanciate my new object, im receiving a BadImageFormatException This seems to be a similar issue, but unresolved Is there any way to instrument System.Reflection.Emit? Here my code: Field: internal class Field { public string FieldName; public Type FieldType; public

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

允我心安 提交于 2019-12-12 09:41:32
问题 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 :-))

How to use OpCodes.Call to generate this code

匆匆过客 提交于 2019-12-12 04:54:20
问题 This question is related to: Casting items of a collection with code generation Since the previous question was not clear enough, here is what I need help with precisely. How to use OpCodes.Call to generate this code: return Enumerable.ToList<Potato>(Eumerable.Cast<Potato>(_proxyPotatoes)); Here is an example of what I'm trying to do: public class Potato { } public class ProxyPotato : Potato { } public class Stew { private ICollection<ProxyPotato> _proxyPotatoes; //This is the code I would

Using C# OpCodes to emit method that returns an object

纵饮孤独 提交于 2019-12-12 03:08:25
问题 I am creating a dynamic type which has a method that I'd like to return an object. I am failing to understand how to achieve this. Here's what I have so far: // .. stuff to create type builder MethodBuilder builder = typeBuilder.DefineMethod( method.Name, MethodAttributes.Virtual | MethodAttributes.Public, method.CallingConvention, method.ReturnType, typeArray1); builder.InitLocals = true; ILGenerator gen = builder.GetILGenerator(); Object myObjectIdLikeToReturn = someMethodCall(); //gen.??(?

C# Reflection IL - Understanding how values are copied

本秂侑毒 提交于 2019-12-12 02:27:51
问题 I'm trying to improve the performance of a certain part of my program which involves deep cloning the same object graph over and over across multiple threads. Currently I use serialization which is a nice easy implementation but I'd like something faster. I came across the idea of IL cloning and am trying to work with some code found here (Whizzo's Blog). I don't really get IL as yet, so I'm hoping someone can help a little bit and explain some of the stuff to me (I imagine this is the first

Using FastActivator in place of Activator.CreateInstance()

≯℡__Kan透↙ 提交于 2019-12-12 00:22:00
问题 Trying to use Class shown here as a sample for Activator.CreateInstance() http://codeblocks.codeplex.com/wikipage?title=FasterActivator%20Sample public static List<T> SortedCollection<T>(SPListItemCollection items, ListSortType sortType, List<Vote> votes) where T : IVotable { var returnlist = new List<T>(); var functionCreator = FastActivator.GenerateFunc<Func<SPListItem, List<Vote>, T>>(); for (int i = 0; i < items.Count; i++) { returnlist.Add(functionCreator(items[i], votes)); } } switch

How to Emit code to assign value/reference to static field of class by calling it's constructor?

社会主义新天地 提交于 2019-12-11 20:19:43
问题 (My code is somewhat a mess of C# and VB.NET) I am trying to Emit class that looks as following: public class SWTTFields { private string fieldName; private int startPosition; private int endPosition; public static readonly SWTTFields ISO = new SWTTFields("ISO", 1, 2); public static readonly SWTTFields EPC = new SWTTFields("EPC", 3, 4); private SWTTFields(String fieldName, Int32 startPositon, Int32 endPositon) { this.fieldName = fieldName; this.startPosition = startPositon; this.endPosition =