C# PropertyInfo類(lèi)案例詳解
對(duì)一個(gè)對(duì)象進(jìn)行屬性分析,并得到相應(yīng)的屬性值,并判斷屬性的默認(rèn)值以及空值
public class People
{
public string name { get; set; }
public int age { get; set; }
public DateTime birthday { get; set; }
public bool isActive { get; set; }
public List<Address> address{get;set;}
}
public class Address
{
public string country { get; set; }
public string province { get; set; }
public string city { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Address> address = new List<Address>()
{
new Address(){
country="china",
province="anHui",
city="bengBu",
},
new Address(){
country="china",
city="shangHai",
},
};
People people = new People()
{
name="wangqilong",
age=23,
birthday=Convert.ToDateTime("2018-09-15"),
isActive=true,
address=address
};
string str = method(people);
}
public static string method(Object obj)
{
string str = "";
Type postType = obj.GetType();
PropertyInfo[] postTypeInfos = postType.GetProperties(); //返回為當(dāng)前 Type 的所有公共屬性,PropertyInfo[] PropertyInfo 的所有公共屬性的 Type 對(duì)象數(shù)組
foreach (PropertyInfo p in postTypeInfos)
{
if (p.PropertyType.FullName == typeof(DateTime).FullName)
{
DateTime pValue = (DateTime)p.GetValue(obj, null);
if (pValue != null && pValue != DateTime.MinValue) //dateTime類(lèi)型申明時(shí)默認(rèn)值為最小值
{
str += p.Name + ":" + pValue + ";";
}
}
else if (p.PropertyType.FullName == typeof(Int32).FullName)
{
int pValue = (int)p.GetValue(obj, null);
if (pValue != 0) //int類(lèi)型申明時(shí)默認(rèn)值為最小值0
{
str += p.Name + ":" + pValue + ";";
}
}
else if (p.PropertyType.FullName == typeof(Boolean).FullName)
{
Object pValue = p.GetValue(obj, null);
str += p.Name + ":" + pValue + ";";
}
else if (p.PropertyType.FullName == typeof(String).FullName)
{
Object pValue = p.GetValue(obj, null);
str += p.Name + ":" + pValue + ";";
}
//如果傳入的對(duì)象包含集合,集合中是另個(gè)對(duì)象
else if (p.PropertyType.FullName == typeof(List<Address>).FullName)
{
List<Address> list = (List<Address>)p.GetValue(obj, null);
if (list != null)
{
foreach (Address address in list)
{
str += p.Name + ":" + address.country+","+address.province+","+address.city + ";";
}
}
}
}
return str;
}
}
結(jié)果:”name:wangqilong;age:23;birthday:2018/9/15 0:00:00;isActive:True;address:china,anHui,bengBu;address:china,,shangHai;”
關(guān)于PropertyInfo類(lèi)信息: https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection.propertyinfo?view=netframework-1.1
到此這篇關(guān)于C# PropertyInfo類(lèi)案例詳解的文章就介紹到這了,更多相關(guān)C# PropertyInfo類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 圖片與Base64碼的相互轉(zhuǎn)化問(wèn)題(代碼詳解)
這篇文章主要介紹了C# 圖片與Base64碼的相互轉(zhuǎn)化的代碼,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C#操作SQLite實(shí)現(xiàn)數(shù)據(jù)的增刪改查
SQLite是一個(gè)輕量級(jí)、跨平臺(tái)的關(guān)系型數(shù)據(jù)庫(kù),在小型項(xiàng)目中,方便,易用,同時(shí)支持多種開(kāi)發(fā)語(yǔ)言。本文將用C#語(yǔ)言對(duì)SQLite 的一個(gè)封裝,實(shí)現(xiàn)數(shù)據(jù)的增刪改查。需要的可以參考一下2022-01-01
C#中如何在Excel工作表創(chuàng)建混合型圖表實(shí)例
本篇文章主要介紹了C#中如何在Excel工作表創(chuàng)建混合型圖表實(shí)例,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11

