c# 映射取值和赋值

在c#中,有时候需要通过映射关系来实现对对象的取值(包括属性名称和值)。

           TSys_Flow flowData = flowApp.GetFormByTool(FID);
            List nameList = new List();
            PropertyInfo[] type = flowData.GetType().GetProperties();
            foreach (PropertyInfo info in type)
            {
                var name = info.Name;                                        //获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                object value = info.GetValue(flowData, null);     //用info.GetValue获得值
                if(value!=null && value.ToString() == "true")
                {
                    nameList.Add(name);
                }
            }
            if(nameList.Count!=0)
            {
                for (int i = 0; i < nameList.Count;i++ )
                {
                  var property = flowData.GetType().GetProperty(nameList[i]);
                  property.SetValue(flowData, null, null);
                }
                flowData.F_State = "0";  //表示未完成
            }
            NFineBase.Current.Update(flowData);

你可能感兴趣的:(c#学习之路,c#原创文章)