最快的映射对象的方法 [英] Fastest way of mapping objects

查看:116
本文介绍了最快的映射对象的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#.Net:

我有一个大对象,其中包含20个属性的复杂映射和38个属性的简单映射,目前我使用反射映射这些,但我仍然需要在性能方面有所改进映射。



任何人都可以建议哪个库最好提高映射性能?



我尝试了什么:



i尝试使用自动播放器以及手动映射属性。

我们还需要改进性能。

我当前的实现:

C# .Net :
I have big object which contains complex mapping with 20 properties and simple mapping with 38 properties ,Currently i'm mapping these using reflection but still i need a performance improvement in my mapping.

Can any one suggest which library is best to improve the mapping performance?

What I have tried:

i tried with automapper and also manual mapping of properties.
still we need improvement in performance.
My current implementation:

public void FillObjectFromData(object objectToFill, object fromData)
		{
			if (fromData != null)
			{
				System.Reflection.PropertyInfo[] contractProperties = (objectToFill.GetType()).GetProperties();
				foreach (System.Reflection.PropertyInfo contractProperty in contractProperties)
				{
					if (contractProperty.CanWrite && fromData.GetType().GetProperty(contractProperty.Name) != null)
					{
						try
						{
							object dataValue = fromData.GetType().GetProperty(contractProperty.Name).GetValue(fromData, null);
							if ((dataValue != null) || (contractProperty.PropertyType.IsGenericType && contractProperty.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))
							{
								contractProperty.SetValue(objectToFill, dataValue, null);
							}
						}
						catch //(Exception ex)
						{

						}
					}
				}
			}
		}

推荐答案

我的建议也是跳过所有映射。您可以通过简单地定义接口并传递它们来轻松隐藏属性。例如,您收到 Abcde ,但将其作为 IAbc 传递,这有效地限制了对某些属性的访问。

My advice would also be to skip all that mapping. You can easily hide properties by simply defining interfaces and pass those. For example, you receive Abcde but pass it as IAbc which effectively restricts access to some properties.
public interface IAbc 
{
  int A { get; }
  int B { get; }
  int C { get; }
}

public class Abcde : IAbc
{
  public int A { get; set; }
  public int B { get; set; }
  public int C { get; set; }
  public int D { get; set; }
  public int E { get; set; }
}



祝你好运!


Good luck!


这篇关于最快的映射对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆