reflection.emit

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 =

Programmatically Adding new attribute for existing class property with Reflection.Emit

对着背影说爱祢 提交于 2019-12-11 03:37:27
问题 I am trying programmatically add new attribute on top of a class member ... using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Text; using System.Threading.Tasks; namespace Test11 { public class SomeAttribute : Attribute { public SomeAttribute(string value) { this.Value = value; } public string Value { get; set; } } // for attribute to be injected the property should be "virtual" public class ClassA { public

Reflection.Emit: How to convert MethodBuilder to RuntimeMethodInfo reliably?

你说的曾经没有我的故事 提交于 2019-12-10 19:27:35
问题 After generating a type dynamically and calling TypeBuilder.CreateType, I want to create a delegate that points to a method in the new type. But if I use code like loadedType = typeBuilder.CreateType(); myDelegate = (MyDelegate)Delegate.CreateDelegate( typeof(MyDelegate), methodBuilder); Reusing the methodBuilder as a methodInfo, I get the exception "MethodInfo must be a RuntimeMethodInfo". Now normally I can re-acquire the MethodInfo with MethodInfo mi = loadedType.GetMethod(methodBuilder

Copy method from another AppDomain's assembly and execute it from CurrentDomain

旧巷老猫 提交于 2019-12-10 17:40:04
问题 In the big picture I am trying to execute method from dll in the main domain but after that, unload that dll. So far I've created new AppDomain loaded the Assembly \dll there, with MarshalByRefObject I fetched the body and MaxStackSize of the method to the main domain, created DynamicMethod there, recreated the body inside it and invoked it. But when I invoke it I get exception: System.BadImageFormatException: Signature is not IMAGE_CEE_CS_CALLCONV_LOCAL_SIG . The code that fetches and

Generating IL for 2D Arrays

爱⌒轻易说出口 提交于 2019-12-10 16:17:20
问题 I want to generate IL for the 2D array construction, using System.Reflection.Emit namespace. My C# code is Array 2dArr = Array.CreateInstance(typeof(int),100,100); Using ildasm , I realized that following IL Code is generated for the above C# code. IL_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) IL_000b: ldc.i4.s 100 IL_000d: ldc.i4.s 100 IL_000f: call class [mscorlib]System.Array [mscorlib]System.Array:

工具导航Map