C# netstandard1.x和winforms/wpf/windows-无法加载文件或程序集System.Runtime,版本=4.1.0.0
我已经设置了一个可重用的基类库,我想用于web、桌面和移动设备。它具有基本的功能,例如字符串、日期时间操作等。。因为我讨厌写两次代码 VS2015告诉我最好使用新的“.NET标准”平台,所以我这样做了,它给了我这个project.jsonC# netstandard1.x和winforms/wpf/windows-无法加载文件或程序集System.Runtime,版本=4.1.0.0,c#,.net,winforms,asp.net-core,.net-standard-1.5,C#,.net,Winforms,Asp.net Core,.net Standard 1.5,我已经设置了一个可重用的基类库,我想用于web、桌面和移动设备。它具有基本的功能,例如字符串、日期时间操作等。。因为我讨厌写两次代码 VS2015告诉我最好使用新的“.NET标准”平台,所以我这样做了,它给了我这个project.json { "supports": {}, "dependencies": { "Microsoft.NETCore.Portable.Compatibility": "1.0.1", "NETStandard.Library": "1.6.0
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.5": {}
}
}
用xunit运行单元测试很顺利。直到我决定在一个以net462为目标的WinForms应用程序中使用它(做一个参考)(是的,有些人仍然有需要WinForms的客户端)。应用程序编译为a-ok,没有任何警告或错误。但当我运行应用程序时,出现了以下错误:
An exception of type 'System.IO.FileNotFoundException' occurred in DelegateIT.Core.Examples.Wizard.WinFormsClient.exe but was not handled in user code
Additional information: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
我尝试了不同的选择,但没有成功:
- 将
添加到框架部分“net462”:{}
- 正在尝试将
中的“bin”生成目标添加到frameworks部分,但未输出平台的自定义dll“net462”:{“bin”:{“assembly”:“external\\TheLibraryName.dll”、“pdb”:“external\\TheLibraryName.pdb”}}
编辑/补遗 我在Wpf和WinForms客户端中有以下代码:
foreach (var line in csvLines) {
var splittedLine = line.Split(';');
splittedLines.Add(splittedLine.Select(s => CleanString(s)).ToList());
}
当我省略了像这样的CleanString
方法时
foreach (var line in csvLines) {
var splittedLine = line.Split(';');
splittedLines.Add(splittedLine.Select(s => /*CleanString(*/s/*)*/).ToList());
}
然后错误消失了,Wpf客户端和WinForms客户端工作正常。看起来像是System.Linq
或至少是System.Linq.Expressions
导致了此错误。但深入挖掘后,我确保编辑CleanString
方法,并删除了扩展方法“RemoveMultipleWhiteSpaces
”
作为引用,这是CleanString方法和引用扩展方法
private string CleanString(string @string) {
return @string
.Trim()
.RemoveMultipleWhitespaces();
}
public static string RemoveMultipleWhitespaces(this string @string) {
if (@string == null) {
return @string;
}
var cleanedString = Regex.Replace(@string, @"(\s)\s+", "$1");
return cleanedString.Trim();
}
因此,总而言之,当我在CleanString中使用以下代码时,它被编译并工作了。所以我不确定,这个System.Linq或其他东西是否不受支持
private string CleanString(string @string) {
return @string
.Trim()
/*.RemoveMultipleWhitespaces()*/;
}
编辑/添加索引2: 在project.json中尝试了这一点,但没有成功。还尝试添加“System.Runtime”:“4.1.0”作为依赖项,而不是运气
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"System.Linq": "4.1.0",
"System.Linq.Expressions": "4.1.0",
"System.Text.RegularExpressions": "4.1.0"
},
"frameworks": {
"net462": {},
"netstandard1.5": {}
}
}
我也有同样的问题(完全相同)。
我只是在本地nuget包中找到了所需的dll(在本例中是
C:\Users\Soren\.nuget\packages\System.Reflection\4.1.0\lib\net462\System.Reflection.dll),并复制到我的项目输出中,异常就消失了。
但这不是解决方案,问题暂时解决了。您不必手动复制.NET标准库的依赖项
微软最终承认这是一个问题,并期待在NuGet 4.0.1版中发布,这是VS 2017发布后对NuGet 4的第一次更新
现在最干净的解决方法是将PackageReference
添加到遗留项目中。但是,MS将在RTM之后忽略此属性,因此另一种解决方法是
另一个解决方法是将.NET标准库像中一样打包到NuGet包中,但这不是最简单的方法