Compiling C# for use into a VB application
-
Tuesday, March 13, 2012 1:35 PM
Is there any way that I can compile a piece of C# code written on a file, then insert it in a function and run it(under VB.net)?
http://www.codeproject.com/Articles/1931/C-Compiler (is this any good?)
Muscalu Ștefan ~~~~ Student
All Replies
-
Tuesday, March 13, 2012 2:06 PMModerator
If you really MUST, you can compile the c# code into a DLL file, then you can make a reference to that DLL file and import it into your class....
Any particular reason you can't save yourself the trouble and just use one language(either c# or vb)?
There's plenty of free c# to vb translators out there... and the translation process is relatively painless....
Here's an example of one such site:
http://converter.telerik.com/
If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
- Edited by Paul IshakMicrosoft Community Contributor, Moderator Tuesday, March 13, 2012 2:06 PM
-
Tuesday, March 13, 2012 2:34 PM
I am making a kind of debugger that goes line by line (of a user written code in C#) and displays current memory objects.
I once found a project that compiled a piece of code (user written) in VB and used it in a function but that was a long time ago and i can't find it anymore. :(
Muscalu Ștefan ~~~~ Student -
Tuesday, March 13, 2012 2:46 PMModerator
You intend to use a code written in c#, with a vb project? if so....
It is very easy, you can create a new class library in c#, add your code(remembering that public things will be accessible from outside of your library).
You the build your solution, and in the output of your debug>bin folder will be a copy of your dll file.
If your VB project you go to the solution explorer window, and click "Add reference", navigating to the dll that is in the debug>bin folder of your c# class library project.
Now that you have added a reference, you import it..
Or do you simply need to evaluate the TEXT of a c# code?
in which case, you could simply open the file to a string
If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
-
Tuesday, March 13, 2012 2:58 PM
Looks like there is a misunderstanding.
Let’s say that I have a Form that contains a textbox.
A user inputs this code into the textbox
Int a=1,i; for(i=0;i<10;i++)a=a*10;
I need to compile the code that is in the textbox and use it in a function(run it inside a function in my application).
Can I do that?
Muscalu Ștefan ~~~~ Student -
Tuesday, March 13, 2012 3:43 PM
No there is no C# scripting tool, because that is what you are asking.
Of course you can build a program using csc.exe
http://msdn.microsoft.com/en-us/library/78f4aasd.aspx
But that is not simply doing it scripting and needs more than just only the code you show.
The code can however not been used then in a VB build (assembly) be aware that it is the same for VB scripting or whatever kind of scripting..
Success
Cor- Edited by Cor LigthertMVP Tuesday, March 13, 2012 3:44 PM
-
Tuesday, March 13, 2012 4:12 PMModerator
Looks like there is a misunderstanding.
Let’s say that I have a Form that contains a textbox.
A user inputs this code into the textbox
Int a=1,i; for(i=0;i<10;i++)a=a*10;
I need to compile the code that is in the textbox and use it in a function(run it inside a function in my application).
Can I do that?
Muscalu Ștefan ~~~~ Student
That sounds like a job for codedom:
Heres a couple examples I found with a quick internet search:
http://www.codeproject.com/Articles/1931/C-Compiler (Compile C# in c#)
http://www.codeproject.com/Articles/5472/Compiling-NET-code-on-the-fly (Compile Vb in Vb)
You can easily twist either of these examples to compile either language as well, plus you can use a c#->Vb convertor or vb->C# converter to translate the code between languages.
If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
- Edited by Paul IshakMicrosoft Community Contributor, Moderator Tuesday, March 13, 2012 4:18 PM
- Marked As Answer by Muscalu Stefan Gabriel Tuesday, March 13, 2012 4:39 PM
-
Tuesday, March 13, 2012 4:36 PM
There is no "scripting" period. The code is SOURCE code.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Tuesday, March 13, 2012 4:41 PM
Looks like there is a misunderstanding.
Let’s say that I have a Form that contains a textbox.
A user inputs this code into the textbox
Int a=1,i; for(i=0;i<10;i++)a=a*10;
I need to compile the code that is in the textbox and use it in a function(run it inside a function in my application).
Can I do that?
Muscalu Ștefan ~~~~ Student
That sounds like a job for codedom:
Heres a couple examples I found with a quick internet search:
http://www.codeproject.com/Articles/1931/C-Compiler (Compile C# in c#)
http://www.codeproject.com/Articles/5472/Compiling-NET-code-on-the-fly (Compile Vb in Vb)
You can easily twist either of these examples to compile either language as well, plus you can use a c#->Vb convertor or vb->C# converter to translate the code between languages.
If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
Yes, the second link is the project that I could not remember of, thank you a lot!
Muscalu Ștefan ~~~~ Student

