public bool TrySet <TProperty>(TInstance instance, string propertyName, TProperty value) { var cache = _genericInstanceSettersCache; var setter = cache[propertyName] as Action <TInstance, TProperty>; if (setter is null) { if (!Properties.TryGetValue(propertyName, out var prop)) { return(false); } lock (cache) { setter = cache[propertyName] as Action <TInstance, TProperty>; if (setter is null) { try { setter = AccessorBuilder.BuildSetter <TInstance, TProperty>( prop.Name, IncludesNonPublic); cache[prop.Name] = setter; } catch (ArgumentException) { return(false); } } } } setter(instance, value); return(true); }
internal GenericAccessor(bool ignoreCase, bool includeNonPublic) : base(typeof(TInstance), ignoreCase, includeNonPublic) { _genericPropertiesGettersCache = new Hashtable(Properties.Count, Comparer); _genericPropertiesSettersCache = new Hashtable(Properties.Count, Comparer); _genericInstanceGettersCache = new Hashtable(Properties.Count, Comparer); _genericInstanceSettersCache = new Hashtable(Properties.Count, Comparer); foreach (var pair in Properties) { var propName = pair.Key; var prop = pair.Value; if (prop.CanRead) { _genericPropertiesGettersCache[propName] = AccessorBuilder.BuildGetter <TInstance>(prop, IncludesNonPublic); } if (prop.CanWrite) { _genericPropertiesSettersCache[propName] = AccessorBuilder.BuildSetter <TInstance>(prop, IncludesNonPublic); } } }
internal Accessor(IReflect type, bool ignoreCase, bool includeNonPublic) { Type = type; IgnoreCase = ignoreCase; IncludesNonPublic = includeNonPublic; Comparer = IgnoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; var flags = BindingFlags.Public | BindingFlags.Instance; if (IncludesNonPublic) { flags = flags | BindingFlags.NonPublic; } Properties = Type.GetProperties(flags); _objectGettersCache = new Dictionary <string, Func <object, object> >(Properties.Length, Comparer); _objectSettersCache = new Dictionary <string, Action <object, object> >(Properties.Length, Comparer); foreach (var prop in Properties) { var propName = prop.Name; _objectGettersCache[propName] = AccessorBuilder.BuildGetter(prop, IncludesNonPublic); _objectSettersCache[propName] = AccessorBuilder.BuildSetter(prop, IncludesNonPublic); } }
/// <summary> /// Attempts to set the value of the given <paramref name="propertyName"/> for the given <paramref name="instance"/>. /// </summary> public bool TrySet <TProperty>(TInstance instance, string propertyName, TProperty value) { var cache = _genericInstanceSettersCache; Action <TInstance, TProperty> setter; if (!cache.TryGetValue(propertyName, out object tmpSetter)) { lock (cache) { if (!cache.TryGetValue(propertyName, out object tmp)) { try { setter = AccessorBuilder.BuildSetter <TInstance, TProperty>(propertyName, IncludesNonPublic); cache[propertyName] = setter; } catch (ArgumentException) { return(false); } } else { setter = (Action <TInstance, TProperty>)tmp; } } } else { setter = (Action <TInstance, TProperty>)tmpSetter; } setter(instance, value); return(true); }
internal ObjectAccessor(IReflect type, bool ignoreCase, bool includeNonPublic) : base(type, ignoreCase, includeNonPublic) { _objectGettersCache = new Dictionary <string, Func <object, object> >(Properties.Length, Comparer); _objectSettersCache = new Dictionary <string, Action <object, object> >(Properties.Length, Comparer); foreach (var prop in Properties) { var propName = prop.Name; if (prop.CanRead) { _objectGettersCache[propName] = AccessorBuilder.BuildGetter(prop, IncludesNonPublic); } if (prop.CanWrite) { _objectSettersCache[propName] = AccessorBuilder.BuildSetter(prop, IncludesNonPublic); } } }
internal GenericAccessor(bool ignoreCase, bool includeNonPublic) : base(typeof(TInstance), ignoreCase, includeNonPublic) { _genericPropertiesGettersCache = new Dictionary <string, Func <TInstance, object> >(Comparer); _genericPropertiesSettersCache = new Dictionary <string, Action <TInstance, object> >(Comparer); _genericInstanceGettersCache = new Dictionary <string, object>(Comparer); _genericInstanceSettersCache = new Dictionary <string, object>(Comparer); foreach (var prop in Properties) { var propName = prop.Name; if (prop.CanRead) { _genericPropertiesGettersCache[propName] = AccessorBuilder.BuildGetter <TInstance>(prop, IncludesNonPublic); } if (prop.CanWrite) { _genericPropertiesSettersCache[propName] = AccessorBuilder.BuildSetter <TInstance>(prop, IncludesNonPublic); } } }
internal ObjectAccessor(IReflect type, bool ignoreCase, bool includeNonPublic) : base(type, ignoreCase, includeNonPublic) { _objectGettersCache = new Hashtable(Properties.Count, Comparer); _objectSettersCache = new Hashtable(Properties.Count, Comparer); foreach (var pair in Properties) { var propName = pair.Key; var prop = pair.Value; if (prop.CanRead) { _objectGettersCache[propName] = AccessorBuilder.BuildGetter(prop, IncludesNonPublic); } if (prop.CanWrite) { _objectSettersCache[propName] = AccessorBuilder.BuildSetter(prop, IncludesNonPublic); } } }
public bool TryGet <TProperty>(TInstance instance, string propertyName, out TProperty value) { var cache = _genericInstanceGettersCache; var getter = cache[propertyName] as Func <TInstance, TProperty>; if (getter is null) { if (!Properties.TryGetValue(propertyName, out var prop)) { value = default(TProperty); return(false); } lock (cache) { getter = cache[propertyName] as Func <TInstance, TProperty>; if (getter is null) { try { getter = AccessorBuilder.BuildGetter <TInstance, TProperty>( prop.Name, IncludesNonPublic); cache[prop.Name] = getter; } catch (ArgumentException) { value = default(TProperty); return(false); } } } } value = getter(instance); return(true); }
/// <summary> /// Attempts to get the value of a property selected by the given <paramref name="propertyName"/> /// for the given <paramref name="instance"/>. /// </summary> public bool TryGet <TProperty>(TInstance instance, string propertyName, out TProperty value) { var cache = _genericInstanceGettersCache; Func <TInstance, TProperty> getter; if (!cache.TryGetValue(propertyName, out object tmpGetter)) { lock (cache) { if (!cache.TryGetValue(propertyName, out object tmp)) { try { getter = AccessorBuilder.BuildGetter <TInstance, TProperty>(propertyName, IncludesNonPublic); cache[propertyName] = getter; } catch (ArgumentException) { value = default(TProperty); return(false); } } else { getter = (Func <TInstance, TProperty>)tmp; } } } else { getter = (Func <TInstance, TProperty>)tmpGetter; } value = getter(instance); return(true); }