dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.01k stars 4.03k forks source link

C# Interactive REPL - NuGet support #43918

Open weitzhandler opened 4 years ago

weitzhandler commented 4 years ago

Version Used:

3.5.0-beta4-20153-05

Steps to Reproduce:

  1. Please open REPL
  2. Add package Microsoft.Data.SqlClient to project
  3. > #r "nuget: Microsoft.Data.SqlClient"

Expected Behavior:

Package should be added

Actual Behavior:

Getting error:

(1,1): error CS0006: Metadata file 'nuget: Microsoft.Data.SqlClient' could not be found

knodel12 commented 4 years ago

I'm having this issue as well. Any update?

acherkashin commented 4 years ago

@weitzhandler It seems, C# Interactive doesn't support referencing packages from Nuget. You have to download an assembly and only after that reference it.

But you can use dotnet script, which supports referencing packages. To lean more about it you can read article Hitchhiker’s Guide to the C# scripting

jkone27 commented 3 years ago

doesn't completely solve, but you can use F# for this : https://docs.microsoft.com/en-us/dotnet/fsharp/tutorials/fsharp-interactive/

#r "nuget: Newtonsoft.Json"
open Newtonsoft.Json

let data = {| Name = "Don Syme"; Occupation = "F# Creator" |}
JsonConvert.SerializeObject(data)
maartenba commented 3 years ago

This would be a very welcome addition

kewlniss commented 3 years ago

Would be great, a work around is to find the package in your .NuGet folder

#r "C:\Users\Chad\.nuget\packages\fakerdotnet\1.0.7\lib\netstandard2.0\FakerDotNet.dll"

FakerDotNet.Faker.MichaelScott.Quote()
Regenhardt commented 3 years ago

Which, of course, means you have to manually download that package first. Not very convenient.

vmandic commented 2 years ago

+1 this would be very convenient, and its weird not to have it as F# console has it per @jkone27's previous comment.

ajklotz commented 1 year ago

I'm learning C# and wanted to play around with C# interactively like I did with NodeJS. I started to try and use other libraries but I can't figure out how to bring in other libraries. I tried using #r C:\path\system.net.http.4.3.4.nupkg" but I get an error (2,1): error CS0009: Metadata file 'C:\path\system.net.http.4.3.4.nupkg' could not be opened -- PE image doesn't contain managed metadata.

The ability to use get nuget packages would be nice.

jkone27 commented 1 year ago

@ajklotz you can try F# for this purpose, works much better and has the same .NET APIs and nuget package support, basic synthax is quite easy plus you have ADTs like in Rust, for easy domain modeling instead of inheritance.

it's already installed with dotnet-sdk ---> dotnet fsi your-script.fsx, or open in vscode with ionide or in visual studio after adding F# support

i think also this might work better for you

#r "nuget: System.Net.Http"
open System.Net.Http

let x = new HttpClient()

here is when running it

image

you can execute interactively line by line by selecting and pressing alt/option + ENTER, when i execute the last line, i get all the pretty html from google home page as value of it, which is the last evaluated value of interactive section in F#.

jkone27 commented 1 year ago

a bit "out of topic", but you can also try this one out if you want to see some F# type provider magic ;) https://fsprojects.github.io/FSharp.Data/

#r "nuget:FSharp.Data" 
open FSharp.Data
open System.Linq

// you don't have this in C# not any other language, rust has macros but this is a bit simpler in my view
type MyJsonType = JsonProvider<"""{ "hello" : "compile time value or sample value" }""">

let result = MyJsonType.Parse("""{ "hello" : "prod value" }""")  // parses a http url or a new json

let str = result.Hello

let betterThanLinq = 
    [ 1 ; 2 ; 3 ] 
    |> Seq.map (fun x -> x + 1) 
    |> Seq.sum

let canStillUseLinq =
    [ 
        1
        2
        3
    ].Select(fun x -> x + 1)
     .Sum()

open FSharp.Data
type MyCsvType = CsvProvider<"Country;Nr\r\nItaly;200\r\nNetherlands;30", ";">

let csv = MyCsvType.Parse("Belgium;500")

for r in csv.Rows do
    printfn $"the country is: {r.Country} had {r.Nr} icecreams last year."

type MyHtmlTable = HtmlProvider<"https://vincentarelbundock.github.io/Rdatasets/datasets.html">

let test = MyHtmlTable.GetSample()

for row in test.Tables.Table2.Rows do
    printfn $"%s{row.Package} %s{row.Item}"