private FastPropertyInfo(PropertyInfo prop) { Name = prop.Name; GetValue = GetAccessorCache.GetOrAdd(prop, GetDelegate); SetValue = SetAccessorCache.GetOrAdd(prop, SetDelegate); PropertyType = prop.PropertyType; PropertyTypeInfo = PropertyType.GetTypeInfo(); PropertyUnderlayingType = PropertyType.GetUnderlyingType(); PropertyUnderlayingTypeInfo = PropertyUnderlayingType.GetTypeInfo(); }
/// <summary> /// Set object property or field value /// </summary> /// <param name="source">Object source</param> /// <param name="name">Property or field name</param> /// <param name="value">Property or field value</param> public static void SetMemberObjectValue(this object source, string name, object value) { if (source == null) { return; } var sourceType = source.GetType(); if (source is IDictionary sDictio) { sDictio[name] = value; return; } SetAccessorDelegate setter = null; var key = string.Format("{0}.{1}", sourceType.AssemblyQualifiedName, name); if (SetterCache.ContainsKey(key)) { setter = SetterCache[key]; } else { var prop = sourceType.GetRuntimeProperty(name); if (prop != null && prop.CanWrite) { setter = Factory.Accessors.BuildSetAccessor(prop); } if (setter == null) { var field = sourceType.GetRuntimeField(name); if (field != null) { setter = (s, v) => field.SetValue(s, v); } } SetterCache[key] = setter; } setter?.Invoke(source, value); }