Beispiel #1
0
 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();
 }
Beispiel #2
0
        /// <summary>
        /// Get object property or field value
        /// </summary>
        /// <param name="source">Object source</param>
        /// <param name="name">Property or field name</param>
        /// <returns>Property or field value</returns>
        public static object GetMemberObjectValue(this object source, string name)
        {
            if (source == null)
            {
                return(null);
            }
            var sourceType             = source.GetType();
            GetAccessorDelegate getter = null;
            var key = string.Format("{0}.{1}", sourceType.AssemblyQualifiedName, name);

            if (GetterCache.ContainsKey(key))
            {
                getter = GetterCache[key];
            }
            else
            {
                if (source is IDictionary sDictionary)
                {
                    return(sDictionary.Contains(name) ? sDictionary[name] : null);
                }

                var prop = sourceType.GetRuntimeProperty(name);
                if (prop != null && prop.CanRead)
                {
                    getter = Factory.Accessors.BuildGetAccessor(prop);
                }

                if (getter == null)
                {
                    var field = sourceType.GetRuntimeField(name);
                    if (field != null)
                    {
                        getter = obj => field.GetValue(obj);
                    }
                }

                GetterCache[key] = getter;
            }
            return(getter?.Invoke(source));
        }