C#中屬性PropertyInfo使用示例小結(jié)
在C#中,PropertyInfo是一個(gè)用于獲取和設(shè)置屬性的類??梢允褂靡韵路绞絹硎褂肞ropertyInfo:
1.獲取屬性的Type: 可以使用PropertyInfo的PropertyType屬性來獲取屬性的類型。例如,如果有一個(gè)名為"Name"的屬性,可以使用以下代碼獲取屬性的類型:
PropertyInfo propertyInfo = typeof(ExampleClass).GetProperty("Name");
Type propertyType = propertyInfo.PropertyType;2.獲取屬性的值: 可以使用PropertyInfo的GetValue方法來獲取屬性的值。需要提供一個(gè)對(duì)象實(shí)例作為參數(shù),表示從該對(duì)象中獲取屬性的值。例如:
ExampleClass example = new ExampleClass();
example.Name = "John";
PropertyInfo propertyInfo = typeof(ExampleClass).GetProperty("Name");
object propertyValue = propertyInfo.GetValue(example);3.設(shè)置屬性的值: 可以使用PropertyInfo的SetValue方法來設(shè)置屬性的值。需要提供一個(gè)對(duì)象實(shí)例和要設(shè)置的值作為參數(shù)。例如:
ExampleClass example = new ExampleClass();
PropertyInfo propertyInfo = typeof(ExampleClass).GetProperty("Name");
propertyInfo.SetValue(example, "John");這些是PropertyInfo的一些基本用法。還可以使用其他方法和屬性來進(jìn)行更高級(jí)的操作,例如獲取和設(shè)置屬性的訪問修飾符、屬性的特性等。
常規(guī)屬性
看下常規(guī)屬性的完成構(gòu)成:
1.私有字段,一般設(shè)置為私有,通過屬性來賦值保證起安全性:
private string _age;
2.get訪問器,負(fù)責(zé)讀取數(shù)據(jù),其中可以進(jìn)行自己的邏輯判斷和數(shù)據(jù)驗(yàn)證,以return或者throw結(jié)束:
自動(dòng)屬性
上面簡單的說了一下常規(guī)屬性,當(dāng)屬性訪問器中不需要其他邏輯時(shí),可以使用自動(dòng)屬性,
public int Id { get; set; }簡單的對(duì)比一下常規(guī)屬性和自動(dòng)屬性之間的區(qū)別吧:
1.自動(dòng)實(shí)現(xiàn)的屬性必須同時(shí)聲明 get 和 set 訪問器。創(chuàng)建 readonly 自動(dòng)實(shí)現(xiàn)屬性時(shí),需要將set 訪問器設(shè)置為private 。
2自動(dòng)實(shí)現(xiàn)的屬性上可以使用特性,不能用在支持后備字段上。 如果屬性的后備字段上使用特性,則應(yīng)該只創(chuàng)建常規(guī)屬性。
3.自動(dòng)實(shí)現(xiàn)屬性get,和set中不能包含特殊的邏輯處理。與字段類似,但不同于字段。與字段不同,屬性不作為變量來分類,不能將屬性作為 ref參數(shù)或 out參數(shù)傳遞。
屬性PropertyInfo的使用
定義Person類:
public class Person {
public Person(int id,string name,string address)
{
this.Id = id;
this.Name = name;
this.Address = address;
}
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}定義User類
public class User {
public int Id { get; set; }
public string Name { get; set; }
public string Group { get; set; }
}轉(zhuǎn)換方法:
public static User ConvertObject(User user,Person person)
{
PropertyInfo[] userPro = user.GetType().GetProperties();
PropertyInfo[] personPro = person.GetType().GetProperties();
if (userPro.Length>0&&personPro.Length>0)
{
for (int i = 0; i < userPro.Length; i++)
{
for (int j = 0; j < personPro.Length; j++)
{<br> //判斷User的屬性是不是的Person中
if (userPro[i].Name == personPro[j].Name && userPro[i].PropertyType == personPro[j].PropertyType)
{
Object value=personPro[j].GetValue(person, null);
//將Person中屬性的值賦值給User<br> userPro[i].SetValue(user,value , null);
}
}
}
}
return user;
}方法的調(diào)用:
static void Main(string[] args)
{
Person person = new Person(1,"FlyElephant","北京");
User user = new User();
user.Id = 20;
user = ConvertObject(user, person);
Console.WriteLine("Id:" + user.Id + "Name:" + user.Name + "角色:" + user.Group);
System.Console.Read();
}2.之前在做Winform的時(shí)候就經(jīng)?;厥褂玫絊qlHelper,現(xiàn)在也有很多公司是這么使用的,當(dāng)時(shí)很多東西感覺就是重復(fù)性的操作,一度以為編程只是復(fù)制粘貼,下面這段代碼大家應(yīng)該很常見:
List<Person> list = new List<Person>();
SqlDataReader sdr = new SqlDataReader();
while (sdr.Read())
{
Person person = new Person();
person.Name = sdr.GetString(0);
//....下面類似
list.Add(person);
}開始寫的時(shí)候覺得是鍛煉了,寫的多了就覺得無聊了,其實(shí)完全可以換一種方式來實(shí)現(xiàn)上面的代碼:
public static List<T> ConvertData<T>(SqlDataReader sdr)
{
List<T> list = new List<T>();
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
while (sdr.Read())
{
T model = Activator.CreateInstance<T>();
for (int i = 0; i < properties.Length; i++)
{
for (int j = 0; j < sdr.FieldCount; j++)
{
//判斷屬性的名稱和字段的名稱是否相同
if (properties[i].Name == sdr.GetName(j))
{
Object value =sdr[j];
//將字段的值賦值給User中的屬性
properties[i].SetValue(model, value, null);
}
}
}
list.Add(model);
}
return list;
}List<User> list = new List<User>(); SqlDataReader sdr = cmd.ExecuteReader(); list = ConvertData<User>(sdr);
到此這篇關(guān)于C#中屬性PropertyInfo怎么使用的文章就介紹到這了,更多相關(guān)C# PropertyInfo使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#獲取變更過的DataTable記錄的實(shí)現(xiàn)方法
這篇文章主要介紹了C#獲取變更過的DataTable記錄的實(shí)現(xiàn)方法,對(duì)初學(xué)者很有學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08
.NET智能處理Word文檔之文本查找替換與書簽操作完全指南
本文將詳細(xì)介紹如何使用MudTools.OfficeInterop.Word庫來執(zhí)行文本查找替換操作,包括普通文本替換、高級(jí)通配符替換以及替換為剪貼板內(nèi)容等高級(jí)功能,有需要的可以了解下2025-09-09

