


.NET implements code by assigning values to classes through dictionaries
Feb 22, 2017 am 10:33 AM No more nonsense, let’s just post the code. The specific code is as follows:
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="origin">源数据</param> /// <param name="target">对象数据</param> /// <param name="dict">变量名对应字典</param> public static void CopyTo<T>(this object origin, T target,Dictionary<string,string> dict)where T :class,new() { PropertyInfo[] props = target.GetType().GetProperties(); foreach (PropertyInfo info in props) { var variable = dict.FirstOrDefault(m => m.Value == info.Name); if (variable.Key!=null) { string variableName = variable.Key; string chineseName = variable.Value; var propertyValue = origin.GetType() .GetProperty(variableName) .GetValue(origin, null); if (propertyValue != null) { if (propertyValue.GetType().IsClass) { } target.GetType() .InvokeMember(chineseName, BindingFlags.SetProperty, null, target, new object[] { propertyValue }); } } } }
The above is the .NET pass Dictionary assigns values to classes to implement code content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Python program to get first and last element in dictionary

Empty dictionary key incorrect: How to solve Python's dictionary key error?

How to get the next key in a dictionary in Python?

Share several .NET open source AI and LLM related project frameworks

What are the employment prospects of C#?

C++ program initialization dictionary

Python program to delete null values in the dictionary as an example

Performance differences between Java framework and .NET framework
