我在具有数据传输对象 (DTO) 类的项目中激活了此功能,如下所示:
public class Connection
{
public string ServiceUrl { get; set; }
public string? UserName { get; set; }
public string? Password { get; set; }
//... others
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
CS8618:不可为空的属性“ServiceUrl”未初始化。考虑将该属性声明为可为空的。
这是一个 DTO 类,所以我没有初始化属性。这将是初始化类的代码的责任,以确保属性不为空。
例如,调用者可以这样做:
var connection = new Connection
{
ServiceUrl=some_value,
//...
}
Run Code Online (Sandbox Code Playgroud)
我的问题:当启用 C#8 的可空性上下文时,如何处理 DTO 类中的此类错误?
我尝试DataTable使用数据库SQL Server 2012中的Newtonsoft.Json版本"Newtonsoft.Json.10.0.3" 将对象序列化为Json.
该表有一个类型为'geography'的列,其中包含类型的实例SqlGeography.
用于生成json的代码:
public string SerializeToJson()
{
var connstring1 ="Data Source=server1;Initial Catalog=database1;user=xxx;password=yyy";
var sql = "SELECT * FROM table_1 "; //table_1 has a column of type geography
using (var c1 = new SqlConnection(connstring1))
{
c1.Open();
var da = new SqlDataAdapter()
{
SelectCommand = new SqlCommand(sql, c1)
};
DataSet ds1 = new DataSet("table");
da.Fill(ds1, "table");
var dt = ds1.Tables[0];
//serialize to Json
try
{
var options = new JsonSerializerSettings
{
Formatting = Formatting.None
}; …Run Code Online (Sandbox Code Playgroud) 开发环境:
Microsoft.NETCore.App 5.0.0-preview.5.20278.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Tools > Options > Environment > Preview Features and select the Use the preview Windows Forms designer for .NET Core apps option. > re-start vs
Run Code Online (Sandbox Code Playgroud)
我按照此答案中的说明进行操作
我在 .net5 中创建了一个演示控制台项目
Tools > Options > Environment > Preview Features and select the Use the preview Windows Forms designer for .NET Core apps option. > re-start vs
Run Code Online (Sandbox Code Playgroud)
然后使用 dotnet cli 构建和运行就可以了
dotnet build …Run Code Online (Sandbox Code Playgroud) 我知道自从msbuild 15(vs 2017)发布以来,NuGet现已完全集成到MSBuild中.
我有一个nuspec文件,包含定义包属性的变量,如:
<metadata>
<id>$id$</id>
<version>$version$</version>
<authors>$authors$</authors>
...
</metadata>
Run Code Online (Sandbox Code Playgroud)
nuspec文件位于项目的同一文件夹中.
使用nuget工具创建包时,它工作正常.
nuget pack
Run Code Online (Sandbox Code Playgroud)
使用msbuild v15时,会引发异常.
运行命令:
msbuild -version
Run Code Online (Sandbox Code Playgroud)
适用于.NET Framework 15.8.168.64424的Microsoft(R)Build Engine版本15.8.168 + ga8fba1ebd7
msbuild /t:pack /p:configuration=release /p:NuspecFile=mylib.nuspec
Run Code Online (Sandbox Code Playgroud)
提出异常:
C:\ Program Files\dotnet\sdk\2.1.402\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(199,5):错误:值不能为null或为空串.
奇怪的是,dotnet sdk版本2.1.402引发了异常.
我尝试使用vs2017安装msbuild及其路径,并且它也引发了相同的异常.
当我用其值替换变量时,msbuild工作正常.
这个问题
这是msbuild版本15.8.168.64424中的错误还是我错过了什么?
换句话说,msbuild可以支持使用包的元数据变量吗?
我遵循了这个答案:How to enable C# 9.0-preview to enable C# 9。
我安装了 .NET 5 preview 5,其中包括新的 C# 9。
Microsoft.NETCore.App 5.0.0-preview.5.20278.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
我尝试使用C# 9:
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
Run Code Online (Sandbox Code Playgroud)
项目设置如下:
<LangVersion>9</LangVersion>
Run Code Online (Sandbox Code Playgroud)
但我收到一个编译错误:
错误 CS1617 /langversion 的无效选项“9”。使用 '/langversion:?' 列出支持的值。
运行时我没有在列表中找到 9:
csc -langversion:?
Run Code Online (Sandbox Code Playgroud)
名单是:
<LangVersion>9</LangVersion>
Run Code Online (Sandbox Code Playgroud)
<LangVersion>preview</LangVersion>
Run Code Online (Sandbox Code Playgroud)
但我收到编译错误。
Program.cs(26,40):错误 CS1014:需要获取或设置访问器
在 .NET 5 预览版 5 中使用 C# 9 时我错过了什么?
我使用 msbuild v15.8.168.64424(vs 2017.8)命令行为控制台应用程序构建 nuget 包。我使用ILMerge合并所有依赖项,因此我只得到一个没有依赖项的exe文件。
运行命令时
msbuild project1.csproj /t:build /p:configuration=release /p:IsTool=true
Run Code Online (Sandbox Code Playgroud)
我得到了 nuget 包,但是在检查它时,我发现了dependencies section.
<dependencies>
<group targetFramework=".NETFramework4.5">
<dependency id="lib1" version="1.3.0" exclude="Build,Analyzers" />
<dependency id="lib2" version="2.3.0" exclude="Build,Analyzers" />
</group>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
我希望“依赖项”部分为空(因为我合并了所有依赖项)。
我可以使用 nuspec 文件生成没有依赖项部分的包,但缺点是手动传递 csproj 中包含的所有元数据。我在包目标输入中没有找到依赖项的属性
问题:
如何从生成的 nuget 包中包含的 nuspec 文件中删除依赖项部分?
我使用的是VS2017新的SDK风格项目
在 dotnet 中,我可以使用以下命令进行打包:
dotnet pack project.csproj --no-dependencies --no-restore --output c:\packages -p:TargetId=abc -p:configuration=release
Run Code Online (Sandbox Code Playgroud)
在 msbuild 中,我可以使用以下命令进行打包:
msbuild project.csproj /t:pack -p:TargetId=abc -p:configuration=release
Run Code Online (Sandbox Code Playgroud)
如何--no-dependencies --no-restore --output 使用 msbuild设置 dotnet 的选项
我有2个不同的第三方程序集,它们为业务服务提供相同的API并使用相同的类名(~40个类/类型/扩展名)但位于不同的程序集中:
Company.Assemply.V1
Company.Assemply.V2
Run Code Online (Sandbox Code Playgroud)
我引用了项目中的两个程序集.
这些程序集没有通用接口,第三方也无法提供通用接口
因此,c#编译器将两个程序集中的每个类型视为不同的类型.
我想Myservice 为每个程序集实现一个类来支持V1/V2版本.
我使用以下代码来实现 Myservice.V1.Myclass
//#define V1
#if V1
using Company.Assemply.V1;
#else
using Company.Assemply.V2;
#endif
#if V1
namespace Myservice.V1
#else
namespace Myservice.V2
#endif
{
//my implementation that use all classes /types in any v1/v2 assembly
class MyClass {.... }
}
Run Code Online (Sandbox Code Playgroud)
然后我将相同的代码复制并粘贴到其他c#文件 MyClassV2.cs (大约400行)中,以获取 Myservice.V2.Myclass和取消注释编译器标志#define V1
我不能使用泛型
MyClass <T> where T:??
Run Code Online (Sandbox Code Playgroud)
因为T没有通用接口
这两个班级工作正常.
问题是在维护v1时,我必须将代码复制/粘贴到另一个文件中,MyClassV2.cs并取消注释编译器标志#define V1以支持V2.
是否有更好的方法/合适的设计模式/重构技术可以解决这样的问题.我想使用/维护一个代码库,避免复制/粘贴其他类版本.
给我一个重构上面代码的例子.
c# ×8
.net ×2
.net-5 ×2
msbuild ×2
nuget ×2
nuspec ×2
.net-core ×1
c#-8.0 ×1
c#-9.0 ×1
dotnet-sdk ×1
dto ×1
generics ×1
geolocation ×1
json ×1
json.net ×1
msbuild-15 ×1
orm ×1
refactoring ×1
sql-server ×1