关于PropertyInfo setvalue()和PropertyInfo getvalue()

var property = obj.GetType().GetProperty(“PropertyName”);

表示获取类obj的属性PropertyName

PropertyInfo[] propertys = t.GetType().GetProperties();

表示获取类t的所有属性

//遍历该对象的所有属性  
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;//将属性名称赋值给临时变量  
                    //检查DataTable是否包含此列(列名==对象的属性名)    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter  
                        if (!pi.CanWrite) continue;//该属性不可写,直接跳出  
                        //取值  
                        object value = dr[tempName];
                        //如果非空,则赋给对象的属性  
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }

利用反射对类的属性进行赋值时,会用到setValue方法

这个方法有 3各参数,第一个表示类t,第二个表示属性值,第3个详细讲解

该属性类型是已知基本类型可以直接将变量传入, 例如:string

var value=”wangheng.org”;

property.SetValue(obj,value,null);

这里需要注意value值的类型必须和属性类型一致,否则会抛出TargetException异常。

如果原值是其他类型。例如:目标类型为int,值为string

string value=”100″;

property.SetValue(obj,int.TryParse(value),null);//类型转换。


getValue(obj,null)


### C# 中的 PropertyInfo 使用与示例 在 C# 中,`PropertyInfo` 是反射(Reflection)的一部分,用于获取有关类或结构中属性的信息。通过 `PropertyInfo`,可以动态地读取设置属性值,或者检查属性的特性(Attributes)。以下是关于 `PropertyInfo` 的详细说明示例。 #### 获取 PropertyInfo 对象 可以通过 `Type` 类的 `GetProperty` 方法获取 `PropertyInfo` 对象。此方法需要提供属性名称作为参数,并可选地指定绑定标志以控制搜索行为[^1]。 ```csharp using System; using System.Reflection; namespace PropertyInfoExample { class Program { static void Main(string[] args) { Type type = typeof(Person); PropertyInfo propertyInfo = type.GetProperty("Name"); if (propertyInfo != null) { Console.WriteLine($"Property Name: {propertyInfo.Name}"); Console.WriteLine($"Property Type: {propertyInfo.PropertyType}"); } } } public class Person { public string Name { get; set; } public int Age { get; set; } } } ``` #### 动态访问修改属性值 一旦获得了 `PropertyInfo` 对象,就可以使用其 `GetValue` `SetValue` 方法来动态读取或设置属性值。 ```csharp using System; using System.Reflection; namespace PropertyInfoExample { class Program { static void Main(string[] args) { Person person = new Person { Name = "Alice", Age = 30 }; Type type = typeof(Person); PropertyInfo nameProperty = type.GetProperty("Name"); PropertyInfo ageProperty = type.GetProperty("Age"); // 获取属性值 Console.WriteLine($"Name: {nameProperty.GetValue(person)}"); Console.WriteLine($"Age: {ageProperty.GetValue(person)}"); // 设置属性值 nameProperty.SetValue(person, "Bob"); ageProperty.SetValue(person, 25); Console.WriteLine($"Updated Name: {nameProperty.GetValue(person)}"); Console.WriteLine($"Updated Age: {ageProperty.GetValue(person)}"); } } public class Person { public string Name { get; set; } public int Age { get; set; } } } ``` #### 检查属性特性 如果属性上有自定义特性(如引用中的 `TestAttribute`),可以使用 `PropertyInfo.GetCustomAttributes` 方法来获取这些特性。 ```csharp using System; using System.Reflection; namespace PropertyInfoExample { class Program { static void Main(string[] args) { Type type = typeof(Person); PropertyInfo propertyInfo = type.GetProperty("Name"); object[] attributes = propertyInfo.GetCustomAttributes(false); foreach (var attribute in attributes) { Console.WriteLine(attribute); } } } public class TestAttribute : Attribute { public int TheNumber { get; set; } public string Name { get; set; } public TestAttribute(int number) { TheNumber = number; } public void PrintOut() { Console.WriteLine($"\tTheNumber = {TheNumber}"); Console.WriteLine($"\tName = \"{Name}\""); } } public class Person { [Test(42)] public string Name { get; set; } public int Age { get; set; } } } ``` #### 反射所有属性 可以使用 `Type.GetProperties` 方法获取类型的所有公共属性,并遍历它们。 ```csharp using System; using System.Reflection; namespace PropertyInfoExample { class Program { static void Main(string[] args) { Type type = typeof(Person); PropertyInfo[] properties = type.GetProperties(); foreach (var property in properties) { Console.WriteLine($"Property Name: {property.Name}, Property Type: {property.PropertyType}"); } } } public class Person { public string Name { get; set; } public int Age { get; set; } } } ``` ### 总结 `PropertyInfo` 提供了强大的功能,允许开发者在运行时动态操作对象的属性。无论是简单的读写操作还是复杂的特性检查,都可以通过反射实现。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值