3
我正在使用AutoMapper在我的域模型和視圖模型之間進行映射。我的虛擬主機只支持中等信任,所以AutoMapper不起作用。對於像AutoMapper這樣的可以以中等信任度運行的優秀映射器還有其他建議嗎?用於中等信託託管的AutoMapper替代方案
我沒有訪問主機上的IIS。
我正在使用AutoMapper在我的域模型和視圖模型之間進行映射。我的虛擬主機只支持中等信任,所以AutoMapper不起作用。對於像AutoMapper這樣的可以以中等信任度運行的優秀映射器還有其他建議嗎?用於中等信託託管的AutoMapper替代方案
我沒有訪問主機上的IIS。
可以建立一種簡單的映射這樣的,如果具有相同名稱的模型使用特性:
public static class Mapper {
/// <summary>
/// Copy all not null properties values of object source in object target if the properties are present.
/// Use this method to copy only simple type properties, not collections.
/// </summary>
/// <param name="source">source object</param>
/// <param name="target">target object</param>
private static void SimpleCopy(object source, object target)
{
foreach (PropertyInfo pi in source.GetType().GetProperties())
{
object propValue = pi.GetGetMethod().Invoke(source, null);
if (propValue != null)
{
try
{
PropertyInfo pit = GetTargetProperty(pi.Name, target);
if (pit != null) pit.GetSetMethod().Invoke(target, new object[] { propValue });
}
catch (Exception) { /* do nothing */ }
}
}
}
private static PropertyInfo GetTargetProperty(string name, object target)
{
foreach (PropertyInfo pi in target.GetType().GetProperties())
{
if (pi.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)) return pi;
}
return null;
}
}