从 C# 脚本引用本地 csproj

Referencing a local csproj from a C# script

我一直在尝试使用 C# 脚本 (*.csx) 对某些内部 API 的服务调用结果进行一些数据处理。到目前为止一切顺利,但我现在正尝试从我有权访问的 C# 项目中调用一些方法。 C#项目没有发布的nuget包,这意味着直接使用#r引用dll。但是,为了访问必要的功能,我发现我还需要为任何项目依赖项和依赖项使用的任何 nuget 包添加对脚本顶部的引用。

#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;

我是 C# 脚本世界的新手,没有找到任何直接解决这个问题的东西,所以希望我在这里问的不是非常明显的问题。

有些项目依赖于大量的 nuget 包,有没有一种方法可以从 C# 脚本中引用 csproj,而不需要显式引用项目的所有依赖项?

如果我可以提供任何其他信息,请告诉我。

请注意 Essential .NET - C# Scripting Microsoft 文档:

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.

获取所有参考资料,将其保存为 CSX 文件并试一试。

例如:

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