本文介绍了从C#字符串属性获取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从数据库中检索一行,该行的属性是ID的逗号分隔列表
I'm retrieving a row from a database that has an attribute which is a comma separated list of ID's
1,2,3,4,5
在我的POCO中可以做这样的事情来找回阵列吗?
In my POCO is it possible to do something like this to get an array back?
public string SomeIDs { get { return SomeIDs.split(','); } set; }很抱歉,为了澄清,我正在设置一个字符串,并想返回一个字符串数组
Sorry, to clarify, I am setting with a string and want to return a string array
推荐答案您不能有一个接受string并返回string[]的设置器.您需要公开一个接受string的属性,以及一个只读属性(如下所示),该属性从该字符串返回已解析的数组:
You can't have a setter which accepts a string and returns a string[]. You'll need to expose one property which accepts a string, and a read-only property (as below) which returns a parsed array from that 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#字符串属性获取数组
发布评论