/// <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); }
// 选择所有的字段 private Expression VisitAllMember(Type type, string alias, Expression node = null) { if (_groupBy != null && node != null && node.IsGrouping()) { // select g.Key LambdaExpression keySelector = _groupBy.Expressions[0] as LambdaExpression; return(this.Visit(keySelector.Body)); } else { TypeRuntimeInfo typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(type); MemberInvokerCollection invokers = typeRuntime.Invokers; foreach (var invoker in invokers) { if (invoker != null && invoker.Column != null && invoker.Column.NoMapped) { continue; } if (invoker != null && invoker.ForeignKey != null) { continue; // 不加载导航属性 } if (invoker.Member.MemberType == System.Reflection.MemberTypes.Method) { continue; } _builder.AppendMember(alias, invoker.Member.Name); // 选择字段 string newName = _pickColumns.Add(invoker.Member.Name); _builder.AppendAs(newName); _builder.Append(","); _builder.AppendNewLine(); } } return(node); }