reflection.emit

Reflection.Emit better than GetValue & SetValue :S

回眸只為那壹抹淺笑 提交于 2019-11-27 11:43:26
问题 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 ? 回答1: 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:

Actual Performance of Fields vs. Properties

我是研究僧i 提交于 2019-11-27 06:48:21
问题 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

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

假装没事ソ 提交于 2019-11-27 02:49:11
问题 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. 回答1: In regular C# / .NET, the answer is a simple

Modifying Existing .NET Assemblies

无人久伴 提交于 2019-11-27 01:02: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.

How to create a method at runtime using Reflection.emit

纵然是瞬间 提交于 2019-11-26 22:51:30
问题 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

Call and Callvirt

别来无恙 提交于 2019-11-26 21:55:16
What is the difference between the CIL instructions "Call" and "Callvirt"? call is for calling non-virtual, static, or superclass methods, i.e., the target of the call is not subject to overriding. callvirt is for calling virtual methods (so that if this is a subclass that overrides the method, the subclass version is called instead). Drew Noakes When the runtime executes a call instruction it's making a call to an exact piece of code (method). There's no question about where it exists. Once the IL has been JITted, the resulting machine code at the call site is an unconditional jmp instruction

Fast creation of objects instead of Activator.CreateInstance(type)

不问归期 提交于 2019-11-26 18:42:07
I'm trying to improve the performance of our application. We have a lot of Activator.CreateInstance calls that are causing some grief. We instantiate a lot of classes based on an interface (ITabDocument) and after looking around I thought of using this code: The code is no better (infact marginally slower) than using the Activator.CreateInstance code we had. public static Func<T> CreateInstance<T>(Type objType) where T : class, new() { var dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + objType.Name, objType, null, objType); ILGenerator ilGen = dynMethod.GetILGenerator(); ilGen.Emit(OpCodes

General purpose FromEvent method

冷暖自知 提交于 2019-11-26 10:28:40
Using the new async/await model it's fairly straightforward to generate a Task that is completed when an event fires; you just need to follow this pattern: public class MyClass { public event Action OnCompletion; } public static Task FromEvent(MyClass obj) { TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); obj.OnCompletion += () => { tcs.SetResult(null); }; return tcs.Task; } This then allows: await FromEvent(new MyClass()); The problem is that you need to create a new FromEvent method for every event in every class that you would like to await on. That could get really

Call and Callvirt

杀马特。学长 韩版系。学妹 提交于 2019-11-26 08:05:32
问题 What is the difference between the CIL instructions \"Call\" and \"Callvirt\"? 回答1: call is for calling non-virtual, static, or superclass methods, i.e., the target of the call is not subject to overriding. callvirt is for calling virtual methods (so that if this is a subclass that overrides the method, the subclass version is called instead). 回答2: When the runtime executes a call instruction it's making a call to an exact piece of code (method). There's no question about where it exists.

Fast creation of objects instead of Activator.CreateInstance(type)

拜拜、爱过 提交于 2019-11-26 06:30:45
问题 I\'m trying to improve the performance of our application. We have a lot of Activator.CreateInstance calls that are causing some grief. We instantiate a lot of classes based on an interface (ITabDocument) and after looking around I thought of using this code: The code is no better (infact marginally slower) than using the Activator.CreateInstance code we had. public static Func<T> CreateInstance<T>(Type objType) where T : class, new() { var dynMethod = new DynamicMethod(\"DM$OBJ_FACTORY_\" +