DIY, create a lightweight vscode/c# environment instead of LINQPad

Source: Internet
Author: User

. Net projects are pretty heavy and have been looking for a lightweight CSharp environment that can run like Python and choose a file. The previous use of LINQPad, but its shortcomings are also obvious:

(1) Do not pay, automatic completion can not be used (OK, I purchased);

(2) Ugly! (This can't be tolerated!) Ugly affect the mood, as an inspiration to eat the algorithm developers, can endure expensive, can not endure ugly! )

So do it yourself, change the vscode into a lightweight CSharp environment.

(1) Establish a directory, for example, Vscodetest, enter the directory, dotnet new console to create the project;

(2) Open the directory with Vscode, debug the next program, at this time, the directory automatically generated Launch.json file, modify the file, will

Switch

"Args": ["${file}"]

This way, the path to the currently open file is automatically passed to the Main method when the program executes.

(3) Establish the Scripts directory, establish the Foo.cs file, the contents are as follows:

// RUN:VSCodeTest.Foo.Test using System; namespace vscodetest{    class  Foo    {        publicstaticvoid  Test ()        {            Console.WriteLine ("test Run Script");        }    }}

In the file, the first line//run: Specify the method name to run after

(4) Modify Program.cs as follows:

usingSystem;usingSystem.IO;usingSystem.Reflection;namespacevscodetest{classProgram {Static voidMain (string[] args) {                        if(args. Length = =1) {String fileName= args[0]; if(File.exists (fileName) = =false) {Console.WriteLine ("file does not exist:"+fileName); return; }                varit =File.readlines (fileName).                GetEnumerator (); if(it. MoveNext ()) {String line=it.                    Current; Line=Line .                    Trim (); if(line. StartsWith ("//run:") ) { line= line. Substring (6); Line= line. Replace (";",""); Line=Line .                        Trim ();                    Eval (line); }                }                ElseConsole.WriteLine ("no calling method specified:"+fileName); }        }        Static voidEval (String cmd) {Console.WriteLine ("[Invoke]:"+cmd); intLastidx = cmd. LastIndexOf ('.'); String TypeName= cmd. Substring (0, LASTIDX); String MethodName= cmd. Substring (Lastidx +1); Try            {                varType =Type.GetType (typeName); if(type!=NULL)                {                    varMETHOD =type.                    GetMethod (MethodName); if(Method! =NULL) {method. Invoke (NULL,NULL); return; }} Console.WriteLine ("call method not found:"+cmd); }            Catch(Exception ex) {Console.WriteLine (ex).            Message); }        }    }}

This way, when run, it resolves the specified method name and invokes it through reflection.

So it's OK. Which file is opened and executes the method specified in the file. For example, open the Foo.cs file, press F5 Debug or ctrl+f5 direct execution, you can see, the program executes the specified method.

Advantages:

(1) Support direct implementation;

(2) Support debugging;

(3) Support referencing third-party DLLs, supporting mutual references between files.

Cons: If other files in the project have compile errors, it will affect the execution of the current file.

----

Or vscode look comfortable ah.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.