我有一个 DisplayedData 类......
public class DisplayedData
{
private int _key;
private String _username;
private String _fullName;
private string _activated;
private string _suspended;
public int key { get { return _key; } set { _key = value; } }
public string username { get { return _username; } set { _username = value; } }
public string fullname { get { return _fullName; } set { _fullName = value; } }
public string activated { get { return _activated; } set { _activated = value; } }
public string suspended { get { return _suspended; } set { _suspended = value; } }
}
我想将此类中的对象放入一个数组中,此类中的所有对象都应转换为String []
我有..
DisplayedData _user = new DisplayedData();
String[] _chosenUser = _user. /* Im stuck here :)
或者我可以创建一个数组,其中的所有项都由不同数据类型的变量组成,这样整数仍然是整数,所以字符串也是如此?
答案 0 :(得分:16)
您可以“亲手”创建数组(请参阅Arrays Tutorial):
String[] _chosenUser = new string[]
{
_user.key.ToString(),
_user.fullname,
_user.username,
_user.activated,
_user.suspended
};
或者您可以使用Reflection (C# Programming Guide):
_chosenUser = _user.GetType()
.GetProperties()
.Select(p =>
{
object value = p.GetValue(_user, null);
return value == null ? null : value.ToString();
})
.ToArray();