C# 反射设置获取类属性

最近在做个项目,采集数据入库,数据库表字段采用V01,V02,....,V120,另外有一张DataStyle表对应V01等代表采集来字段。

需要根据传过来的字段通过Dictionary找到对应的V字段,在Model类对象中赋值,考虑采用反射来实现。

        /// 
        /// 获取类中的属性值
        /// 
        /// 
        /// 
        /// 
        public string GetModelValue(string FieldName, object obj)
        {
            try
            {
                Type Ts = obj.GetType();
                object o = Ts.GetProperty(FieldName).GetValue(obj, null);
                string Value = Convert.ToString(o);
                if (string.IsNullOrEmpty(Value)) return null;
                return Value;
            }
            catch
            {
                return null;
            }
        }

        /// 
        /// 设置类中的属性值
        /// 
        /// 
        /// 
        /// 
        public bool SetModelValue(string FieldName,string Value, object obj)
        {
            try
            {
                Type Ts = obj.GetType();
                object v = Convert.ChangeType(Value, Ts.GetProperty(FieldName).PropertyType);
                Ts.GetProperty(FieldName).SetValue(obj, v, null);
                return true;
            }
            catch
            {
                return false;
            }
        }

采用一行代码

 foreach (KeyValuePair<string, string> pair in datastylemap)
                {
                    
                    object v = ValueParse(packString, pair.Key);
                    if (v != null)
                    {
                        minDataModel.GetType().GetProperty(pair.Value).SetValue(minDataModel, Convert.ToDecimal(v), null);
                    }
                }

 

参考:

https://www.cnblogs.com/zhangchenliang/p/4441624.html

C# 中,反射提供了一种动态访问程序运行时信息的方法,包括获取型、方法、字段等。如果你想使用反射设置型为的对象的属性值,你可以按照以下步骤操作: 1. **获取型对象**: 首先,你需要获取你要操作的的 `Type` 对象。如果你有一个已知的型名称(如字符串),你可以用 `Type.GetType()` 方法获取。 ```csharp Type myClassType = Type.GetType("Namespace.ClassName"); ``` 2. **获取属性信息**: 使用 `GetProperty()` 方法找到指定名称的属性,如果属性存在,返回 `PropertyInfo` 对象。 ```csharp string attributeName = "PropertyName"; PropertyInfo property = myClassType.GetProperty(attributeName); ``` 3. **检查属性可写性**: 在设置属性之前,确保属性是可以被写的(即它是可赋值的)。 ```csharp if (property != null && property.CanWrite) { // ... } else { Console.WriteLine($"无法写入属性 '{attributeName}'"); return; } ``` 4. **获取属性值(如果有的话)并设置新值**: 如果属性是可写的,你可以获取当前值并设置新的值。这里假设属性是 `public` 或者有合适的访问修饰符。 ```csharp object currentValue = property.GetValue(myObjectInstance); // 获取当前值 object newValue = ...; // 新的值 property.SetValue(myObjectInstance, newValue); // 设置新值 ``` 5. **处理异常**: 当尝试访问或修改属性时,可能会抛出异常,比如找不到属性或权限不足。记得捕获这些异常。 ```csharp try { // 设置属性值 } catch(TargetInvocationException ex) { Console.WriteLine($"错误: {ex.InnerException.Message}"); } catch(Exception ex) { Console.WriteLine($"错误: {ex.Message}"); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值