9using System.Collections.Generic;
10using System.ComponentModel;
22 public static class DataMapper
25 #region Map from IDictionary
37 public static void Map(System.Collections.IDictionary source,
object target)
39 Map(source, target,
false);
54 public static void Map(System.Collections.IDictionary source,
object target, params
string[] ignoreList)
56 Map(source, target,
false, ignoreList);
72 public static void Map(
73 System.Collections.IDictionary source,
74 object target,
bool suppressExceptions,
75 params
string[] ignoreList)
77 List<string> ignore =
new List<string>(ignoreList);
78 foreach (
string propertyName
in source.Keys)
80 if (!ignore.Contains(propertyName))
84 SetPropertyValue(target, propertyName, source[propertyName]);
88 if (!suppressExceptions)
89 throw new ArgumentException(
99 #region Map to Dictionary
106 public static void Map(
object source, Dictionary<string, object> target)
108 Map(source, target,
false);
118 public static void Map(
object source, Dictionary<string, object> target, params
string[] ignoreList)
120 Map(source, target,
false, ignoreList);
131 public static void Map(
132 object source, Dictionary<string, object> target,
133 bool suppressExceptions,
134 params
string[] ignoreList)
136 List<string> ignore =
new List<string>(ignoreList);
137 foreach (var propertyName
in GetPropertyNames(source.GetType()))
139 if (!ignore.Contains(propertyName))
143 target.Add(propertyName, MethodCaller.CallPropertyGetter(source, propertyName));
147 if (!suppressExceptions)
148 throw new ArgumentException(
149 String.Format(
"{0} ({1})",
158 #region Map from Object
171 public static void Map(
object source,
object target)
173 Map(source, target,
false);
189 public static void Map(
object source,
object target, params
string[] ignoreList)
191 Map(source, target,
false, ignoreList);
214 public static void Map(
215 object source,
object target,
216 bool suppressExceptions,
217 params
string[] ignoreList)
219 List<string> ignore =
new List<string>(ignoreList);
220 foreach (var propertyName
in GetPropertyNames(source.GetType()))
222 if (!ignore.Contains(propertyName))
226 object value = MethodCaller.CallPropertyGetter(source, propertyName);
227 SetPropertyValue(target, propertyName, value);
231 if (!suppressExceptions)
232 throw new ArgumentException(
233 String.Format(
"{0} ({1})",
252 public static void Map(
object source,
object target, DataMap map)
254 Map(source, target, map,
false);
270 public static void Map(
object source,
object target, DataMap map,
bool suppressExceptions)
272 foreach (DataMap.MemberMapping mapping in map.GetMap())
276 object value = mapping.FromMemberHandle.DynamicMemberGet(source);
277 SetValueWithCoercion(target, mapping.ToMemberHandle, value);
281 if (!suppressExceptions)
282 throw new ArgumentException(
283 String.Format(
"{0} ({1})",
289 private static IList<string> GetPropertyNames(Type sourceType)
291 List<string> result =
new List<string>();
292 PropertyDescriptorCollection props = TypeDescriptor.GetProperties(sourceType);
293 foreach (PropertyDescriptor item
in props)
294 if (item.IsBrowsable)
295 result.Add(item.Name);
300 #region Load from IDictionary
318 public static void Load(System.Collections.IDictionary source,
object target, Func<string, object> nameMapper)
320 var validTarget = target as Core.IManageProperties;
321 if (validTarget ==
null)
322 throw new NotSupportedException();
323 var propertyList = validTarget.GetManagedProperties();
324 foreach (var p
in propertyList)
325 validTarget.LoadProperty(p, source[nameMapper(p.Name)]);
330#region Load to IDictionary
348 public static void Load(
object source, System.Collections.IDictionary target, Func<string, object> nameMapper)
350 var validSource = source as Core.IManageProperties;
351 if (validSource ==
null)
352 throw new NotSupportedException();
353 var propertyList = validSource.GetManagedProperties();
354 foreach (var p
in propertyList)
355 target[nameMapper(p.Name)] = validSource.ReadProperty(p);
369 public static void SetPropertyValue(
370 object target,
string propertyName,
object value)
372 DynamicMemberHandle handle = MethodCaller.GetCachedProperty(target.GetType(), propertyName);
373 SetValueWithCoercion(target, handle, value);
376 private static void SetValueWithCoercion(
object target, DynamicMemberHandle handle,
object value)
378 var oldValue = handle.DynamicMemberGet(target);
380 Type pType = handle.MemberType;
382 var isGeneric = pType.IsGenericType;
383 var isPrimitive = pType.IsPrimitive;
384 var isValueType = pType.IsValueType;
386 || (isGeneric && pType.GetGenericTypeDefinition() != typeof(Nullable<>)))
388 if (isValueType && (isPrimitive || pType == typeof(decimal)) && value ==
null)
396 Type vType = Utilities.GetPropertyType(value.GetType());
397 value = Utilities.CoerceValue(pType, vType, oldValue, value);
399 handle.DynamicMemberSet(target, value);
409 public static void SetFieldValue(
410 object target,
string fieldName,
object value)
412 DynamicMemberHandle handle = MethodCaller.GetCachedField(target.GetType(), fieldName);
413 SetValueWithCoercion(target, handle, value);
422 public static object GetFieldValue(
423 object target,
string fieldName)
425 DynamicMemberHandle handle = MethodCaller.GetCachedField(target.GetType(), fieldName);
426 return handle.DynamicMemberGet.Invoke(target);
A strongly-typed resource class, for looking up localized strings, etc.
static string PropertyCopyFailed
Looks up a localized string similar to Property copy failed.