在C#6.0中引入了"when"关键字,现在您可以在catch块中过滤异常.但是这不同于catch块中的if语句吗?如果是这样,是不是只是语法糖或我错过了什么?
例如,带有"when"关键字的try catch块:
try { … }
catch (WebException ex) when ex.Status == WebExceptionStatus.Timeout {
//do something
}
catch (WebException ex) when ex.Status== WebExceptionStatus.SendFailure {
//do something
}
catch (Exception caught) {…}
Run Code Online (Sandbox Code Playgroud)
要么
try { … }
catch (WebException ex) {
if(ex.Status == WebExceptionStatus.Timeout) {
//do something
}
}
catch (WebException ex) {
if(ex.Status == WebExceptionStatus.SendFailure) {
//do something
}
}
catch (Exception caught) {…}
Run Code Online (Sandbox Code Playgroud) 我正在寻找Microsoft Fakes in .NET Core的替代方案.我知道.NET Core不再支持它.我只是不明白为什么不,我认为这在某些情况下是一个很好的解决方案.
我的问题是我想模拟DateTime.Now.以前您可以使用以下代码执行此操作:
System.Fakes.ShimDateTime.NowGet = () =>
{
return new DateTime(2000, 1, 1);
};
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅Microsoft文档中的链接:https://docs.microsoft.com/en-us/visualstudio/test/using-shims-to-isolate-your-application-from-other-assemblies -用于单元测试?视图= VS-2017
现在我通过为DateTime创建一个包装器来解决它,如下所示:
/// <summary>
/// Used for getting DateTime.Now(), time is changeable for unit testing
/// </summary>
public static class SystemTime
{
/// <summary>
/// Normally this is a pass-through to DateTime.Now, but it can be
/// overridden with SetDateTime( .. ) for testing or debugging.
/// </summary>
public static Func<DateTime> Now = () => DateTime.Now;
/// …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个使用多个私有 API 的公共 API(无法从外部访问)。已经为私有 API 编写了业务验证,我不想为公共 API 重新编写它们。但我确实希望 swagger 文档是相同的。
这就是为什么我想知道是否可以将属性标记为强制属性,而不使用 ASP.NET 的 Required 属性。但是 swagger 文档表明它是强制性的。这可能吗?
是否可以使用 Angular CLI (7.0.5) 为开发和生产提供不同的资产?
对于生产,我想要资产:
"assets": [
"projects/example/src/favicon.ico"
]
Run Code Online (Sandbox Code Playgroud)
对于开发,我想要资产:
"assets": [
"projects/example/src/favicon.ico",
"projects/example/src/assets/development.css"
]
Run Code Online (Sandbox Code Playgroud)
先感谢您。