Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,

Create a Textbox, user can keyin C# commands/codes within. for example, the "For Loop" and some action syntax.

after the user press the "OK" button, the software/application will process the code and perform some action.

Anyone has any clue? How to do this?

Thanks.

#Update:
Oh, I see. I haven't make my question clear. em.. yes, its for windows Application.

and many thanks again for your advices and answers. These have gave me a clue on it. and I'll study it. thanks again
Posted
Updated 7-Apr-11 21:22pm
v2
Comments
walterhevedeich 8-Apr-11 3:08am    
Not clear. You havent even mentioned if what you wanted was a solution for web or for windows application. Clarify your question so we can help you.
Sergey Alexandrovich Kryukov 8-Apr-11 23:17pm    
It does not really matter. The root of it is CodeDOM, no matter what's the hosting of it, can be anything, in fact.
Please see my Answer.
--SA
Sandeep Mewara 8-Apr-11 3:11am    
Not clear. Please edit and rephrase and update it more details.
Sergey Alexandrovich Kryukov 8-Apr-11 23:14pm    
Clear enough to me, as I answered several times to similar Questions.
Please see my Answer.
--SA
Sandeep Mewara 9-Apr-11 0:39am    
as I answered several times to similar Question
:) :thumbsup:

Is this what you are saying: User will type some code in the textbox and your application will run it?

This can be done using CodeDom and CSharpCodeProvider classes. Have a look at these classes and give a try. You will need to manually wrap Main function around the input for it to compile. Take a look at snippet compiler as well. Might help.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Apr-11 23:13pm    
Good to start from, my 5.
Did you know: I already posted a whole cycle of Answers on related topic. Please see my Answer here.
--SA
Albin Abel 9-Apr-11 2:02am    
Good answer. Also need to note that this opens a security hole. My 5
walterhevedeich 9-Apr-11 2:04am    
Good Advice. voted 5.
Absolutely. I provided detailed Answers here:
code generating using CodeDom[^].

Some more detail here:
Create WPF Application that uses Reloadable Plugins...[^].

I sometime suggest your approach (I appreciate your Question) instead of inventing a language.
Please see my Answer and also this discussion:
Create WPF Application that uses Reloadable Plugins...[^].

A short note: It is not called "real time". Real time is something completely different; there are several meaning of this term. But I understand what you mean. This is a good idea, thank you for the interesting Question.

Good luck.
—SA
 
Share this answer
 
v3
Comments
Albin Abel 9-Apr-11 2:06am    
good discussions over there. my 5
Sergey Alexandrovich Kryukov 9-Apr-11 2:08am    
Thank you, Albin.
--SA
adriancs 11-Apr-11 2:10am    
I was initially thinking of creating a game which the game will be controlled by C# standard commands/codes, and this will replace the used of mouse and arrow key from keyboard.

for example a multiplayer war strategy game (Creating a Tank unit in game), user has to keyin:

Tank t1 = new Tank();
t1.location = new point(5,10);
this.controls.Add(t1);

then, a tank will appear on screen, ready for fight. This is same to move and attack.
A game for C# programmer. ;p :)

So, this make me think of a way to capture user command codes.
After some consideration, processing real command codes will lead to several unpredictable and uncontrollable situations. Therefore, I decided to use "Switch/Case" to capture user keyin codes to perform action.

I don't know the game will be interesting or not. Just a little spark of idea. It may be done, and might not.

However, truly appreciate the info that you gave me. thank you, SA.
Sergey Alexandrovich Kryukov 11-Apr-11 3:31am    
Well, thank you for this explanation and accepting my Answer.
As I already commented, this could require most advanced techniques in handling CodeDOM and especially AppDomain. (Again, don't call it "real-time"; nothing in Windows is real-time; call it "mission-critical" maybe.)

However, the fact I spend so much time on your question says your idea deserve some interest. And I can see some simpler way of working through the AppDomain boundary. You need to use some totally serializeable API which does not require sending any UI specific object, it should send just data used to manipulate the game. This would make implementation perfectly feasible. If you have questions or interested I could get some more time and write about it.

By the way, the game does not need to use any Controls -- just one to render the whole scene (well, maybe also some "non-C#" controls...) Also, with WPF it's much more easy.

Cheers,
--SA
There is an article explaining this here: Compiling .NET code on-the-fly[^]

It may not be exactly what you want, but it will give you the idea.

[edit]

A word of warning: If you implement this in a website, you leave a VERY big security hole in your server. Remember that the code will be run on the server, and .NET has access to anything it needs. If the code the user enters is the C# equivalent of "Delete c:\*.*" then the server will try to do it...

[/edit]
 
Share this answer
 
v2
Comments
Dalek Dave 8-Apr-11 3:46am    
That can be compensated for by having pre-submission checks.
OriginalGriff 8-Apr-11 3:54am    
That's a heck of a lot of checkin' to do there! :laugh:
johannesnestler 8-Apr-11 5:01am    
If finished with checking C# won't be used anymore. ;-)
Sergey Alexandrovich Kryukov 8-Apr-11 23:17pm    
Good to start from, my 5. Did you know: I already posted a whole cycle of Answers on related topic. Please see my Answer here.
--SA
Albin Abel 9-Apr-11 1:58am    
Agreed it is a security issue. My 5

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900