Пример #1
0
        /// <summary>
        /// 访问表示方法调用的节点
        /// </summary>
        /// <param name="node">方法调用节点</param>
        /// <returns></returns>
        protected Expression VisitMethodCall(BinaryExpression node)
        {
            string methodName = string.Empty;

            if (node.NodeType == ExpressionType.Modulo)
            {
                methodName = "Modulo";
            }
            else if (node.NodeType == ExpressionType.Divide)
            {
                methodName = "Divide";
            }
            else
            {
                methodName = node.Method.Name;
            }

            MemberInvokerBase invoker = this.TypeRuntime.GetInvoker("Visit" + methodName);

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} is not supported.", node.Method.DeclaringType, node.Method.Name);
            }
            else
            {
                object exp = invoker.Invoke(this, new object[] { node });
                return(exp as Expression);
            }
        }
Пример #2
0
        /// <summary>
        /// 初始化成员包装器集合
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        protected virtual MemberInvokerCollection InitializeInvokers(Type type)
        {
            // fix issue#多线程下导致 FieldCount 不正确
            // 单个实例只初始化一次

            //Fixed issue#匿名类的属性不可写
            //匿名类:new{ClientId=a.ClientId}

            if (!_isInitialize)
            {
                lock (_lock)
                {
                    if (!_isInitialize)
                    {
                        var invokers   = new MemberInvokerCollection();
                        var collection = this.GetMembers(type, _private).Select(x => MemberInvokerBase.Create(x));

                        foreach (MemberInvokerBase invoker in collection)
                        {
                            if (invoker.GetCustomAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute>() != null)
                            {
                                continue;
                            }

                            // 判断当前成员是否重复
                            if (invokers.Contains(invoker.Name))
                            {
                                // 属性和字段不允许重复
                                if (invoker.Member.MemberType != MemberTypes.Method)
                                {
                                    continue;
                                }
                                else
                                {
                                    // 方法成员考虑到有重载的情况,允许重复
                                    int dup = invokers.Count(x => x.Member.Name == invoker.Name);
                                    invoker.Name = string.Format("{0}{1}", invoker.Name, dup);
                                }
                            }
                            // 添加成员
                            invokers.Add(invoker);

                            // 累计数据字段,即与数据库一一对应的字段
                            bool isDataField = !(invoker.Column != null && invoker.Column.NoMapped || invoker.ForeignKey != null || invoker.Member.MemberType == MemberTypes.Method);
                            if (isDataField)
                            {
                                _dataFieldCount += 1;
                            }
                        }


                        _invokers     = invokers;
                        _isInitialize = true;
                    }
                }
            }


            return(_invokers);
        }
Пример #3
0
        /// <summary>
        /// 获取成员反射器
        /// </summary>
        /// <param name="memberName">成员名称</param>
        /// <returns></returns>
        public MemberInvokerBase GetInvoker(string memberName)
        {
            MemberInvokerBase invoker = null;

            this.Invokers.TryGetValue(memberName, out invoker);

            return(invoker);
        }
Пример #4
0
        /// <summary>
        /// 访问成员
        /// </summary>
        /// <param name="target">实例</param>
        /// <param name="memberName">成员名称</param>
        /// <param name="parameters">参数列表</param>
        /// <returns></returns>
        public object Invoke(object target, string memberName, params object[] parameters)
        {
            MemberInvokerBase invoker = null;

            this.Invokers.TryGetValue(memberName, out invoker);

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} Not Found.", _type.Name, memberName);
            }
            return(invoker.Invoke(target, parameters));
        }
Пример #5
0
        /// <summary>
        /// 访问表示字段或者属性的属性的节点 a.Name.Length
        /// </summary>
        protected Expression VisitMemberMember(MemberExpression node)
        {
            MemberInvokerBase invoker = this.TypeRuntime.GetInvoker("Visit" + node.Member.Name);

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} is not supported.", node.Member.DeclaringType, node.Member.Name);
            }
            else
            {
                object exp = invoker.Invoke(this, new object[] { node });
                return(exp as Expression);
            }
        }
Пример #6
0
        /// <summary>
        /// 访问表示方法调用的节点
        /// </summary>
        /// <param name="node">方法调用节点</param>
        /// <returns></returns>
        public virtual Expression VisitMethodCall(MethodCallExpression node)
        {
            if (_typeRuntime == null)
            {
                _typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(this.GetType(), true);
            }
            MemberInvokerBase invoker = _typeRuntime.GetInvoker("Visit" + node.Method.Name);

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} is not supported.", node.Method.DeclaringType, node.Method.Name);
            }
            else
            {
                object exp = invoker.Invoke(this, new object[] { node });
                return(exp as Expression);
            }
        }
Пример #7
0
        /// <summary>
        /// 初始化成员包装器集合
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        protected virtual Dictionary <string, MemberInvokerBase> InitializeInvoker(Type type)
        {
            // fix issue#多线程下导致 FieldCount 不正确
            // 单个实例只初始化一次

            //Fixed issue#匿名类的属性不可写
            //匿名类:new{ClientId=a.ClientId}

            if (!_isInitialize)
            {
                lock (_lock)
                {
                    if (!_isInitialize)
                    {
                        Dictionary <string, MemberInvokerBase> invokers = new Dictionary <string, MemberInvokerBase>();
                        var collection = this.GetMembers(type).Select(x => MemberInvokerBase.Create(x));

                        foreach (MemberInvokerBase invoker in collection)
                        {
                            if (invoker.GetCustomAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute>() != null)
                            {
                                continue;
                            }
                            if (!invokers.ContainsKey(invoker.Member.Name))
                            {
                                invokers.Add(invoker.Member.Name, invoker);
                            }
                            if (!(invoker.Column != null && invoker.Column.NoMapped || invoker.ForeignKey != null || invoker.Member.MemberType == MemberTypes.Method))
                            {
                                _dataFieldCount += 1;
                            }
                        }


                        _invokers     = invokers;
                        _isInitialize = true;
                    }
                }
            }


            return(_invokers);
        }
Пример #8
0
        /// <summary>
        /// 创建成员反射器
        /// </summary>
        /// <param name="member">元数据</param>
        /// <returns></returns>
        public static MemberInvokerBase Create(MemberInfo member)
        {
            MemberInvokerBase invoker = null;

            if (member.MemberType == MemberTypes.Property)
            {
                invoker = new PropertyInvoker((PropertyInfo)member);
            }
            if (member.MemberType == MemberTypes.Field)
            {
                invoker = new FieldInvoker((FieldInfo)member);
            }
            if (member.MemberType == MemberTypes.Method)
            {
                invoker = new MethodInvoker((MethodInfo)member);
            }
            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} not supported", member.ReflectedType, member.Name);
            }
            return(invoker);
        }
Пример #9
0
        /// <summary>
        /// 访问表示方法调用的节点
        /// </summary>
        /// <param name="node">方法调用节点</param>
        /// <returns></returns>
        protected Expression VisitMethodCall(MethodCallExpression node)
        {
            MemberInvokerBase invoker = null;

            if (node.Method.Name == "Concat")
            {
                invoker = this.TypeRuntime.GetMethod("Visit" + node.Method.Name, new[] { typeof(MethodCallExpression) });
            }
            else
            {
                invoker = this.TypeRuntime.GetInvoker("Visit" + node.Method.Name);
            }

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} is not supported.", node.Method.DeclaringType, node.Method.Name);
            }
            else
            {
                object exp = invoker.Invoke(this, new object[] { node });
                return(exp as Expression);
            }
        }
Пример #10
0
        /// <summary>
        /// 获取成员包装器的自定义特性。
        /// </summary>
        /// <typeparam name="TAttribute">自定义特性</typeparam>
        /// <returns></returns>
        public TAttribute GetInvokerAttribute <TAttribute>(string memberName) where TAttribute : Attribute
        {
            MemberInvokerBase invoker = this.GetInvoker(memberName);

            return(invoker != null?invoker.GetCustomAttribute <TAttribute>() : null);
        }
Пример #11
0
        // 反序列化导航属性
        // @prevLine 前一行数据
        // @isLine   是否同一行数据<同一父级>
        void Deserialize_Navigation(object prevModel, object model, string typeName, bool isThisLine)
        {
            // CRM_SaleOrder.Client
            // CRM_SaleOrder.Client.AccountList
            Type            type        = model.GetType();
            TypeRuntimeInfo typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(type);

            if (string.IsNullOrEmpty(typeName))
            {
                typeName = type.Name;
            }

            foreach (var kvp in _map.Navigations)
            {
                int start      = -1;
                int end        = -1;
                var navigation = kvp.Value;
                if (navigation.FieldCount > 0)
                {
                    start = navigation.Start;
                    end   = navigation.Start + navigation.FieldCount;
                }

                string keyName = typeName + "." + navigation.Name;
                if (keyName != kvp.Key)
                {
                    continue;
                }

                var navInvoker = typeRuntime.GetInvoker(navigation.Name);
                if (navInvoker == null)
                {
                    continue;
                }

                Type navType = navInvoker.DataType;
                Func <IDataRecord, object> deserializer   = null;
                TypeRuntimeInfo            navTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navType);
                object navCollection = null;
                //if (navType.IsGenericType && navTypeRuntime.GenericTypeDefinition == typeof(List<>))
                if (TypeUtils.IsCollectionType(navType))
                {
                    // 1:n关系,导航属性为 List<T>
                    navCollection = navInvoker.Invoke(model);
                    if (navCollection == null)
                    {
                        // new 一个列表类型,如果导航属性定义为接口,则默认使用List<T>来实例化
                        TypeRuntimeInfo navTypeRuntime2 = navType.IsInterface
                            ? TypeRuntimeInfoCache.GetRuntimeInfo(typeof(List <>).MakeGenericType(navTypeRuntime.GenericArguments[0]))
                            : navTypeRuntime;
                        navCollection = navTypeRuntime2.ConstructInvoker.Invoke();
                        navInvoker.Invoke(model, navCollection);
                    }
                }

                if (!_deserializers.TryGetValue(keyName, out deserializer))
                {
                    deserializer            = _deserializerImpl.GetTypeDeserializer(navType.IsGenericType ? navTypeRuntime.GenericArguments[0] : navType, _reader, _map.PickColumns, start, end);
                    _deserializers[keyName] = deserializer;
                }

                // 如果整个导航链中某一个导航属性为空,则跳出递归
                object navModel = deserializer(_reader);
                if (navModel != null)
                {
                    if (navCollection == null)
                    {
                        // 非集合型导航属性
                        navInvoker.Invoke(model, navModel);
                        //
                        //
                        //
                    }
                    else
                    {
                        // 集合型导航属性
                        if (prevModel != null && isThisLine)
                        {
                            #region 合并列表

                            // 判断如果属于同一个主表,则合并到上一行的当前明细列表
                            // 例:CRM_SaleOrder.Client.AccountList
                            string[]          keys           = keyName.Split('.');
                            TypeRuntimeInfo   curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>();
                            Type              curType        = curTypeRuntime.Type;
                            MemberInvokerBase curInvoker     = null;
                            object            curModel       = prevModel;

                            for (int i = 1; i < keys.Length; i++)
                            {
                                curInvoker = curTypeRuntime.GetInvoker(keys[i]);
                                curModel   = curInvoker.Invoke(curModel);
                                if (curModel == null)
                                {
                                    continue;
                                }

                                curType        = curModel.GetType();
                                curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(curType);

                                // <<<<<<<<<<< 一对多对多关系 >>>>>>>>>>
                                if (curType.IsGenericType && i != keys.Length - 1)
                                {
                                    curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(curType);
                                    //if (curTypeRuntime.GenericTypeDefinition == typeof(List<>))
                                    if (TypeUtils.IsCollectionType(curType))
                                    {
                                        var invoker = curTypeRuntime.GetInvoker("get_Count");
                                        int count   = Convert.ToInt32(invoker.Invoke(curModel));    // List.Count
                                        if (count > 0)
                                        {
                                            var invoker2 = curTypeRuntime.GetInvoker("get_Item");
                                            curModel       = invoker2.Invoke(curModel, count - 1);  // List[List.Count-1]
                                            curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(curModel.GetType());
                                        }
                                        else
                                        {
                                            // user.Roles.RoleFuncs=>Roles 列表有可能为空
                                            curModel = null;
                                            break;
                                        }
                                    }
                                }
                            }


                            if (curModel != null)
                            {
                                // 如果有两个以上的一对多关系的导航属性,那么在加入列表之前就需要剔除重复的实体


                                bool isAny = false;
                                if (_map.Navigations.Count > 1)
                                {
                                    if (_manyNavigationNumber == null)
                                    {
                                        _manyNavigationNumber = _map.Navigations.Count(x => IsHasMany(x.Value.Member));
                                    }
                                    if (_manyNavigationNumber != null && _manyNavigationNumber.Value > 1)
                                    {
                                        if (!_manyNavigationKeys.ContainsKey(keyName))
                                        {
                                            _manyNavigationKeys[keyName] = new HashSet <string>();
                                        }
                                        curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navModel.GetType());
                                        StringBuilder keyBuilder = new StringBuilder(64);

                                        foreach (var invoker in curTypeRuntime.KeyInvokers)
                                        {
                                            var value = invoker.Invoke(navModel);
                                            keyBuilder.AppendFormat("{0}={1};", invoker.Name, (value ?? string.Empty).ToString());
                                        }
                                        string hash = keyBuilder.ToString();
                                        if (_manyNavigationKeys[keyName].Contains(hash))
                                        {
                                            isAny = true;
                                        }
                                        else
                                        {
                                            _manyNavigationKeys[keyName].Add(hash);
                                        }
                                    }
                                }

                                if (!isAny)
                                {
                                    // 如果列表中不存在,则添加到上一行的相同导航列表中去
                                    var myAddMethod = navTypeRuntime.GetInvoker("Add");
                                    myAddMethod.Invoke(curModel, navModel);
                                }
                            }

                            #endregion
                        }
                        else
                        {
                            // 此时的 navTypeRuntime 是 List<> 类型的运行时
                            // 先添加 List 列表
                            var myAddMethod = navTypeRuntime.GetInvoker("Add");
                            myAddMethod.Invoke(navCollection, navModel);

                            var           curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navModel.GetType());
                            StringBuilder keyBuilder     = new StringBuilder(64);

                            foreach (var invoker in curTypeRuntime.KeyInvokers)
                            {
                                var value = invoker.Invoke(navModel);
                                keyBuilder.AppendFormat("{0}={1};", invoker.Name, (value ?? string.Empty).ToString());
                            }
                            string hash = keyBuilder.ToString();
                            if (!_manyNavigationKeys.ContainsKey(keyName))
                            {
                                _manyNavigationKeys[keyName] = new HashSet <string>();
                            }
                            if (!_manyNavigationKeys[keyName].Contains(hash))
                            {
                                _manyNavigationKeys[keyName].Add(hash);
                            }
                        }
                    }

                    //if (navTypeRuntime.GenericTypeDefinition == typeof(List<>)) navTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navTypeRuntime.GenericArguments[0]);
                    if (TypeUtils.IsCollectionType(navTypeRuntime.Type))
                    {
                        navTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navTypeRuntime.GenericArguments[0]);
                    }
                    if (navTypeRuntime.NavInvokers.Count > 0)
                    {
                        Deserialize_Navigation(prevModel, navModel, keyName, isThisLine);
                    }
                }
            }
        }
Пример #12
0
        // 初始化自增列信息
        private void InitializeIdentity()
        {
            TypeRuntimeInfo typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>();

            _autoIncrement = typeRuntime.Invokers.FirstOrDefault(x => x.Column != null && x.Column.IsIdentity);
        }