Asp.net C#类库引用时如何指定正确版本的依赖DLL?
我创建了名为sharepointClassSample的类库,它依赖版本为16.1.0.0的Microsoft.Sharepoint.Client.dll及其他组件。编译后将生成文件移至myLibrary文件夹,随后在另一个项目中引用该文件夹下的sharepointClassSample.dll,编译项目后,sharepointClassSample.dll所需的所有DLL都被复制到项目的bin/debug文件夹,但其中的Microsoft.Sharepoint.Client.dll版本为15.0.0.0。请问在类库项目中是否有设置可以指定引用时复制正确版本的DLL?
解决办法
1. 类库项目中配置引用属性
在sharepointClassSample类库项目里,找到Microsoft.SharePoint.Client.dll的引用,右键选择属性:
- 将复制本地设为
True,确保类库编译时会把16.1.0.0版本的DLL输出到自身编译目录。 - 把特定版本设为
True,强制项目仅使用指定版本的引用,避免自动降级匹配低版本。
2. 编辑类库项目文件锁定版本
如果属性设置不生效,直接编辑类库的.csproj文件,找到对应引用节点,明确版本、路径和复制规则:
<Reference Include="Microsoft.SharePoint.Client, Version=16.1.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL"> <HintPath>你的本地路径\Microsoft.SharePoint.Client.dll</HintPath> <SpecificVersion>True</SpecificVersion> <Private>True</Private> <!-- 对应「复制本地」为True --> </Reference>
3. 排查引用项目的依赖冲突
如果引用sharepointClassSample.dll的项目本身也引用了15.0.0.0版本的Microsoft.SharePoint.Client.dll,会导致版本覆盖:
- 移除引用项目中对15.0.0.0版本的直接引用,仅保留通过
sharepointClassSample的间接引用。 - 若无法移除,在引用项目的
app.config或web.config中添加绑定重定向,强制使用16.1.0.0版本:
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.SharePoint.Client" publicKeyToken="71e9bce111e9429c" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-16.1.0.0" newVersion="16.1.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
4. 完整复制类库编译输出
编译sharepointClassSample后,将其整个编译目录(如bin/Debug)的所有文件复制到myLibrary,不要仅复制sharepointClassSample.dll——确保依赖的16.1.0.0版本DLL一并被复制,避免后续引用时缺失正确版本。
内容的提问来源于stack exchange,提问作者El Bayames
相关产品推荐
相关产品推荐

