C#獲取某個物件的屬性值

哎呦喂O_o嗨發表於2020-12-30
/// <summary>
/// 獲取某個物件中的屬性值
/// </summary>
/// <param name="info"></param>
/// <param name="field"></param>
/// <returns></returns>
public static object GetPropertyValue(object info, string field)
{
    if (info == null) return null;
    Type t = info.GetType();
    IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
    return property.First().GetValue(info, null);
}

相關文章