Referencing a local csproj from a C# script

1.9k Views Asked by At

I've been experimenting with C# script (*.csx) to do some data processing with the results of service calls to some internal APIs. It's been pretty smooth thus far, but I'm now trying to call some methods from a C# project I have access to. The C# project doesn't have a published nuget package, which means referencing the dll directly using #r. However in order to access the necessary functions, I've found I also need to add references to the top of my script for any of that projects dependencies and any nuget packages that dependency uses.

#r "PATH_TO_SOLUTION\Project\bin\Debug\netstandard2.0\binary.dll"
#r "PATH_TO_SOLUTION\ProjectDependency\bin\Debug\netstandard2.0\dependent.dll"
#r "nuget: Some.Dependency, 11.0.2"
#r "nuget: Some.Other.Dependency, 10.0.2"
#r "nuget: Some.Third.Dependency, 9.0.2"
using Project;

I'm new to the C# scripting world, and didn't find anything directly addressing this question so hopefully I'm not asking something super obvious here.

Some projects depend on a pretty large number of nuget packages, is there a way to reference a csproj from a C# script that doesn't require explicitly referencing all of the dependencies for the project?

Let me know if there's any additional information I can provide.

1

There are 1 best solutions below

1
On BEST ANSWER

Please note Essential .NET - C# Scripting at Microsoft Docs:

you can take the entire listing and save it as a CSX file, then “import” or “inline” the file into the C# REPL window using #load Spell.csx. The #load directive allows you to include additional script files as if all the #load files were included in the same “project” or “compilation.” Placing code into a separate C# script file enables a type of file refactoring and, more important, the ability to persist the C# script over time.

Take all of the reference, save it as a CSX file and give it a shot.

for example:

//Load scripts
#load "Script.csx"  
using Project;