小编bro*_*and的帖子

对象投射是一种好习惯吗?

面向对象的编程语言,例如java,c#,...支持对象类型转换.例如,这在java中完全有效:

URL url = new URL("url");    
URLConnection conn = url.openConnection();

if( !conn instanceof HttpURLConnection )
  throw new Exception("not http request");

HttpURLConnection con = (HttpURLConnection) conn;   
Run Code Online (Sandbox Code Playgroud)

或者我试过的另一个基本例子:

public class Base
{
  public void base(){}
}

public class Derived extends Base
{
  public void derived(){}
}

Base b = new Derived();
b.base();
Run Code Online (Sandbox Code Playgroud)

派生类具有基类具有的所有方法,以及更多.没有理由不能通过调用派生类构造函数来创建基类.

我还看到了这个链接http://www.volantec.biz/castingObjects.htm,它解释了对象类型转换如何在内部工作.到目前为止,好的.

但是为什么第一个例子不使用HttpURLConnection con = new HttpURLConnection("url address")(我知道HttpURLConnection是一个抽象类).它看起来更清晰,更简单.另一方面,当您处理接口时,对象类型转换就派上用场了.另一个例子是List<Object>list,我有时会在某些类中看到它.这意味着您可以在此列表中保存所有可能的类.之后,如果您知道它是什么类型,您可以将其转换为原始版本.那岂不是更清楚,只保存特定的类列出,即List<String>,List<MyClass>.List<Object>那么使用好的设计实践呢?

design-patterns casting

9
推荐指数
1
解决办法
1999
查看次数

如何在 WPF 中折叠星形大小的网格列?

Width通过设置为 来使三列具有相同的宽度Auto

<Grid x:Name="myGrid">
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>

  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>

  <Label Grid.Row="0" Grid.Column="0">One</Label>
  <Label Grid.Row="0" Grid.Column="1" x:Name="label1">Two</Label>
  <Label Grid.Row="0" Grid.Column="2">Three</Label>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我想实现的是,如果中间列折叠或隐藏,则其他两列占用剩余空间并获得相同的宽度。

如果我只是因为宽度=“*”而将可见性设置为折叠或隐藏,则其他两列宽度保持不变。

<Label Grid.Row="0" Grid.Column="1" Visibility="Collapsed">Two</Label>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我通过以编程方式将第二列宽度设置为自动来实现所需的功能,但我正在寻找其他一些解决方案(最好是 xaml 方式一)。

private void Button_Click(object sender, RoutedEventArgs e)
{
  this.myGrid.ColumnDefinitions[1].Width = GridLength.Auto;
  this.label1.Visibility = Visibility.Collapsed;
}
Run Code Online (Sandbox Code Playgroud)

wpf xaml wpf-grid

9
推荐指数
2
解决办法
3539
查看次数

强制 linq sum 返回 null

我想答案很简单:

List<int?> intList = new List<int?>(){null, null, null};
int? sum = list.Sum();
int sum2 = list.Sum(a => a.HasValue ? a.Value : 0);
int sum3 = list.Sum().Value;
Run Code Online (Sandbox Code Playgroud)

Sum 总是返回 0,为什么根本需要 nullable?有没有办法强制 linq sum 返回 null?我在这里缺少什么?

c# linq

7
推荐指数
2
解决办法
2666
查看次数

如果在数据库中找不到记录,则返回 (RecordNotFound) 异常或 null?

在处理数据库中未找到的记录时,我不确定什么是首选方法。编写返回 null 的 Find 方法或返回的 Get 方法更好RecordNotFoundException吗?

[AuthenticateFilter(UsernameAndSecretKey)]
[Route("api/v1/activities/emails/{id}")]
[HttpGet]
public IHttpActionResult GetEmailActivity(int id)
{
  try
  {
    // business logic service, could use only db service but this way we can do unit tests (just fill bl service method with fake objects)
    var service = new EmailActivityBlService();

    // 1. use Find method which returns null in case record with provided id does not exist in db
    var model = service.FindActivity(id);
    if( model != null )
      return Ok(model);
    return …
Run Code Online (Sandbox Code Playgroud)

c# exception-handling

6
推荐指数
2
解决办法
5865
查看次数

如何忽略 TypeScript 错误 TS2339 - 类型上不存在属性

我正在使用 Angular 8(在 Visual Studio 2019 中启动了一个项目)并拥有fabric.init.ts包含以下内容的文件:

import 'fabric';
import * as fabric from 'fabric/fabric-impl';

// (TS) Property 'DPI' does not exist on type 'typeof import(..@types/fabric/fabric-impl)'
fabric.DPI = 213;
....
Run Code Online (Sandbox Code Playgroud)

接下来就可以了,因为它符合 d.ts 定义:

// Override fabric to meet ourrequirements
fabric.util.object.extend(fabric.Object.prototype, {
  centeredRotation: true,
Run Code Online (Sandbox Code Playgroud)

如何忽略DPITS2339 错误。拥有自动补全功能真是太棒了,特别是对于学习结构而言。

我的package.json:

"private": true,
"dependencies": {
  "@angular/core": "8.2.14",
  ....
  "@angular/router": "8.2.14",
  "@types/fabric": "3.5.1",
  "fabric": "3.5.1",
  ...
},
Run Code Online (Sandbox Code Playgroud)

编辑

现在我通过添加这一行解决了这个问题:

// @ts-ignore
fabric.DPI = 213;
Run Code Online (Sandbox Code Playgroud)

我知道这不是最好的解决方案,我更喜欢@wentjun 的解决方案。

通过更改此:我import * as fabric from …

fabricjs typescript angular

6
推荐指数
1
解决办法
1万
查看次数

如何等待 ASP.NET Core 6.0 Runtime 的静默安装

我想等到 dotnet 托管捆绑包安装完成。通常会有某种返回代码:

  • 0 -> 好的
  • <1 -> 错误
PS D:\app> .\dotnet-hosting-6.0.4-win.exe /install /passive /norestart
Run Code Online (Sandbox Code Playgroud)

Msi不存在,还有其他办法吗?

在此输入图像描述

.net installation .net-core

6
推荐指数
1
解决办法
2388
查看次数

FileHelper转义分隔符

我正在使用FileHelper 2.0来解析我的csv数据.filehelper有没有可以正确处理转义分隔符的选项?它可以将字段标识为数据而不是分隔符.

我们的csv格式:使用\来转义逗号(,)

示例数据:

姓名,姓氏

尼科\,文件,opeka

当前代码:

[DelimitedRecord(",")] 
public class contactTemplate
{
  public string firstName;
  public string lastName;
}
Run Code Online (Sandbox Code Playgroud)

我如何获得firstName = nico,le和lastName = opeka.FileHelpers按逗号分割,现在返回:

firstName - > nico \

lastName - >,le,opeka

c# csv filehelpers

5
推荐指数
1
解决办法
3738
查看次数

正则表达式组恰好匹配n次

我必须验证下一个字符串格式:

text-text-id-text
Run Code Online (Sandbox Code Playgroud)

分隔符是字符' - '.第三列必须始终为id.我写了下一个regex(在python中)验证字符串:

import re

s = 'col1-col2-col3-id' # any additional text at the end
                        # is allowed e.g. -col4-col5
print re.match('^(.*-){3}id(-.*)?$', s) # ok 
print re.match('^(.*-){1}id(-.*)?$', s) # still ok, is should not be
Run Code Online (Sandbox Code Playgroud)

我尝试添加非贪婪模式,但结果仍然相同:

^(.*?-){1}id(-.*)?$
Run Code Online (Sandbox Code Playgroud)

我的正则表达式中缺少什么?我可以像这样验证字符串:

>>> import re
>>> print re.split('-', 'col1-col2-col3-id')
['col1', 'col2', 'col3', 'id']
Run Code Online (Sandbox Code Playgroud)

然后检查第三个元素是否与id匹配,但我感兴趣的是为什么第一个正则表达式如上所述.

python regex string

5
推荐指数
1
解决办法
5766
查看次数

String.ToLowerInvariant() 如何确定它必须转换为哪个字符串/字符?

正如我们所知,Unicode 的发明是为了解决代码页问题并表示世界上所有(不是所有但大多数)语言的所有字符。接下来我们有 unicode 转换格式 - 如何以计算机字节表示 unicode 字符:

  • utf-8 一个字符可以占用 1 到 4 个字节
  • utf-16 一个字符需要 2 个字节,或者 2*2bytes = 4bytes(.NET 使用这个)
  • utf-32 一个字符总是需要 4 个字节(我听说 Python 使用这个)

到目前为止,还好。接下来我们以两种语言为例:

英国的英语 (en-GB) 和斯洛文尼亚的斯洛文尼亚语 (sl-SI)。英语有下一个字符:a, b, c, d, e, ... x, y, z。除了 x,y 之外,斯洛文尼亚语具有相同的字符,并且还有其他字符:?、š、ž。如果我运行以下代码:

Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
string upperCase = "?".ToUpper(); // returns ?, which is correct based on sl-SI culture

// returns ?, how does it know that it must convert ? to ?. 
// What if some other …
Run Code Online (Sandbox Code Playgroud)

c# unicode

5
推荐指数
1
解决办法
2246
查看次数

如何使按查询分组更快?

我的下一张表有近 200 万条记录,当然每天都在增加。一些表记录(columnId 下方是父表的外键,例如 DirectionId --> 方向表,...):

Id         TypeId   DirectionId UserId  IndicatorId Date                                     Size      ExternalId
2003    100        1              1          1              2015-06-01 00:02:23.0000000 11931   28657340
2004    2           1               2          1             2015-06-01 00:03:21.0000000 10358   28657341
2005    2           2               2          1             2015-06-01 00:03:31.0000000 10848   28657342
2006    100        1              2          1             2015-06-01 00:03:52.0000000  7860    28657343
2007    100        1              3          1             2015-06-01 00:03:59.0000000  13353   28657344
Run Code Online (Sandbox Code Playgroud)

我需要获取最后一条消息 TypeId 和 DirectionId 的日期时间。下面的查询返回我需要的

select TypeId, DirectionID, max(date) as Date
from message
group by TypeId, DirectionID;

DirectionId TypeId  Date …
Run Code Online (Sandbox Code Playgroud)

sql-server group-by

5
推荐指数
2
解决办法
1万
查看次数