Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button.

License

NotificationsYou must be signed in to change notification settings

devlooped/SmallSharp

Repository files navigation

Icon SmallSharp

VersionDownloadsLicenseGH CI Status

Icon

Create, edit and run multiple C# top-level programs in the same project 😍

Why

The new-ish C#top-level programsallow a very intuitive, simple and streamlined experience for quickly spiking or learning C#.

One missing thing since their introduction in Visual Studio is that you can only have one suchtop-level program in a project. This means that in order to prototype or learn a different areaof .NET, you'd be forced to either replace your previous top-level program or change it to be anon-compile item somehow so you can keep it around (i.e. rename to a.txt or change its build action).

SmallSharp allows you to select which file should be the top-level program to run, right fromthe Start button/dropdown (for compilation and launch/debug). Moreover, it will monitor the activefile you're editing, and automatically make it the startup file for you!

start button

This list is automatically kept in sync as you add more.cs files to the project. When you selectone target C# file, that becomes the only top-level program to be compiled, so you don't have tomodify any of the others since they automatically becomeNone items.

All compile files directly under the project directory root are considered top-level programs forselection and compilation purposes. If you need to share code among them, you can place them insubdirectories and those will behave like normal compile items.

Usage

There is no need to install any Visual Studio extension. SmallSharp works by just installing theSmallSharp nuget package in a C# console project.

  1. Create a new Console project:

New Project Dialog

By default, this new console project may not be set up to targetnet5.0 or use the latest C# version.So click on the project node, and the project file will open in the editor. Make sure you either:

  • Target the recommended framework version (i.e. net6.0):
<ProjectSdk="Microsoft.NET.Sdk">  <PropertyGroup>    <OutputType>Exe</OutputType>    <TargetFramework>net6.0</TargetFramework>  </PropertyGroup></Project>
  • Or use latest C# language version if targeting another framework:
<ProjectSdk="Microsoft.NET.Sdk">  <PropertyGroup>    <OutputType>Exe</OutputType>    <TargetFramework>net472</TargetFramework>    <LangVersion>latest</LangVersion>  </PropertyGroup></Project>
  1. Install theSmallSharp nuget package using your preferred method:
  • From the Dependencies node, Manage NuGet Packages dialog:

New Project Dialog

  • By just adding it directly to the .csproj:
  <ItemGroup>    <PackageReferenceInclude="SmallSharp"Version="*" />  </ItemGroup>
  • Via the dotnet CLI:
> dotnet add package SmallSharp
  • Via the Package Manager console:
PM> install-package SmallSharp
  1. Now open that Program.cs and make changes to the new concise top-level program such as:
usingSystem;usingstaticSystem.Console;WriteLine("Hello World!");

Keep adding as many top-level programs as you need, and switch between them easily by simplychanging the active document.

Demo

How It Works

This nuget package leverages in concert the following standalone and otherwiseunrelated features of the compiler, nuget, Visual Studio and MSBuild:

  1. The C# compiler only allows one top-level program per compilation.
  2. Launch profiles (the entries in the Run dropdown) are populated from the Properties\launchSettings.json file
  3. Whenever changed, the dropdown selection is persisted as the$(ActiveDebugProfile) MSBuild property in a filenamed after the project with the.user extension
  4. This file is imported before NuGet-provided MSBuild targets
  5. The$(DefaultItemExcludesInProjectFolder) MSBuild property allows excluding items at the project-level fromthe automatically added items by the SDK.

Using the above features in concert,SmallSharp essentially does the following:

  • Monitor the active document in VS and emit it as alaunchSettings.json profile and set it asthe$(ActiveDebugProfile).

  • Exclude.cs files at the project level from being included as<Compile> by the default SDKincludes and include them explicitly as<None> instead so they show up in the solution explorer.This prevents the compiler from causing an error for multiple top-level programs.

  • Explicitly include as<Compile> only the$(ActiveDebugProfile) property value.

This basically mean that this it will also work consistently if you usedotnet run from the command-line,since the "Main" file selection is performed exclusively via MSBuild item manipulation.

Finally, there is some lovely COM-based magic to access the active Visual Studio IDE (via DTE) to monitorthe currently opened source file to keep it in sync with the launch profile. This is done purely usingpublic COM primitives and equally public VSSDK nuget packages their APIs. This enables some useful integrationwith the IDE without requiring installing a VS extension from the marketplace and deal with gracefullydegrading functionality.

NOTE: If active document tracking fails to initialize properly restarting Visual Studio will almostalways fix it. Once tracking starts, it will work consistently from that point on. The Start dropdownis always available as a fallback in case of issues.

Sponsors

Clarius OrgMFB Technologies, Inc.TorutekDRIVE.NET, Inc.Keith PickfordThomas BolonKori FrancisToni WenzelUno PlatformDan SiegelReuben SwartzJacob FosheeEric JohnsonIx Technologies B.V.David JENNIJonathan Charley WuJakob Tikjøb AndersenTino HagerKen BonnySimon Croppagileworks-eusorahexZheyu ShenVezelChilliCream4OTCVincent LimoJordan S. Jonesdomischell

Sponsor this project 

Learn more about GitHub Sponsors

About

Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

[8]ページ先頭

©2009-2025 Movatter.jp