reflection.emit

Linking a .NET Expression Tree into a new assembly

故事扮演 提交于 2019-12-03 15:38:22
I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies. The problem is, I can't figure out how to actually build these assemblies. The MethodBuilder class only accepts raw MSIL method bodies, so I have to get the raw MSIL of my expression tree. Calling Expression.Compile() returns a working delegate but I'm not able

Create a class dynamically with Reflection.Emit. I got stuck

旧时模样 提交于 2019-12-03 12:34:38
A read about creating types at runtime and i found it amazing. My goal is to create this class: [DelimitedRecord(",")] public class Person { [FieldOrder(0)] private string firstName; [FieldOrder(1)] private string lastName; public string FirstName { get { return firstName; } set { firstName = value; } } public string LastName { get { return lastName; } set { lastName = value; } } } I did this: //create the builder AssemblyName assembly = new AssemblyName("FileHelpersTests"); AppDomain appDomain = System.Threading.Thread.GetDomain(); AssemblyBuilder assemblyBuilder = appDomain

Creating a class for an interface at runtime, in C#

↘锁芯ラ 提交于 2019-12-03 11:57:51
I'm looking at taking a set of objects, let's say there's 3 objects alive at the moment, which all implement a common interface, and then wrap those objects inside a fourth object, also implementing the same interface. The fourth object's implementations of methods and properties would simply call the relevant bits on those 3 underlying objects. I know that there will be cases here where it won't make sense to do that, but this is for a service multicast architecture so there's already a good set of limitations in place. My question is where to start. The generation of that fourth object

How do I add attributes to a method at runtime?

和自甴很熟 提交于 2019-12-03 04:27:25
问题 We're using Microsoft.Practices.CompositeUI.EventBroker to handle event subscription and publication in our application. The way that works is that you add an attribute to your event, specifying a topic name, like this: [EventPublication("example", PublicationScope.Global)] public event EventHandler Example; then you add another attribute to your handler, with the same topic name, like this: [EventSubscription("example", ThreadOption.Publisher)] public void OnExample(object sender, EventArgs

Curiosity: Why does Expression<…> when compiled run faster than a minimal DynamicMethod?

。_饼干妹妹 提交于 2019-12-03 00:29:31
问题 I'm currently doing some last-measure optimizations, mostly for fun and learning, and discovered something that left me with a couple of questions. First, the questions: When I construct a method in-memory through the use of DynamicMethod, and use the debugger, is there any way for me to step into the generated assembly code, when vieweing the code in the disassembler view? The debugger seems to just step over the whole method for me Or, if that's not possible, is it possible for me to

Dynamic C#.NET Webservice

喜夏-厌秋 提交于 2019-12-02 22:54:07
I am using a class in a C# ASP.NET project to allow a script written in some random scripting language to expose webservice methods dynamically - in other words, the script should be able to expose a method of any name with any signature (as long as it's valid, anyway) to the outside world through this SOAP interface (able to add and remove them at will, without needing a hard code change), and as such I need to be able to create a webservice class in C# while being able to dynamically add and remove methods at runtime. Now, the best plan I've been able to come up with so far is (runtime)

How to emit a Type in .NET Core

南楼画角 提交于 2019-12-02 18:00:54
In C#, how do I emit a new Type at runtime with .NET Core? All of the examples I can find for .NET 6 don't seem to work in .NET core (they all begin with getting the current AppDomain, which doesn't exist in .NET core any more). If possible I would appreciate an example that involves creating a Type and adding a property to the Type. Aram Kocharyan Here is SO post about creating a dynamic type in .NET 4. How to dynamically create a class in C#? And in the accepted answer is just one use of AppDomain . AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an,

How do I add attributes to a method at runtime?

人盡茶涼 提交于 2019-12-02 17:41:59
We're using Microsoft.Practices.CompositeUI.EventBroker to handle event subscription and publication in our application. The way that works is that you add an attribute to your event, specifying a topic name, like this: [EventPublication("example", PublicationScope.Global)] public event EventHandler Example; then you add another attribute to your handler, with the same topic name, like this: [EventSubscription("example", ThreadOption.Publisher)] public void OnExample(object sender, EventArgs e) { ... } Then you pass your objects to an EventInspector which matches everything up. We need to

Curiosity: Why does Expression<…> when compiled run faster than a minimal DynamicMethod?

天大地大妈咪最大 提交于 2019-12-02 14:06:27
I'm currently doing some last-measure optimizations, mostly for fun and learning, and discovered something that left me with a couple of questions. First, the questions: When I construct a method in-memory through the use of DynamicMethod , and use the debugger, is there any way for me to step into the generated assembly code, when vieweing the code in the disassembler view? The debugger seems to just step over the whole method for me Or, if that's not possible, is it possible for me to somehow save the generated IL code to disk as an assembly, so that I can inspect it with Reflector ? Why

LambdaExpression CompileToMethod

旧时模样 提交于 2019-12-01 22:56:56
I Have a few lines of code public void CreateMethod<TContract>(Expression<Action<TContract>> method) { var innerMethod = Builder.DefineMethod("SomeName",MethodAttributes.Private); method.CompileToMethod(innerMethod); //more code } However the second line fails. I've tried with different versions of DefineMethod with little luck. Any suggestions? Unfortunately, CompileToMethod requires a static method as its argument (see here ). Therefore, you need to add MethodAttributes.Static to innerMethod 's definition. 来源: https://stackoverflow.com/questions/3992376/lambdaexpression-compiletomethod