.NET通过字典给类赋值

 1 /// <summary>
 2         ///
 3         /// </summary>
 4         /// <typeparam name="T"></typeparam>
 5         /// <param name="origin">源数据</param>
 6         /// <param name="target">对象数据</param>
 7         /// <param name="dict">变量名对应字典</param>
 8         public static void CopyTo<T>(this object origin, T target,Dictionary<string,string> dict)where T :class,new()
 9         {
10             PropertyInfo[] props = target.GetType().GetProperties();
11
12             foreach (PropertyInfo info in props)
13             {
14                 var variable = dict.FirstOrDefault(m => m.Value == info.Name);
15                 if (variable.Key!=null)
16                 {
17                     string variableName = variable.Key;
18                     string chineseName = variable.Value;
19                     var propertyValue =
20                         origin.GetType()
21                             .GetProperty(variableName)
22                             .GetValue(origin, null);
23                     if (propertyValue != null)
24                     {
25                         if (propertyValue.GetType().IsClass)
26                         {
27
28                         }
29                         target.GetType()
30                             .InvokeMember(chineseName, BindingFlags.SetProperty, null, target,
31                                 new object[] { propertyValue });
32                     }
33                 }
34             }
35         }

时间: 2024-08-22 10:23:26

.NET通过字典给类赋值的相关文章

iOS开发&gt;学无止境 - 遍历Model类的属性并完善使用Runtime给Model类赋值

在前几天的一篇博客<iOS开发之使用Runtime给Model类赋值>中介绍了如何使用运行时在实体类的基类中添加给实体类的属性赋值的方法,这个方法的前提是字典的Key必须和实体类的Property Name相同,然后通过运行时来生成和执行Setter方法给Model类的属性赋值. 通 过Runtime来给Model类属性赋值的好处是多多的,它便于代码的后期维护,并且提高了开发效率.当你拿到解析后的字典时你不用一个一个的通过 key去把字典的值赋值给相应的Model类的属性,本篇博客中会给出如何

用字典给Model赋值

此篇教程讲述通过runtime扩展NSObject,可以直接用字典给Model赋值,这是相当有用的技术呢. 源码: NSObject+Properties.h 与 NSObject+Properties.m // // NSObject+Properties.h // // Created by YouXianMing on 14-9-4. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <Foundatio

iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值

http://www.cocoachina.com/ios/20150807/12877.html 在上篇博客<iOS开发之使用Runtime给Model类赋值>中介绍了如何使用运行时在实体类的基类中添加给实体类的属性赋值的方法,这个方法的前提是字典的Key必须和实体类的Property Name相同,然后通过运行时来生成和执行Setter方法给Model类的属性赋值. 通过Runtime来给Model类属性赋值的好处是多多的,它便于代码的后期维护,并且提高了开发效率.当你拿到解析后的字典时你

iOS开发之使用Runtime给Model类赋值

本篇博客算是给网络缓存打个基础吧,本篇博客先给出简单也是最容易使用的把字典转成实体类的方法,然后在给出如何使用Runtime来给Model实体类赋值.本篇博客会介绍一部分,主要是字典的key与Model的属性名相同时,使用Runtime来进行赋值,下篇博客会给出字典key的值和Model的名字不同时的解决方案,并给出使用Runtime打印实体类属性值的方式. 当然你可以使用KVC的setValuesForKeysWithDictionary:方法,下面的方法也是一种解决方案.如果使用setVal

python :列表 字典 集合 类 ----局部变量可以改全局变量

#列表 字典 集合 类 ----局部变量可以改全局变量,除了整数和字符串 names=["alex","jack","luck"] def func(names): names[0]='金角大王' print("inside name:" ,names) func(names) print (names) #字符串 name='jack' name1=name name='jack_chen' print(name,name1

类赋值问题

package 类赋值问题; import java.util.Arrays; public class ST { private byte[] szMsgCode = new byte[4]; private byte[] sBitMap = new byte[3]; public byte[] getSzMsgCode() { return szMsgCode; } public void setSzMsgCode(byte[] szMsgCode) { this.szMsgCode = s

字典的快速赋值 setValuesForKeysWithDictionary

字典的快速赋值 setValuesForKeysWithDictionary ? 前言 在学习解析数据的时候,我们经常是这么写的:PersonModel.h文件中    @property (nonatomic,copy)NSString *name;    @property (nonatomic,copy)NSString *sex;    @property (nonatomic,copy)NSString *age; 字典:     NSDictionary *dic = @{@"nam

JS字典 Dictionary类

字典 Dictionary类 /*字典 Dictionary类*/ function Dictionary() { this.add = add; this.datastore = new Array(); this.find = find; this.remove = remove; this.showAll = showAll; this.count = count; this.clear = clear; } function add(key, value) { this.datastor

c++,派生类对象可以对基类赋值,基类对派生类不可以赋值

#include <iostream> using namespace std; class DemoA { public: int m_a; void show(); DemoA(int val); }; DemoA::DemoA(int val) { m_a = val ; } void DemoA::show() { cout<<"ashow: DemoA.m_a="<<this->m_a<<endl; } //-------