我正在从数据库中检索一行,该行的属性是逗号分隔的 ID 列表
1,2,3,4,5
在我的 POCO 中是否可以做这样的事情来取回数组?
public string SomeIDs
{
get
{
return SomeIDs.split(',');
}
set;
}
编辑:抱歉,澄清一下,我正在设置一个字符串并想返回一个字符串数组
最佳答案
你不能有一个接受 string
并返回 string[]
的 setter。您需要公开一个接受 string
的属性,以及一个从该字符串返回已解析数组的只读属性(如下所示):
private static readonly string[] emptyIds = new string[0];
public string SomeIds { get; set; }
public string[] ParsedIds
{
get
{
return !string.IsNullOrEmpty(SomeIds) ? SomeIds.Split(',') :
emptyIds;
}
}
编辑:
关于c# - 从 C# 字符串属性获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30576412/