reflection.emit

Using Reflection.Emit to create a class implementing an interface

断了今生、忘了曾经 提交于 2019-11-28 19:41:10
I need to generate a class using Reflection.Emit that implements the following interface. public interface IObject { T Get<T>(string propertyName); } Does anyone have an example of how I would emit the following as a simple test case? class GeneratedObject : IObject { public T Get<T>(string propertyName) { // this is the simplest possible implementation return default(T); } } If you're using Reflection.Emit, you really ought to grab a copy of the Reflection.Emit language add-in for Reflector . While not perfect, it should get you at least 95% of the way to any given emitted code. I don't have

Reflection.Emit better than GetValue & SetValue :S

眉间皱痕 提交于 2019-11-28 18:50:53
I've been told to use Reflection.Emit instead of PropertyInfo.GetValue / SetValue because it is faster this way. But I don't really know what stuff from Reflection.Emit and how to use it to substitute GetValue and SetValue. Can anybody help me with this ? Just an alternative answer; if you want the performance, but a similar API - consider HyperDescriptor ; this uses Reflection.Emit underneath (so you don't have to), but exposes itself on the PropertyDescriptor API, so you can just use: PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj); props["Name"].SetValue(obj, "Fred");

Actual Performance of Fields vs. Properties

孤街浪徒 提交于 2019-11-28 12:09:39
I'm doing some post-build CIL weaving that adds CIL to all methods in an assembly (in other words tons of methods). Each method checks if a specific value is null. Example (C# Reflector'd version of CIL code): // CIL woven region start if (MyType.Something == null) { // ... some new stuff } // CIL woven region end What is the performance impact of having MyType.Something as a Property vs. a Field? I know I've read that the C# compiler performs special optimizations and there should be no performance impact in that case...but what about in the case of direct CIL code (no C# compiler)...? Or is

Is it possible to add a method to an EXISTING class at runtime? why or why not?

混江龙づ霸主 提交于 2019-11-28 09:20:21
I would imagine this might use Reflection.Emit, but a similar question on SO only answers how to create a class/method dynamically, not how to update an existing class. In a similar vein, is it possible to delete methods / classes at runtime? If so, I suppose one could just delete the class, and add it back with its old methods plus the new one. Thanks in advance. P.S. I don't have an intended use for this, it is merely a matter of curiosity. In regular C# / .NET, the answer is a simple "no". The most you can do is write a DynamicMethod which can behave like a method of that type (access to

Generate dynamic method to set a field of a struct instead of using reflection

∥☆過路亽.° 提交于 2019-11-28 06:04:05
Let's say I have the following code which update a field of a struct using reflection. Since the struct instance is copied into the DynamicUpdate method, it needs to be boxed to an object before being passed . struct Person { public int id; } class Test { static void Main() { object person = RuntimeHelpers.GetObjectValue(new Person()); DynamicUpdate(person); Console.WriteLine(((Person)person).id); // print 10 } private static void DynamicUpdate(object o) { FieldInfo field = typeof(Person).GetField("id"); field.SetValue(o, 10); } } The code works fine. Now, let's say I don't want to use

Modifying Existing .NET Assemblies

若如初见. 提交于 2019-11-28 05:30:35
Is there a way to modify existing .NET assemblies without resorting to 3rd party tools? I know that PostSharp makes this possible but I find it incredibly wasteful that the developler of PostSharp basically had to rewrite the functionality of the whole System.Reflection namespace in order to make existing assemblies modifiable. System.Reflection.Emit only allows the creation of new, dynamic assemblies. However, all the builder classes used here inherit from the basic reflection classes (e.g. TypeBuilder inherits from System.Type ). Unfortunately, there doesn't seem to be a way to coerce an

How to create a method at runtime using Reflection.emit

只愿长相守 提交于 2019-11-27 19:54:53
I'm creating an object at runtime using reflection emit. I successfully created the fields, properties and get set methods. Now I want to add a method. For the sake of simplicity let's say the method just returns a random number. How do I define the method body? EDIT: Yes, I've been looking at the msdn documentation along with other references and I'm starting to get my head wrapped around this stuff. I see how the example above is adding and/or multplying, but what if my method is doing other stuff. How do I define that "stuff" Suppose I was generating the class below dynamically, how would I

Is there kind of runtime C++ assembler library around? [closed]

一世执手 提交于 2019-11-27 18:19:38
问题 For my small hobby project I need to emit machine code from C++ program in runtime. I have base address 0xDEADBEEF and want to write something like this: Assembler a((void*)0xDEADBEEF); a.Emit() << Push(Reg::Eax) << Push(Reg::Ebx) << Jmp(0xFEFEFEFE); Inline assembler isn't my choice because generated machine code is dependent of the program state. Does anybody know any existing library for doing this? If no, would it be a good idea to develop one from scratch and make it open source? (I mean,

Cannot bind to the target method when creating delegates for properties

雨燕双飞 提交于 2019-11-27 16:34:05
问题 Trying to create two dictionaries of emitted delegates to allow for improved performance when dynamically getting/setting the values of properties. Code: Properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.CanRead && !p.GetIndexParameters().Any()) .AsEnumerable(); PropertyGetters = Properties.ToDictionary(p => p.Name, p => (Func<object, object>)Delegate.CreateDelegate(typeof(Func<object, object>), p.GetGetMethod())); PropertySetters = Properties.Where(p

Using Reflection.Emit to create a class implementing an interface

半城伤御伤魂 提交于 2019-11-27 12:31:06
问题 I need to generate a class using Reflection.Emit that implements the following interface. public interface IObject { T Get<T>(string propertyName); } Does anyone have an example of how I would emit the following as a simple test case? class GeneratedObject : IObject { public T Get<T>(string propertyName) { // this is the simplest possible implementation return default(T); } } 回答1: If you're using Reflection.Emit, you really ought to grab a copy of the Reflection.Emit language add-in for