Để bắt đầu mình sẽ tạo ra 1 class với mục đích demo như sau:
public class User 
{
    public string ID { get; set; }
    public string username { get; set; }
    public string email { get; set; }
    public string phoneNumber { get; set; }
    public string password { get; set; }
    public string salt { get; set; }
}Với class User đã tạo ra ở trên, ta có 5 thuộc tính:
- ID
- username
- phoneNumber
- password
- salt
Để chuyển thuộc tính về mảng hay dictionary thì cần:
using System.Reflection;Chuyển các thuộc tính thành mảng
Từ 5 thuộc tính trên, ta sẽ chuyển về mảng 1 chiều ( kiểu string ) và giá trị của các thuộc tính tương ứng với thự tự các thuộc tính.
public static void Main()
{
  
	User user = new User 
    {
      ID = "23487",
      email = "[email protected]",
      password = "slfew39232nfi3fj435",
      phoneNumber = "0324709637",
      username = "phanxuanchanh",
      salt = "230923fjdslfj"
    }
  
    string[] arr = user.GetType()
                .GetProperties()
                .Select(prop =>
                {
                    object value = prop.GetValue(obj, null);
                    return value == null ? null : value.ToString();
                }).ToArray();
  
   for(int i = 0; i < arr.Length; i++)
   {
     Console.WriteLine($"Index = {i} => Value: {arr[i]}");
   }
}Kết quả sau khi chạy chương trình sẽ như sau:
- Index = 0 => Value: 23487
- Index = 1 => Value: phanxuanchanh
- Index = 2 => Value: [email protected]
- Index = 3 => Value: 0324709637
- Index = 4 => Value: slfew39232nfi3fj435
- Index = 5 => Value: 230923fjdslfj
Chuyển các thuộc tính thành kiểu Dictionary
Các thuộc tính sẽ được chuyển thành 1 Dictionary trong đó:
- Key là tên của thuộc tính kiểu string
- Value là giá trị của thuộc tính kiểu string
public static void Main()
{
 
	User user = new User
    {
      ID = "23487",
      username = "phanxuanchanh",
      email = "[email protected]",
      phoneNumber = "0324709637",
      password = "slfew39232nfi3fj435",
      salt = "230923fjdslfj"
    }
  
     Dictionary<string, string> dict = obj.GetType()
         .GetProperties(BindingFlags.Instance | BindingFlags.Public)
         .ToDictionary(
              prop => prop.Name,
              prop => (prop.GetValue(obj, null) == null) 
       	             ? null 
                     : prop.GetValue(obj, null).ToString()
          );
  
     foreach(KeyValuePair<string, string> pair in dict){
         Console.WriteLine($"Key: {pair.Key} ==> Value: {pair.Value}");
     }
}Kết quả sau khi chạy chương trình sẽ như sau:
- Key: ID ==> Value: 23487
- Key: username ==> Value: phanxuanchanh
- Key: email ==> Value: [email protected]
- Key: phoneNumber ==> Value: 0324709637
- Key: password ==> Value: slfew39232nfi3fj435
- Key: salt ==> Value: 230923fjdslfj
Xây dựng lớp tổng quát
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Common.ReflectionExtension
{
    public class ReflectionExtension<T>
    {
        public static string[] ToArray(T obj)
        {
            string[] arr = obj.GetType()
                .GetProperties()
                .Select(prop =>
                {
                    object value = prop.GetValue(obj, null);
                    return value == null ? null : value.ToString();
                }).ToArray();
            return arr;
        }
        public static Dictionary<string, string> ToDictionary(T obj)
        {
            Dictionary<string, string> dict = obj.GetType()
                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .ToDictionary(
                    prop => prop.Name,
                    prop => (prop.GetValue(obj, null) == null) 
                           ? null 
                           : prop.GetValue(obj, null).ToString());
            return dict;
        }
    }
}