[Tip]dotNet project Add Reference

本文探讨了如何在.NET项目中添加来自本地目录的依赖模块,并保持项目的成功编译和部署。文中详细介绍了.NET运行时如何定位程序集,以及在不同平台下加载程序集的过程。同时给出了正确的引用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

We are so used to adding dotNet module references and project references, but rarely meet situations requiring adding other dotnet module directly. The Other dotnet module consists of 3 kinds:
    --precondition of installing my app. Like installing a acad addin app requires ACAD to be there as precondition. So refering to ACAD modules requires NOT adding
    the referred dlls adding into my app installer.
    --my app's 3rd party library. My app installer should make sure deploying required modules into user machine.

    --my app's component modules. NP here..

The question is: how to add a local-directory dependent dotNet module as reference and can still keep this project compiled successfully and deployed successfully?

Well, to answer it, we need to understand:

1. how dotnet CLR locates a assembly at runtime.

Firstly, it totally has no relationship with the local directory you specified in the VS project for adding reference. In my understanding, building the dotnet project will embed its dependent assmemblies into its manifest file while it doesn't contain where to find it and its platform (x32/x64) info. When at runtime, CLR will follow its locating rule and its platform version to find and load required assembly. I didn't read enough. so still put articles below for further reading:

http://msdn.microsoft.com/en-us/library/yx7xezcf.aspx 

http://support.microsoft.com/kb/837908#appliesto

http://msdn.microsoft.com/en-us/library/4191fzwb.aspx 

and two books:

a. Don Box's "Essential dotNet"

b. and Steven Pratschner's "Customzing the Microsoft .NET Framework CLR"

which has detailed description on .NET runtime's assembly binding andloading.

2. what about CLR's handling of x32 / x64 in loading assembly for dotnet application?

NOT ready more yet.. need further reading.

3. What's the correct way to add reference about a separate dotnet dll living in a local directory?

You need to add $(XX)---environment variable into project file manually instead of using VS to specify the local path so that on the build machine and other's machine, as we share the same set of environment variables, so in this way, the project building can be no problem; while it has no relationship with the deployment of the refered dotnet modules.

Refer to: http://blog.youkuaiyun.com/ham235/archive/2010/08/09/5800073.aspx

Coming here, it's resolved.

转载于:https://www.cnblogs.com/taoxu0903/archive/2010/09/30/1839543.html

<think>我们正在处理一个关于在.NET项目中添加Microsoft.Data.SqlClient包的问题。用户询问如何使用dotnetCLI添加这个包。根据引用[1]中的信息,我们知道有三种方式安装包,其中第三种就是使用命令行界面(CLI)的命令:dotnetadd packageSystem.Data.SqlClient。但是注意,用户要求的是Microsoft.Data.SqlClient包,而不是System.Data.SqlClient。不过,安装方式是一样的,只需替换包名即可。因此,我们可以给出如下步骤:1.打开命令行(例如:命令提示符、PowerShell或终端)。2.导航到项目目录(即.csproj文件所在的目录)。3.运行命令:dotnetadd packageMicrosoft.Data.SqlClient另外,引用[3]中提到了使用dotnetbuild和dotnetpack命令,但这里我们只需要添加包,所以不需要这些步骤。注意:引用[1]中提到的System.Data.SqlClient的安装命令是类似的,所以我们只需要将包名改为Microsoft.Data.SqlClient即可。因此,回答如下:</think>要在.NET项目中使用dotnet CLI添加`Microsoft.Data.SqlClient`包,请按以下步骤操作: ### 添加步骤 1. **打开命令行工具** 启动终端(Windows:CMD/PowerShell;macOS/Linux:终端) 2. **导航到项目目录** ```bash cd 你的项目路径 ``` 例如: ```bash cd C:\Projects\MyApp ``` 3. **执行安装命令** ```bash dotnet add package Microsoft.Data.SqlClient ``` ✅ 此命令将: - 自动解析最新稳定版本 - 修改项目文件(`.csproj`)添加包引用 - 恢复依赖项(相当于`dotnet restore`) 4. **验证安装** 检查项目文件`.csproj`中是否出现: ```xml <PackageReference Include="Microsoft.Data.SqlClient" Version="x.x.x" /> ``` ### 关键说明 - **版本选择**:需指定版本时添加`--version`参数: ```bash dotnet add package Microsoft.Data.SqlClient --version 5.1.0 ``` - **NuGet源**:使用私有源时添加`--source`参数: ```bash dotnet add package Microsoft.Data.SqlClient --source https://自定义源地址 ``` - **项目指定**:多项目解决方案中指定项目: ```bash dotnet add YourProjectName.csproj package Microsoft.Data.SqlClient ``` ### 性能优化建议 使用该包时建议结合`SqlDataReader`而非`DataTable`处理大数据查询,避免内存溢出(参考引用[2]的优化方案): ```csharp using (var reader = command.ExecuteReader()) { while (reader.Read()) { // 流式处理数据 } } ``` > ⚠️ 注意:若遇到包缓存问题,可运行`dotnet nuget locals all --clear`清理本地缓存
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值