示例#1
0
文件: Mapper.cs 项目: kdakan/DbMapper
        private string parseNestedPropertyNameFromMemberExpression <T>(Expression <Func <T, object> > memberExpression)
        {
            DB.throwIfNullOrEmpty <MappingException>(memberExpression, "Member expression");

            MemberExpression propertyMemberExpression = null;
            string           nestedPropertyName;

            if (memberExpression.Body is MemberExpression)
            {
                propertyMemberExpression = (memberExpression.Body as MemberExpression);
            }
            else if (memberExpression.Body is UnaryExpression)             // value typelar objecte cast edildiği için member expression, unary (cast) expression içerisinde yeralıyor
            {
                UnaryExpression unaryExpression = (memberExpression.Body as UnaryExpression);
                if (unaryExpression.Operand is MemberExpression)
                {
                    propertyMemberExpression = (unaryExpression.Operand as MemberExpression);
                }
            }

            DB.throwIfNullOrEmpty <MappingException>(propertyMemberExpression, "Property member expression");

            int firstDotPosition = propertyMemberExpression.ToString().IndexOf(".", StringComparison.Ordinal);

            nestedPropertyName = propertyMemberExpression.ToString().Substring(firstDotPosition + 1);
            PropertyInfo propertyInfo = (propertyMemberExpression.Member as PropertyInfo);

            DB.throwIfNullOrEmpty <MappingException>(propertyInfo, "Property info");

            //mesela DateTime.Now gibi birşey girilmişse entity tipinin bir nested property'si olmadığı için hata verecek
            if (typeof(T) != getOutermostTypeFromNestedMemberExpression(propertyMemberExpression))
            {
                throw new MappingException("Property is in not a direct or nested property of the entity type.");
            }

            entityMapping.NestedPropertyInfoCacheAtMapper.Add(nestedPropertyName, propertyInfo);
            PropertyGetter getter = PropertyGetterSetterFactory.CreateGetter(propertyInfo);

            entityMapping.NestedPropertyGetterCacheAtMapper.Add(nestedPropertyName, getter);
            PropertySetter setter = PropertyGetterSetterFactory.CreateSetter(propertyInfo);

            entityMapping.NestedPropertySetterCacheAtMapper.Add(nestedPropertyName, setter);

            entityMapping.GenerateRootTypePropertyNamePropertyInfoAndGetterSetterCacheEntryAtMapper(typeof(T), nestedPropertyName);

            return(nestedPropertyName);
        }