Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a local csproj from a C# script

Tags:

c#

.net-core

csx

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.

like image 438
RAnderson Avatar asked Dec 27 '18 15:12

RAnderson


People also ask

How do I reference a NuGet package?

After you install a NuGet package, you can then make a reference to it in your code with the using <namespace> statement, where <namespace> is the name of package you're using. After you've made a reference, you can then call the package through its API.

How do I add a reference to another project in C?

Add a reference In Solution Explorer, right-click the References or Dependencies node, and then choose either Add Project Reference, Add Shared Project Reference, or Add COM Reference from the context menu. (You can right-click the project node and select Add from the fly-out menu to choose from these options, too.)


1 Answers

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;
like image 102
Barr J Avatar answered Oct 19 '22 19:10

Barr J