- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
...除了明显的列表循环和肮脏的 great case 语句外!
我已经在脑海中思考了几个 Linq 查询,但似乎没有任何结果。
如果有帮助的话,下面是一个 DTO 示例:
class ClientCompany
{
public string Title { get; private set; }
public string Forenames { get; private set; }
public string Surname { get; private set; }
public string EmailAddress { get; private set; }
public string TelephoneNumber { get; private set; }
public string AlternativeTelephoneNumber { get; private set; }
public string Address1 { get; private set; }
public string Address2 { get; private set; }
public string TownOrDistrict { get; private set; }
public string CountyOrState { get; private set; }
public string PostCode { get; private set; }
}
恐怕我们无法控制以 KV 对形式获取数据这一事实。
虽然每个 KV 对到每个属性都有一个有效的映射,但我确实事先知道 key ,但它们的名称与 DTO 不同。
最佳答案
这是一个用于从字典加载 DTO 的优雅、可扩展、可维护且速度极快的解决方案。
创建一个控制台应用程序并添加这两个文件。其余的是 self 记录。
要点:
注意:如果您复制了之前的 DynamicProperties.cs,您将需要获取此文件。我添加了一个标志以允许生成以前版本中没有的私有(private) setter 。
干杯。
program.cs
using System.Collections.Generic;
using System.Diagnostics;
using Salient.Reflection;
namespace KVDTO
{
/// <summary>
/// This is our DTO
/// </summary>
public class ClientCompany
{
public string Address1 { get; private set; }
public string Address2 { get; private set; }
public string AlternativeTelephoneNumber { get; private set; }
public string CountyOrState { get; private set; }
public string EmailAddress { get; private set; }
public string Forenames { get; private set; }
public string PostCode { get; private set; }
public string Surname { get; private set; }
public string TelephoneNumber { get; private set; }
public string Title { get; private set; }
public string TownOrDistrict { get; private set; }
}
/// <summary>
/// This is our DTO Map
/// </summary>
public sealed class ClientCompanyMapping : KeyValueDtoMap<ClientCompany>
{
static ClientCompanyMapping()
{
AddMapping("Title", "Greeting");
AddMapping("Forenames", "First");
AddMapping("Surname", "Last");
AddMapping("EmailAddress", "eMail");
AddMapping("TelephoneNumber", "Phone");
AddMapping("AlternativeTelephoneNumber", "Phone2");
AddMapping("Address1", "Address1");
AddMapping("Address2", "Address2");
AddMapping("TownOrDistrict", "City");
AddMapping("CountyOrState", "State");
AddMapping("PostCode", "Zip");
}
}
internal class Program
{
private const string Address1 = "1243 Easy Street";
private const string CountyOrState = "Az";
private const string EmailAddress = "nunya@bidnis.com";
private const string Forenames = "Sky";
private const string PostCode = "85282";
private const string Surname = "Sanders";
private const string TelephoneNumber = "800-555-1212";
private const string Title = "Mr.";
private const string TownOrDistrict = "Tempe";
private static void Main(string[] args)
{
// this represents our input data, some discrepancies
// introduced to demonstrate functionality of the map
// the keys differ from the dto property names
// there are missing properties
// there are unrecognized properties
var input = new Dictionary<string, string>
{
{"Greeting", Title},
{"First", Forenames},
{"Last", Surname},
{"eMail", EmailAddress},
{"Phone", TelephoneNumber},
// missing from this input {"Phone2", ""},
{"Address1", Address1},
// missing from this input {"Address2", ""},
{"City", TownOrDistrict},
{"State", CountyOrState},
{"Zip", PostCode},
{"SomeOtherFieldWeDontCareAbout", "qwerty"}
};
// rehydration is simple and FAST
// instantiate a map. You could store instances in a dictionary
// but it is not really necessary for performance as all of the
// work is done in the static constructors, so no matter how many
// times you 'new' a map, it is only ever built once.
var map = new ClientCompanyMapping();
// do the work.
ClientCompany myDto = map.Load(input);
// test
Debug.Assert(myDto.Address1 == Address1, "Address1");
Debug.Assert(myDto.Address2 == null, "Address2");
Debug.Assert(myDto.AlternativeTelephoneNumber == null, "AlternativeTelephoneNumber");
Debug.Assert(myDto.CountyOrState == CountyOrState, "CountyOrState");
Debug.Assert(myDto.EmailAddress == EmailAddress, "EmailAddress");
Debug.Assert(myDto.Forenames == Forenames, "Forenames");
Debug.Assert(myDto.PostCode == PostCode, "PostCode");
Debug.Assert(myDto.Surname == Surname, "Surname");
Debug.Assert(myDto.TelephoneNumber == TelephoneNumber, "TelephoneNumber");
Debug.Assert(myDto.Title == Title, "Title");
Debug.Assert(myDto.TownOrDistrict == TownOrDistrict, "TownOrDistrict");
}
}
/// <summary>
/// Base mapper class.
/// </summary>
/// <typeparam name="T"></typeparam>
public class KeyValueDtoMap<T> where T : class, new()
{
private static readonly List<DynamicProperties.Property> Props;
private static readonly Dictionary<string, string> KvMap;
static KeyValueDtoMap()
{
// this property collection is built only once
Props = new List<DynamicProperties.Property>(DynamicProperties.CreatePropertyMethods(typeof(T)));
KvMap=new Dictionary<string, string>();
}
/// <summary>
/// Adds a mapping between a DTO property and a KeyValue pair
/// </summary>
/// <param name="dtoPropertyName">The name of the DTO property</param>
/// <param name="inputKey">The expected input key</param>
protected static void AddMapping(string dtoPropertyName,string inputKey)
{
KvMap.Add(dtoPropertyName,inputKey);
}
/// <summary>
/// Creates and loads a DTO from a Dictionary
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public T Load(Dictionary<string, string> input)
{
var result = new T();
Props.ForEach(p =>
{
string inputKey = KvMap[p.Info.Name];
if (input.ContainsKey(inputKey))
{
p.Setter.Invoke(result, input[inputKey]);
}
});
return result;
}
}
}
DynamicProperties.cs
/*!
* Project: Salient.Reflection
* File : DynamicProperties.cs
* http://spikes.codeplex.com
*
* Copyright 2010, Sky Sanders
* Dual licensed under the MIT or GPL Version 2 licenses.
* See LICENSE.TXT
* Date: Sat Mar 28 2010
*/
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
namespace Salient.Reflection
{
/// <summary>
/// Gets IL setters and getters for a property.
/// </summary>
public static class DynamicProperties
{
#region Delegates
public delegate object GenericGetter(object target);
public delegate void GenericSetter(object target, object value);
#endregion
public static IList<Property> CreatePropertyMethods(Type T)
{
var returnValue = new List<Property>();
foreach (PropertyInfo prop in T.GetProperties())
{
returnValue.Add(new Property(prop));
}
return returnValue;
}
public static IList<Property> CreatePropertyMethods<T>()
{
var returnValue = new List<Property>();
foreach (PropertyInfo prop in typeof (T).GetProperties())
{
returnValue.Add(new Property(prop));
}
return returnValue;
}
/// <summary>
/// Creates a dynamic setter for the property
/// </summary>
/// <param name="propertyInfo"></param>
/// <returns></returns>
/// <source>
/// http://jachman.wordpress.com/2006/08/22/2000-faster-using-dynamic-method-calls/
/// </source>
public static GenericSetter CreateSetMethod(PropertyInfo propertyInfo)
{
/*
* If there's no setter return null
*/
MethodInfo setMethod = propertyInfo.GetSetMethod(true);
if (setMethod == null)
return null;
/*
* Create the dynamic method
*/
var arguments = new Type[2];
arguments[0] = arguments[1] = typeof (object);
var setter = new DynamicMethod(
String.Concat("_Set", propertyInfo.Name, "_"),
typeof (void), arguments, propertyInfo.DeclaringType);
ILGenerator generator = setter.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
generator.Emit(OpCodes.Ldarg_1);
if (propertyInfo.PropertyType.IsClass)
generator.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
else
generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
generator.EmitCall(OpCodes.Callvirt, setMethod, null);
generator.Emit(OpCodes.Ret);
/*
* Create the delegate and return it
*/
return (GenericSetter) setter.CreateDelegate(typeof (GenericSetter));
}
/// <summary>
/// Creates a dynamic getter for the property
/// </summary>
/// <param name="propertyInfo"></param>
/// <returns></returns>
/// <source>
/// http://jachman.wordpress.com/2006/08/22/2000-faster-using-dynamic-method-calls/
/// </source>
public static GenericGetter CreateGetMethod(PropertyInfo propertyInfo)
{
/*
* If there's no getter return null
*/
MethodInfo getMethod = propertyInfo.GetGetMethod(true);
if (getMethod == null)
return null;
/*
* Create the dynamic method
*/
var arguments = new Type[1];
arguments[0] = typeof (object);
var getter = new DynamicMethod(
String.Concat("_Get", propertyInfo.Name, "_"),
typeof (object), arguments, propertyInfo.DeclaringType);
ILGenerator generator = getter.GetILGenerator();
generator.DeclareLocal(typeof (object));
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
generator.EmitCall(OpCodes.Callvirt, getMethod, null);
if (!propertyInfo.PropertyType.IsClass)
generator.Emit(OpCodes.Box, propertyInfo.PropertyType);
generator.Emit(OpCodes.Ret);
/*
* Create the delegate and return it
*/
return (GenericGetter) getter.CreateDelegate(typeof (GenericGetter));
}
#region Nested type: Property
public class Property
{
public GenericGetter Getter;
public PropertyInfo Info;
public GenericSetter Setter;
public Property(PropertyInfo info)
{
Info = info;
Setter = CreateSetMethod(info);
Getter = CreateGetMethod(info);
}
}
#endregion
}
}
关于c# - 有没有办法将键/值对列表转换为数据传输对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2543774/
我需要将数据从一个表传输到另一台已截断的服务器中的同一个表。最简单的方法是什么? 最佳答案 设置 linked servers然后在目标数据库上使用以下内容: INSERT INTO existing
我尝试从 mysql 服务器获取数据到 ms sql 服务器。我已经在本地主机(使用 ODBC 连接器)中完成了这个过程。 但是现在这些服务器作为在线数据库托管。谁能告诉我这样做的方法吗? 我想我不能
我有一个论坛在这里或http://neue.st/index.php 我想将数据从该论坛转移到不同目录中的新论坛http://neue.st/forums 我也只想选择要传输的数据,例如用户、帖子、主
我正在 Android 操作系统上开发 BLE 应用程序。我必须通过我的应用程序在 BLE 硬件上写入数据。我对必须发送的传输数据的类型感到困惑。下面的图像显示了我必须发送的字节数据。对于每个字节,它
我正在尝试将 RabbitMQ 用于分布式系统,其工作原理如下: 生产者将 JSON 格式的订单 ID 列表放入队列 多个消费者从该队列中取出,使用该订单 ID 执行业务逻辑,并将结果(JSON 格式
我正在将 ARM ComputeLibrary 集成到一个项目中。 这不是我所熟悉的语义的 API,但我正在研究文档和示例。 目前,我正在尝试将 std::vector 的内容复制到 CLTensor
我正在使用面向连接的 channel 开发 BLE 应用程序。我使用 nordic semiconductor nrf52 作为外围设备,iPhone 6 作为中央管理器。 我使用了蓝牙 SIG 提供
我有一个 redis 数据库、logstash 和两个 elasticsearch 和一个 influxdb。我正在将 key 从 redis 传输到 elasticsearch,它工作正常并且想测试
例如,我们在master1上运行一个主节点 在server2,server3上运行的两个数据节点 我们说分片重定位发生在server2到server3之间 现在要复制数据文件夹,elasticsear
基本上,我想做的是创建一个方法,可以采用任何数据类型,并将其转换为 php 识别的数据或 JSON。假设我想传递一个数组、一个二维数组或只是一些基本字符串。我会调用这个函数,传入参数并将其发送到 ph
我们在 UI 中使用 JSF,在业务层中使用 Spring,在持久层中使用 Hibernate。现在我的问题是如何将数据从 JSF UI 传递到 Spring 业务层。我可以直接在支持 bean 中使
我正在构建一个 android 应用程序(使用 java 1.6) - 这是实际的客户端 它向 Windows 计算机发送和接收数据,该计算机托管使用 networkStream 和 socket T
我将编写一个对用户输入使用react并将用户输入数据发送到服务器的应用程序。如果没有互联网连接,应用程序将批量数据并尽快发送。加密并不重要,因为只是发送了一堆关键信息,如果没有相应的真实数据,这些信息
我知道 Javascriptbridge 可以将数据从 js 发送到 tizen。 Is there any way to pass data from native to javascript 最佳
几年前,我编写了一个小实用程序,用于将数据从 Oracle 数据库移动到 Postgres 数据库。我使用 Java 和 JDBC 来完成此任务,因为我希望 Java 处理准备好的语句中使用的数据的数
我最近加入了 Facebook(我知道我迟到了),昨晚我带着一个奇迹醒来。看起来像实时聊天模块大约每秒“寻找”新的应答消息。在我看来,这有点太快了。我不知道他们是如何设法不得到他们的服务器处于事件状态
因此,我正在使用 jdbc 与 MySQL 数据库通信。对于许多表和许多查询/ View ,我创建了一个类,它封装了表的一行或查询/表结果。对 DB 的访问返回此类的一个对象(当我确切知道只有一个匹配
我尝试创建一个在我的虚拟机中运行的程序,以便我可以将数据从目录传输到我的 azure blob 存储帐户。每当我在程序外部(在命令行上)运行该命令时,它都会起作用,但是,如果我运行包含运行该命令的子进
我正在尝试建立一个小型系统,让一台服务器(也称为传感器)在发现另一台服务器(也称为服务器)可用时将数据文件传输到另一台服务器(也称为服务器)(都运行 node.js 应用程序)。 理想情况下,服务器应
我有一个带有两个按钮和两个文本字段的 DialogFragment。 我只希望当我在两个文本字段中输入数据并按下“确定”按钮时,它可以匹配两个字段的数据并将结果保存到 String。 Toast ms
我是一名优秀的程序员,十分优秀!