将一个类到一个数组

将一个类到一个数组

问题描述:

我有一个的 DisplayedData ​​ em>的类...

I have a DisplayedData class ...

  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 []

And I want to to put the objects from this class into an array where all objects inside of this class should be converted into an String[]

我..

DisplayedData _user = new DisplayedData();
String[] _chosenUser = _user. /* Im stuck here :)

或者我可以创建一个数组,其中的所有项目里面都包含不同的数据类型的变量,这样的整数仍然是一个整数,因此琴弦呢?

or can I create an array where all the items inside are consist of variables of different datatype so that the integer remains an integer and so the strings too?

您可以创建一个数组用自己的双手(见的Ar​​rays教程):

You can create an array "with your own hands" (see Arrays Tutorial):

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

或者你可以使用反射(C#编程指南)

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