Home Backend Development C#.Net Tutorial .NET implements code by assigning values ​​to classes through dictionaries

.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 });
}
}
}
}
Copy after login



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)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python program to get first and last element in dictionary Python program to get first and last element in dictionary Sep 07, 2023 pm 05:01 PM

Python program to get first and last element in dictionary

Empty dictionary key incorrect: How to solve Python's dictionary key error? Empty dictionary key incorrect: How to solve Python's dictionary key error? Jun 24, 2023 pm 03:03 PM

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

How to get the next key in a dictionary in Python? How to get the next key in a dictionary in Python? Aug 28, 2023 pm 11:45 PM

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

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

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

What are the employment prospects of C#? What are the employment prospects of C#? Oct 19, 2023 am 11:02 AM

What are the employment prospects of C#?

C++ program initialization dictionary C++ program initialization dictionary Sep 09, 2023 pm 07:01 PM

C++ program initialization dictionary

Python program to delete null values ​​​​in the dictionary as an example Python program to delete null values ​​​​in the dictionary as an example Sep 03, 2023 pm 04:45 PM

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

Performance differences between Java framework and .NET framework Performance differences between Java framework and .NET framework Jun 03, 2024 am 09:19 AM

Performance differences between Java framework and .NET framework

See all articles