0

I wonder if it is possible to convert text to C# code for dynamic validation. For example, I'd love to be able to store a string in my DB like this:

((Value.StartsWith("CR") || Value.StartsWith("TRC")) && Value.Length == 10)

Then - pull it from the DB and use it to validate data. If would automatically swap out Value for the piece of data I'm trying to validate.

All validations would return a boolean that I would use in the code to see if the data being tested passed or failed.

I would have numerous variations of validations similar to the one above like this stored in my DB.

I've tried researching various phrases on Google but can't seem to think of what this might be called?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
99823
  • 2,407
  • 7
  • 38
  • 60
  • "cool movies", "strange toys" and other various phrases may not be so useful for search in this case. Try "C# parse expression" or "C# evaluate string" instead. – Alexei Levenkov Jan 08 '16 at 03:37
  • Do you mean that if value was replaced with two phrase words? If thats the cause ultimately i would prefer that the two word phrase be stored in a variable called Value... I could be interrupting what you are saying incorrectly, if so i apologize – 99823 Jan 08 '16 at 03:40
  • not an easy job. but there are options. You can use [CodeDOM](https://msdn.microsoft.com/en-us/library/y2k85ax6.aspx). I use [IromPython](http://ironpython.net/) you can execute python code dynamically with it not C# code. there are more options.... – bansi Jan 08 '16 at 03:44
  • I was trying to joke about your "various phrases"... Sorry for that not being explicit. I just recommended different search terms https://www.bing.com/search?q=C%23+parse+expression and https://www.bing.com/search?q=C%23+evaluate+string that has tons of answers on SO for exactly the same problem. – Alexei Levenkov Jan 08 '16 at 03:45
  • ahh gotcha... thanks – 99823 Jan 08 '16 at 03:46
  • 2
    [Is it possible to dynamically compile and execute C# code fragments?](http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments) – bansi Jan 08 '16 at 03:49
  • 1
    You would need to look into serializing & deserializing `Expression>`. – Enigmativity Jan 08 '16 at 05:26
  • 1
    What about a string like this: `^(CR.|TRC).{7}$` ? It means the same thing when used as a regular expression, and can already be stored in a DB. Otherwise, executing arbitrary strings in .Net is seen as a _major security concern_, such it was made possible in the framework, but never simple or easy. Better alternatives are Domain Specific Languages that you can make yourself or take one that already exists, such as the regular expression language previously indicated – Joel Coehoorn Jan 08 '16 at 05:41
  • I like the idea of serializing/deserializing Expression> - thanks. Also - to your point Joel, regex would be my last resort however it is something i've been considering if nothing else works. Also bansi if serializing/deserializing expressions does not work i will be looking into your suggestion next. – 99823 Jan 08 '16 at 13:18

0 Answers0