using FastMember;
using System;
using System.Linq;
namespace FastMemberDemo
{
class Program
{
static void Main(string[] args)
{
TypeAccessor Accessor = TypeAccessor.Create(typeof(Employee));
MemberSet Members = Accessor.GetMembers();
Console.WriteLine("------- Member 相關");
Console.WriteLine("-- 只列出公開屬性,所以沒有 Status");
foreach (Member m in Members)
{
Console.WriteLine(m.Name);
}
Console.WriteLine("-- 屬性存在測試");
string PropertyName = "PropertyNotExists";
if (Members.Any(m => m.Name == PropertyName) == false)
Console.WriteLine($"該屬性 {PropertyName} 不存在");
Console.WriteLine("------- Accessor 相關");
Console.WriteLine("-- Property 取值和設定測試");
Employee employee = new Employee()
{
EmployeeID = 1,
EmployeeName = "員工姓名",
Birthday = new DateTime(2021, 8, 13)
};
// 設定 Property 值
Accessor[employee, nameof(Employee.EmployeeName)] = "哇係 FastMember 啦";
Console.WriteLine($"透過 FastMember 設定 EmployeeName:{employee.EmployeeName}");
// 取得 Property 值
DateTime birthday = (DateTime)Accessor[employee, nameof(Employee.Birthday)];
Console.WriteLine($"生日為 {birthday}");
}
}
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public DateTime Birthday { get; set; }
/// <summary>
/// 故意設定為 private 來測試的
/// </summary>
private bool Status { get; set; }
public int? Age { get; set; }
}
}
星期五, 8月 13, 2021
[C#] FastMember
在公司內發現該套件 - FastMember,了解如何使用它,簡易筆記一下
沒有留言:
張貼留言