自从我开始使用.NET以来,这一直是我的一个宠儿,但我很好奇,以防我错过了什么.我的代码片段无法编译(请原谅示例的强制性质),因为(根据编译器)缺少return语句:
public enum Decision { Yes, No}
public class Test
{
public string GetDecision(Decision decision)
{
switch (decision)
{
case Decision.Yes:
return "Yes, that's my decision";
case Decision.No:
return "No, that's my decision";
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我知道我可以简单地放置一个默认语句来摆脱编译器警告,但在我看来,不仅是冗余代码,它的危险代码.如果枚举在另一个文件中并且另一个开发人员出现并将Maybe添加到我的枚举中,它将由我的默认子句处理,该子句对Maybe s 一无所知,并且我们很可能会引入逻辑错误.
然而,如果编译器允许我使用上面的代码,那么它可以识别我们有问题,因为我的case语句将不再覆盖枚举中的所有值.当然听起来对我来说更安全.
这对我来说根本就是错误的,我想知道它是否只是我缺少的东西,还是我们在switch语句中使用枚举时必须非常小心?
编辑: 我知道我可以在默认情况下引发异常或在交换机外添加一个返回,但这仍然是根本不能解决编译错误,这不应该是错误.
关于enum真的只是一个int,这是.NET肮脏的小秘密之一,真的非常令人尴尬.让我声明一个有限数量的可能性的枚举,并给我一个汇编:
Decision fred = (Decision)123;
Run Code Online (Sandbox Code Playgroud)
然后如果有人尝试这样的事情,则抛出异常:
int foo = 123;
Decision fred = (Decision)foo;
Run Code Online (Sandbox Code Playgroud)
编辑2:
有些人对enum在不同的程序集中会发生什么以及如何导致问题发表评论.我的观点是,这是我认为应该发生的行为.如果我更改方法签名,这将导致问题,我的前提是更改枚举应该是相同的.我的印象是很多人都不认为我理解.NET中的枚举.我只是认为这种行为是错误的,我希望有人可能知道一些非常模糊的功能会改变我对.NET枚举的看法.
我选择将其视为一个问题,因为它在原帖的评论中产生了如此多的争论.
很有趣的是,很多SO(开发人员)的人都没有获得本地化.以下是我应该如何运作的看法:
在我查看用户的文化偏好时,我所看到的所有浏览器(以及那里的.NET开发人员)都采用以下格式:language-Culture.
所以我们有:
有关.NET框架支持的完整列表,请参阅MSDN.
当我访问一个网站时,它知道我需要来自英语的英语,它知道我对它倾向于英国感兴趣(数字格式化,日期格式化).因此,当我去google.com并且它带我去google.de(因为我的IP地址),如果google.de用英语向我显示所有内容但完全错误,因为google.de是德语的.我几乎无法控制我的IP地址,但可以完全控制我的语言和文化设置.如果你有兴趣,微软的新搜索引擎(bing.com)会妥善处理事情.让我们希望微软可以学习如何进行搜索以及谷歌或谷歌可以学习本地化以及微软;)
MSDN在这里有另一篇好文章以获取更多信息
那么您对网站应如何处理本地化的建议是什么?
language-agnostic globalization localization user-experience
我正在使用一个实用程序来使用模板来生成针对Postgres数据库的数据访问层.作为其中的一部分,我试图动态发现存储过程的返回类型.在返回单个标准类型的简单情况下这很容易,但是当它返回用户定义的类型时我很挣扎.
如果有人能提供必要的SQL来返回这些数据,我将不胜感激.
谢谢马克
我很欣赏我到目前为止的答案,这些答案有效地归结为以下SQL
SELECT p.proname, t.typname, p,proretset
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
INNER JOIN pg_type t ON p.prorettype = t.oid
WHERE n.nspname = 'public'
--and proname = 'foo'
ORDER BY proname;
Run Code Online (Sandbox Code Playgroud)
这将返回返回类型的名称.但是,当它返回用户定义的类型时,我仍然需要将类型分解为构成它的属性.
在函数返回记录的情况下,除了调用函数并检查其返回值之外,我认为没有任何方法可以发现它的返回结构.
有没有办法使用XAML为Windows Phone 7格式化日期?
如果尝试使用:
<TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" />
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
在'Binding'类型中找不到属性'StringFormat'
我一直在使用 Swagger 关注 ASP.NET Web API 帮助页面的帮助,但在 Chrome 中收到以下错误:
它在 IE10 中工作,但似乎没有接受更改。
我发现以下条目Can't read swagger JSON from http://localhost:9000/api-docs/不幸的是,它指的是在 grunt 下更改它,而现在它在 gulp 下工作。
我还尝试更改 ASP.NET core 中的 CORS 设置:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
// Inject an implementation of ISwaggerProvider with defaulted settings applied
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Status API",
Description = "A simple example ASP.NET Core Web API!",
TermsOfService = "None", …Run Code Online (Sandbox Code Playgroud) 我正在尝试在云 Firestore 数据库中插入表单数据。下面是我的 x.component.ts 文件,我在其中编写的构造函数出错
private firestore: AngularFireStore
import { Component, OnInit } from '@angular/core';
import { GroupService } from '../shared/group.service';
import { NgForm } from '@angular/forms';
// import { NullTemplateVisitor } from '@angular/compiler';
import { AngularFirestore } from '@angular/fire/firestore';
// import { AngularFireModule } from 'angularfire2';
// import { AngularFirestoreModule } from 'angularfire2/firestore';
@Component({
selector: 'app-group',
templateUrl: './group.component.html',
styleUrls: ['./group.component.css']
})
export class GroupComponent implements OnInit {
constructor(private groupService: GroupService, private firestore: AngularFirestore) { }
ngOnInit() {
this.resetForm();
} …Run Code Online (Sandbox Code Playgroud) 在我的组件中,我将一组需要执行的操作组合在一起。它们执行的顺序并不重要,但如果成功,我想在最后显示。每个操作都会导致调用 WebAPI。(我不明白,这将是很多更好,如果操作都是在一个呼叫传递然而,这是不是一种选择。
不幸的是,调用的次数太多了,它有效地对 API 进行了 DOS 处理。我试图延迟管道,但也许我没有把它放在正确的地方
const results: any[] = [];
this.bigArray.forEach(item =>
results.push(
this.aServiceWhichWillCallAPostMethod.doAnUpdate(item)
)
);
forkJoin(results).subscribe(
data => {
Console.log('Yeah');
},
error => {
Console.log('Oops');
},
() => {
}
);
Run Code Online (Sandbox Code Playgroud)
我试过在这里添加延迟
this.aServiceWhichWillCallAPostMethod.doAnUpdate(item).pipe(delay(5000))
Run Code Online (Sandbox Code Playgroud)
和这里
forkJoin(results).subscribe
Run Code Online (Sandbox Code Playgroud)
但没有运气
angular ×2
.net ×1
.net-core ×1
angular7 ×1
asp.net-core ×1
c# ×1
enums ×1
firebase ×1
localization ×1
postgresql ×1
rxjs ×1
swagger-ui ×1
typescript ×1
xaml ×1