Example #1
0
 public ColumnInfo[] GetColumnInfos(Type type)
 {
     return(ReflectionUtils.GetFieldsAndPropertiesForClasses(type)
            .Where(x => !IsDictionaryType(x.DeclaringType))
            .Concat(ReflectionUtils.GetPrivatePropertiesForClasses(type)
                    .Where(x => ShouldIncludePrivateColumn(x, type)))
            .Select(x => GetColumnInfo(x, type))
            .ToArray());
 }
Example #2
0
        /// <summary>
        /// Creates a new property accessor.
        /// </summary>
        /// <param name="targetType">Target object type.</param>
        /// <param name="memberName">Property name.</param>
        public MemberAccessor(Type targetType, string memberName)
        {
            _targetType = targetType;
            MemberInfo memberInfo = ReflectionUtils.GetFieldsAndPropertiesForClasses(targetType)
                                    .Concat(ReflectionUtils.GetPrivatePropertiesForClasses(targetType))
                                    .First(x => x.Name == memberName);

            if (memberInfo == null)
            {
                throw new Exception(string.Format("Property \"{0}\" does not exist for type " + "{1}.", memberName, targetType));
            }

            var canRead  = memberInfo.IsField() || ((PropertyInfo)memberInfo).CanRead;
            var canWrite = memberInfo.IsField() || ((PropertyInfo)memberInfo).CanWrite;

            // roslyn automatically implemented properties, in particular for get-only properties: <{Name}>k__BackingField;
            if (!canWrite)
            {
                var backingFieldName       = $"<{memberName}>k__BackingField";
                var backingFieldMemberInfo = targetType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(x => x.Name == backingFieldName);
                if (backingFieldMemberInfo != null)
                {
                    memberInfo = backingFieldMemberInfo;
                    canWrite   = true;
                }
            }

            _memberType = memberInfo.GetMemberInfoType();
            _member     = memberInfo;

            if (canWrite)
            {
                SetDelegate = GetSetDelegate();
            }

            if (canRead)
            {
                GetDelegate = GetGetDelegate();
            }
        }