reflection.emit

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:

Reflection.Emit and generic types

二次信任 提交于 2019-12-10 15:21:10
问题 I am using Reflection.Emit and I want to create a type that would be the equivalent of the following type defined in C#: class A { public Tuple<A, int> GetValue(int x) { return new Tuple<A, int>(this, x); } } The trick is that I need to use a generic type from BCL that uses my custom type as a generic argument. I'm messing with the following snippet: var asmName = new AssemblyName("Test"); var access = AssemblyBuilderAccess.Run; var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName,

MethodBuilder.CreateMethodBody() problem in Dynamic Type creation

我怕爱的太早我们不能终老 提交于 2019-12-10 09:28:01
问题 For an experiment, i am trying to read the method body (using GetILAsByteArray() ) from source type and adding it to the new Type (Using CreateMethodBody() ). My source class is simply this public class FullClass { public string Test(string data) { return data; } public string Test2(string data) { return data; } public string Test5(string data, string data1) { return data + data1; } } The IL generated for this code (taken using reflector) .method public hidebysig instance string Test(string

C# Reflection - How to set field value for struct

佐手、 提交于 2019-12-09 08:41:14
问题 How can I set value into struct field - myStruct.myField with reflection using DynamicMethod? When I call setter(myStruct, 111) value was not set, because MyStruct is value type. Console.WriteLine(myStruct.myField) shows value 3. How to modify GetDelegate method to set value into myStruct.myField ? public struct MyStruct { public int myField; } public delegate void SetHandler(object source, object value); private static SetHandler GetDelegate(Type type, FieldInfo fieldInfo) { DynamicMethod dm

How to extract class IL code from loaded assembly and save to disk?

三世轮回 提交于 2019-12-08 13:33:26
问题 How would I go about extracting the IL code for classes that are generated at runtime by reflection so I can save it to disk? If at all possible. I don't have control of the piece of code that generates these classes. Eventually, I would like to load this IL code from disk into another assembly. I know I could serialise/deserialise classes but I wish to use purely IL code. I'm not fussed with the security implications. Running Mono 2.10.1 回答1: Or better yet, use Mono.Cecil. It will allow you

How to mutate a boxed value type (primitive or struct) in C#/IL

末鹿安然 提交于 2019-12-08 13:07:14
问题 Related to How to mutate a boxed struct using IL I am trying to change the value of a boxed value type but in a generic way, so trying to implement the following method: void MutateValueType<T>(object o, T v) where T : struct So the following should be possible: var oi = (object)17; MutateValueType<int>(oi, 43); Console.WriteLine(oi); // 43 var od = (object)17.7d; MutateValueType<double>(od, 42.3); Console.WriteLine(od); // 42.3 I am failing to get it this to work on .NET Framework (see

Reflection.Emit Building a entity graph

你。 提交于 2019-12-08 08:26:55
问题 I have spent some time trying to building an entity graph dynamically with Reflection.Emit. Creating a assembly with a new flat type (class), instantiate it and use it with reflection is easy and works fine. But when it comes to building a structure with generic lists of yet another dynamic classes, it gets more complicated and I got stuck. Basically, I would like to build the following structure dynamically: public class Head { public string HeadId { get; set; } public AssignmentType