Dynamic function creation at runtime

It may not even be possible to do, but I will ask anyway. Is it possible to create a function that receives a string and then uses it as a right argument to jump to the operator (=>) used in lambda?

In fact, I want to be able to override the specific method of a particular class at runtime. I want to write a function with a running program and associate it with a delegate. Is it possible?

+6
c # lambda delegates
source share
7 answers

The easiest way to do this is probably DLINQ, as suggested by TcK.

The fastest (I believe in 3.5) is to create a DynamicMethod . This is also the scariest method. You essentially build a method using IL, which has about the same feel as writing code in a machine language.

I needed to do this in order to dynamically attach event handlers in one or another (well, I didnโ€™t need to do this, I just wanted to make it easier to test the modules). It seemed a bit complicated at the time because I donโ€™t know shit about IL, but I came up with an easy way to achieve this.

What you do is create a method that does exactly what you want. The more compact, the better. I would give an example if I could find out exactly what you are trying to do. You write this method in a class in a DLL project and compile it in release mode. Then you open the DLL in Reflector and parse your method. Reflector gives you the opportunity to choose the language you want to parse - select IL. You now have the exact calls you need to add to your dynamic method. Just follow the MSDN example by disabling the IL example for your reflected methods code.

Dynamic methods, once built, call at about the same speed as compiled methods (they saw a test in which dynamic methods could be called in ~ 20 ms, where reflection took more than 200 ms).

+8
source share

You have several ways to do this:

+8
source share

Your question is rather obscure, but you can certainly use expression trees to dynamically create delegates at runtime. (There are other ways to do this, such as CodeDOM, but expression trees are more efficient if they do everything you need. However, there are significant limitations on what you can do.)

It is often easier to use a lambda expression with some captured variables.

For example, to create a function that adds the specified amount to any integer, you can write:

static Func<int, int> CreateAdder(int amountToAdd) { return x => x + amountToAdd; } ... var adder = CreateAdder(10); Console.WriteLine(adder(5)); // Prints 15 

If this does not help, clarify your question.

+5
source share

Not that I recommend this compared to other best options, but there is a 7th method for using AssemblyBuilder , ModuleBuilder , TypeBuilder and MethodBuilder in the System.Reflection.Emit to create a dynamic assembly. This is the same voodoo-like one used with DynamicMethod .

For example, you can use them at runtime, create a proxy class for a type, and override virtual methods of that type.

To get started here, this is the code ...

 using System; using System.Reflection; using System.Reflection.Emit; var myAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly( new AssemblyName("Test"), AssemblyBuilderAccess.RunAndSave); var myModule = myAssembly.DefineDynamicModule("Test.dll"); var myType = myModule.DefineType("ProxyType", TypeAttributes.Public | TypeAttributes.Class, typeof(TypeToSeverelyModifyInAnUglyWayButItsNecessary)); var myMethod = myType.DefineMethod("MethodNameToOverride", MethodAttributes.HideBySig | MethodAttributes.Public, typeof(void),Type.EmptyTypes); var myIlGenerator = myMethod.GetILGenerator(); myIlGenerator.Emit(OpCodes.Ret); var type = myType.CreateType(); 
+3
source share

You should check if your problem can be solved with simple polymorphism. Unless you define abstract interoperability in another language or edit the compiler, trying to change the method at runtime is probably the wrong solution.

+1
source share

If you declare the method as virtual, you can use Castle DynamicProxy to replace dynamically generated (with one of the other response methods) at runtime:

DynamicProxy Lock is a library for creating simple .NET proxies on the fly at runtime. Proxy objects allow you to intercept the elements of an object without changing the class code. Both classes and interfaces can be approximated, however only virtual participants can be intercepted.

0
source share

The second paragraph in your question says that really what you need is a simple IOC (Inversion of Control)

Instead of declaring an instance of your class, you declare an instance of the interface, and based on any condition you choose, you use a specific override class with the correct method in it. Hope this makes sense.

0
source share

All Articles