2

I have a string expresion Like ( This will be a string)

"Func<int, string> myfunc = (var1 => (var1 == 0 ? 'ok' : var1 == 1 ? 'Running' : 'Fals'));"

I want to convert this expresiion to a valid lambda exression Like

Func<int, string> myfunc = (var1 => (var1 == 0 ? "ok" : var1 == 1 ? "Running" : "Fals"));

How i can parse my string expression to lambda

Update

I think i want just remove 1st and last "" (double quotes) in c# code via . and i think the string.Romove() is good(Any other way will appreciate) .but I don't know how can remove that ? Any one help me

New Updated:

my actual work is convert c# language code to vb.net language code to etc. The user put a code like string format .So i want to change string to c# code.

  • 2
    This isn't trivial. What is it that's creating the string? Can the user type in anything? – George Duckett Apr 16 '13 at 13:59
  • Not sure on your intended usage, but there are some expression evaluators that can do work _like_ this such as [FLEE](http://flee.codeplex.com/) and [NCalc](http://ncalc.codeplex.com/). Perhaps they might be applicable to your specific usage. – Chris Sinclair Apr 16 '13 at 14:02
  • I think i want just remove 1st and last "" (double quotes) in c# code via . –  Apr 16 '13 at 14:02
  • As George already told you, it's not simple. You can try to "piggyback" on some of the [dynamic linq](http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx) functionality, but as they say, "your actual mileage may vary". – Sergey Kalinichenko Apr 16 '13 at 14:04
  • You will need to include a scripting engine in your project. [See this question][1] [1]: http://stackoverflow.com/questions/137933/what-is-the-best-scripting-language-to-embed-in-a-c-sharp-desktop-application – Captain Kenpachi Apr 16 '13 at 14:04
  • You will need to include a scripting engine in your project. [See this question][1] [1]: http://stackoverflow.com/questions/137933/what-is-the-best-scripting-language-to-embed-in-a-c-sharp-desktop-application – Captain Kenpachi Apr 16 '13 at 14:05
  • 3
    What's your actual goal? There's likely to be a more straightforward way of accomplishing it. – Daniel Mann Apr 16 '13 at 14:06
  • 1
    I was posting an answer. Don't know I it ended up as a comment. Thanks for the info though, it's useful. – Captain Kenpachi Apr 16 '13 at 14:06
  • Hey boys . just i want to how to remove the "" in my string ? –  Apr 16 '13 at 14:09
  • @RameshRams It's not that simple. Please explain what you're actually trying to accomplish, and someone can help point you in the right direction. – Daniel Mann Apr 16 '13 at 14:11
  • 1
    @RameshRams: If you simply removed the `"` from your string then you'd end up with a string, not a `Func` that could be invoked. Answers here are talking about getting that func from a string. To remove the `"` just do `MyString = MyString.Trim('"');` – George Duckett Apr 16 '13 at 14:11
  • my actual work is convert c# language to vb language to etc. So user put string code.So i want to change string to c# code –  Apr 16 '13 at 14:14
  • Maybe edit your question with the code you imagine ending up with, maybe using a `MagicMethod(MyString)` or something, explaining that you're not sure what the MagicMethod would be. It would be useful to know where the string comes from and how the result will be used. – George Duckett Apr 16 '13 at 14:14
  • 1
    Ok, so basically this is string manipulation. We've all got the wrong end of the stick because you've said you want to convert a string to a "lambda", what you seem to want is a string that contains C# code to make a lambda. This still isn't trivial though. I've posted code (in a comment above) to solve this specific case, but not the general one. – George Duckett Apr 16 '13 at 14:16
  • @RameshRams - do you input just one line like that - or an entire `functional unit` - and how does that 'line of code' relate to anything else (in the 'syntax tree') - i.e. do you have a `myfunc` somewhere else. As this alone doesn't do anything. – NSGaga-mostly-inactive Apr 16 '13 at 14:25
  • Dynamic compilation http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments – sh1ng Oct 26 '13 at 10:52

2 Answers2

7

You should be able to convert C# code to VB.NET back and forth using Roslyn. There is a sample project that comes with it and shows how to perform a convesion between these two languages.

The sample is called "Paste as C#/VB". Its code will give you understanding of what interactions are needed to solve the task. To be more precise a code that you are looking for is located in Converting directories of CSharpToVisualBasicConverter and VisualBasicToCSharpConverter projects. That code is a good read if you have enough time to dig in it.

As a note Roslyn project currently implements only C# and VB.NET compilers so there will be no "etc. language" so you will need to write another syntax tree visitor and a hand written converter.

Alexander Manekovskiy
  • 3,185
  • 1
  • 25
  • 34
1

I'm not sure I understood the question but I'll give it a try.

Using your example string, let's say you just want to remove the leading and trailing double quotes:

var vbString = "\"Func<int, string> myfunc = (var1 => (var1 == 0 ? 'ok' : var1 == 1 ? 'Running' : 'Fals'));\"";
var csString = vbString.Substring(1, vbString.Length - 2);

If you also want to replace single quotes with double quotes:

var vbString = "\"Func<int, string> myfunc = (var1 => (var1 == 0 ? 'ok' : var1 == 1 ? 'Running' : 'Fals'));\"";
var csString = vbString.Substring(1, vbString.Length - 2).Replace("'", "\"");

You could also make sure the string is enclosed within double quotes before stripping them:

var vbString = "\"Func<int, string> myfunc = (var1 => (var1 == 0 ? 'ok' : var1 == 1 ? 'Running' : 'Fals'));\"";
var csString = vbString.StartsWith("\"") && vbString.EndsWith("\"") ? vbString.Substring(1, vbString.Length - 2).Replace("'", "\"") : vbString.Replace("'", "\"");
Henrique Miranda
  • 1,030
  • 1
  • 13
  • 31