public void SetValue(object target, object value) { if (_inner != null) { _inner.SetValue(target, value); } if (_setter == null) { try { var fastProp = FastProperty.GetProperty(_pi, PropertyCachingStrategy.EagerCached); if (fastProp.IsPublicSettable) { _setter = fastProp.ValueSetter; } if (_setter == null) { throw new Exception(); } } catch { CreateInnerProvider(); _inner.SetValue(target, value); return; } } _setter(target, value); }
public object GetValue(object target) { if (_inner != null) { return(_inner.GetValue(target)); } if (_getter == null) { try { _getter = FastProperty.GetProperty(_pi, PropertyCachingStrategy.EagerCached).ValueGetter; if (_getter == null) { throw new Exception(); } } catch { CreateInnerProvider(); return(_inner.GetValue(target)); } } return(_getter(target)); }
private static FastProperty[] GetFastPropertiesFor(Type type) { return(FastProperty.GetProperties(type, PropertyCachingStrategy.Uncached) .Values .Where(pi => pi.IsPublicSettable) .ToArray()); }
private static bool TryGetCachedProperty( Type type, string propertyName, bool eagerCached, out FastProperty fastProperty) { fastProperty = null; IDictionary <string, FastProperty> allProperties; if (eagerCached) { allProperties = (IDictionary <string, FastProperty>)GetProperties(type); allProperties.TryGetValue(propertyName, out fastProperty); } if (fastProperty == null && _propertiesCache.TryGetValue(type, out allProperties)) { allProperties.TryGetValue(propertyName, out fastProperty); } return(fastProperty != null); }
private static void MapComplex(object from, object to, Type fromType, CultureInfo culture) { if (object.ReferenceEquals(from, to)) { // Cannot map same instance or null source return; } var toType = to.GetType(); var isEntityMapping = typeof(BaseEntity).IsAssignableFrom(toType); var toProps = GetFastPropertiesFor(toType).ToArray(); foreach (var toProp in toProps) { var fromProp = FastProperty.GetProperty(fromType, toProp.Name, PropertyCachingStrategy.Cached); if (fromProp == null) { continue; } var fromPropType = fromProp.Property.PropertyType; var toPropType = toProp.Property.PropertyType; var sourceValue = fromProp.GetValue(from); if (isEntityMapping && (fromPropType == typeof(int) || fromPropType == typeof(int?)) && toPropType == typeof(int?) && object.Equals(0, sourceValue) && toProp.Name.EndsWith("Id")) { // TODO: This is more a hack than a proper solution. Find a more generic way to convert int FK properties... // Special mapper, that avoids DbUpdate exceptions in cases where // optional (nullable) int FK properties are 0 instead of "null" // after mapping model > entity. toProp.SetValue(to, null); } else if (TryMap(sourceValue, fromPropType, toPropType, culture, out var targetValue)) { // Set it toProp.SetValue(to, targetValue); } } }
private IDictionary <string, FastProperty> GetInstanceProperties() { if (_instance == null) { return(EmptyProps); } if (_instanceProps == null) { var props = FastProperty.GetProperties(_instance.GetType()) as IDictionary <string, FastProperty>; if (_optMembers != null) { props = props .Where(x => _optMethod == MemberOptMethod.Allow ? _optMembers.Contains(x.Key) : !_optMembers.Contains(x.Key)) .ToDictionary(x => x.Key, x => x.Value); } _instanceProps = props; } return(_instanceProps); }