Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# string to executed code

string

  • Please log in to reply
13 replies to this topic

#1 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 14 September 2012 - 03:47 PM

Here is a million dollar question ... I'm trying to do something new ...

if I have a block of code in string ... and I want to excute it ... does anyone have any experience with this?

For example ...

string text = "MessageBox.Show('wow')";

I'm expected this code to show a Messagebox on the screen, meaning the string is dynamically created.

I searched google and found a couple: Reflection.Emit, and
http://www.codeproje...e-Eval-Function

but it's too complicated to implement ... Microsoft also just released "Microsoft Roslyn" that does these stuffs. If anyone has any experience related to this topic please share your options.

Thanks
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#2 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 14 September 2012 - 03:56 PM

Maybe when you are trying to execute your string as a code you should try to remove stuff that doesn't belong to the code. You can add C# compiler to your application but you will need to set up the compiler. If I am getting it right you want to write a code in your application but with "string text =" and if I am correct, than you should just remove that part with Regex or something else before you parse that code to the compiler.
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#3 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 15 September 2012 - 09:15 AM

hmm ... I dont follow what you're writing or maybe you didn't understand my question.

My question is that if I have a string something like this:


string myText = @"
int num = 10;
Console.WriteLine(num.ToString());
";
something.execute(myText);
The console should write 10. This is a bit more advance in the programming ... the reason behind this is I want to write code on the fly instead of recompiling again and again.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#4 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 15 September 2012 - 11:00 AM

I understand your question. Try to find on google how to import and use C# compiler in your application. When you figure out how to set a compiler in your code it would be easy to execute what's behind myText. I was working on similar project but with ILASM insted of C#.
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#5 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 15 September 2012 - 12:31 PM

I dont know why do you have to import the compiler ... but I was able to do this after 2 hours of research by using ScriptEngine. For those who are interested in doing the same thing ... can check it out here:

http://blogs.msdn.co...ipting-api.aspx

I'm fairly new at this .... so I'll be reading more info on it ... but I was able to do what I want to do.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#6 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts
  • Programming Language:C, C++, C#, Perl, Visual Basic .NET, Assembly, VBScript, Others

Posted 16 September 2012 - 04:40 PM

Funnily enough I created an application that does this a while back :)

http://tech.reboot.p...=23465#pid23465

Posted Image
  • 1

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image


#7 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 17 September 2012 - 05:47 AM

In case you are considering deploying this anywhere, I suggest being real good at permissions before hand. :worry:
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#8 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 17 September 2012 - 10:05 AM

it looks good ... i think it's using CodeDom ... I believe CodeDom comes with .NET framework ... I don't think you need permission for deployment.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#9 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 17 September 2012 - 11:17 AM

I wasn't talking about permissions for deployment.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#10 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts
  • Programming Language:C, C++, C#, Perl, Visual Basic .NET, Assembly, VBScript, Others

Posted 17 September 2012 - 03:40 PM

it looks good ... i think it's using CodeDom ... I believe CodeDom comes with .NET framework ... I don't think you need permission for deployment.


It is using CodeDom, I wrote the program.

I wasn't talking about permissions for deployment.


Permissions for what then? I don't understand.
  • 0

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image


#11 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 18 September 2012 - 04:39 AM

Something like:

System.IO.File.Delete, or any other System.IO or file stream operation.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#12 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts
  • Programming Language:C, C++, C#, Perl, Visual Basic .NET, Assembly, VBScript, Others

Posted 18 September 2012 - 02:23 PM

Something like:

System.IO.File.Delete, or any other System.IO or file stream operation.


For the most part you can check use a Try Catch to do it the dirty way if that's a concern. But most cases that i've ran into permissional IO exceptions were caused by resources that were already in use by some other resource that had a lock or handle on that filesystem object.

Although, do remember. That this is CodeDom, not System.IO operations... It all depends on what you're trying to do with the runtime executed code compiled by CodeDom methods. And in that case, it's basically the same as if you were in your IDE and considering how to avoid runtime errors... Nothing really to worry about at this point in time.
  • 0

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download