Click here to Skip to main content
12,688,926 members (31,050 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# .NET
Hello folks,[^]

Is there any way to convert a string of code to a delegate now or is it still impossible?
Posted 16-Jan-13 6:33am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can...but I wouldn't.
Reflection.Emit can do what you ask for:[^]

However, it is a big security risk - depending on what your users can type, is what they can run:
Process.Start(@"Format c: /Y");
and such like would be rather nasty...

If you want to evaluate expressions they type, they why not use this: Math Parser .NET C#[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Are you looking for DynamicLinq[^]?
Or you might have a look at my article about Invent your own Dynamic LINQ parser[^]. Both convert some "expression" text into a lambda expression that can be used e.g. in linq expressions.

If you look for something more elaborate that goes beyond expressions, you might have to look into MSDN: System.CodeDom.Compiler Namespace[^].


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170113.4 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100