将类转换为数组

Emm*_*ion 11 c# arrays class

我有一个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; } }
  }
Run Code Online (Sandbox Code Playgroud)

我想将这个类中的对象放入一个数组中,此类中的所有对象都应该转换为String []

我有..

DisplayedData _user = new DisplayedData();
String[] _chosenUser = _user. /* Im stuck here :)
Run Code Online (Sandbox Code Playgroud)

或者我可以创建一个数组,其中的所有项目都包含不同数据类型的变量,以便整数保持整数,所以字符串也是如此?

hor*_*rgh 18

您可以"自己动手"创建一个数组(参见Arrays Tutorial):

String[] _chosenUser = new string[] 
{ 
    _user.key.ToString(), 
    _user.fullname,
    _user.username,
    _user.activated,
    _user.suspended
};
Run Code Online (Sandbox Code Playgroud)

或者您可以使用Reflection(C#编程指南):

_chosenUser = _user.GetType()
                    .GetProperties()
                    .Select(p =>
                        {
                            object value = p.GetValue(_user, null);
                            return value == null ? null : value.ToString();
                        })
                    .ToArray();
Run Code Online (Sandbox Code Playgroud)