Int32 GetSize() { var member = CurrentMember as ReflectMemberInfo; if (member != null) { // 获取FieldSizeAttribute特性 var att = AttributeX.GetCustomAttribute <FieldSizeAttribute>(member.Member, true); if (att != null) { // 如果指定了固定大小,直接返回 if (att.Size > 0 && String.IsNullOrEmpty(att.ReferenceName)) { return(att.Size); } // 如果指定了引用字段,则找引用字段所表示的长度 var size = att.GetReferenceSize(CurrentObject, member.Member); if (size >= 0) { return(size); } } } return(-1); }
/// <summary>读取</summary> /// <param name="reader"></param> /// <param name="value"></param> public static void ReadXml(XmlReader reader, Object value) { foreach (var item in GetProperties(value.GetType())) { if (!item.Property.CanRead) { continue; } if (AttributeX.GetCustomAttribute <XmlIgnoreAttribute>(item.Member, false) != null) { continue; } var v = reader.GetAttribute(item.Name); if (String.IsNullOrEmpty(v)) { continue; } if (item.Type == typeof(String[])) { var ss = v.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); item.SetValue(value, ss); } else { item.SetValue(value, TypeX.ChangeType(v, item.Type)); } } //reader.Skip(); }
protected PropertyInfo[] GetProperties() { PropertyInfo[] pis = Config.GetType().GetProperties(); List <PropertyInfo> list = new List <PropertyInfo>(); foreach (PropertyInfo item in pis) { if (AttributeX.GetCustomAttribute <XmlIgnoreAttribute>(item, true) == null) { list.Add(item); } } return(list.ToArray()); }
static String GetCustomAttributeValue <TAttribute>(MemberInfo member, String name) { TAttribute att = AttributeX.GetCustomAttribute <TAttribute>(member, true); if (att == null) { return(null); } PropertyInfoX pix = PropertyInfoX.Create(typeof(TAttribute), name); if (pix == null) { return(null); } return((String)pix.GetValue(att)); }
/// <summary>读取大小</summary> /// <returns></returns> protected override Int32 OnReadSize() { var member = CurrentMember as ReflectMemberInfo; if (member != null) { // 获取FieldSizeAttribute特性 var att = AttributeX.GetCustomAttribute <FieldSizeAttribute>(member.Member, true); if (att != null) { // 如果指定了固定大小,直接返回 if (att.Size > 0 && String.IsNullOrEmpty(att.ReferenceName)) { return(att.Size); } // 如果指定了引用字段,则找引用字段所表示的长度d Int32 size = att.GetReferenceSize(CurrentObject, member.Member); if (size >= 0) { return(size); } } } switch (Settings.SizeFormat) { case TypeCode.Int16: return(ReadInt16()); case TypeCode.UInt16: return(ReadEncodedInt16()); case TypeCode.Int32: case TypeCode.Int64: default: return(ReadInt32()); case TypeCode.UInt32: case TypeCode.UInt64: return(ReadEncodedInt32()); } }
Boolean HasFieldSize() { var member = CurrentMember as ReflectMemberInfo; if (member != null) { // 获取FieldSizeAttribute特性 var att = AttributeX.GetCustomAttribute <FieldSizeAttribute>(member.Member, true); if (att != null) { // 如果指定了固定大小或者引用字段,直接返回 if (att.Size > 0 || !String.IsNullOrEmpty(att.ReferenceName)) { return(true); } } } return(false); }
/// <summary>写入大小</summary> /// <param name="size"></param> protected override void OnWriteSize(Int32 size) { var member = CurrentMember as ReflectMemberInfo; if (member != null) { // 获取FieldSizeAttribute特性 var att = AttributeX.GetCustomAttribute <FieldSizeAttribute>(member.Member, true); if (att != null) { // 如果指定了固定大小或者引用字段,直接返回 if (att.Size > 0 || !String.IsNullOrEmpty(att.ReferenceName)) { return; } } } switch (Settings.SizeFormat) { case TypeCode.Int16: Write((Int16)size); break; case TypeCode.UInt16: WriteEncoded((Int16)size); break; case TypeCode.Int32: case TypeCode.Int64: default: Write(size); break; case TypeCode.UInt32: case TypeCode.UInt64: WriteEncoded(size); break; } }
static String GetCustomAttributeValue <TAttribute>(MemberInfo member, String name) { var att = AttributeX.GetCustomAttribute <TAttribute>(member, true); if (att == null) { return(null); } //var pix = PropertyInfoX.Create(typeof(TAttribute), name); //if (pix == null) return null; //return (String)pix.GetValue(att); Object value = null; if (att.TryGetValue(name, out value)) { return((String)value); } return(null); }
/// <summary>获取需要序列化的成员(属性或字段)</summary> /// <param name="type">指定类型</param> /// <param name="value">对象</param> /// <returns>需要序列化的成员</returns> protected override IObjectMemberInfo[] OnGetMembers(Type type, object value) { var ms = base.OnGetMembers(type, value); if (ms != null && value != null) { // 遍历成员,寻找FieldSizeAttribute特性,重新设定大小字段的值 foreach (var item in ms) { var member = item as ReflectMemberInfo; if (member != null) { // 获取FieldSizeAttribute特性 var att = AttributeX.GetCustomAttribute <FieldSizeAttribute>(member.Member, true); if (att != null) { att.SetReferenceSize(value, member.Member, Settings.Encoding); } } } } return(ms); }
/// <summary>XML反序列化为实体</summary> /// <param name="xml"></param> /// <returns></returns> public static TEntity FromXml(String xml) { var doc = new XmlDocument(); doc.LoadXml(xml); TEntity entity = new TEntity(); //TypeX tx = TypeX.Create(typeof(TEntity)); foreach (var item in typeof(TEntity).GetProperties(true)) { //if (AttributeX.GetCustomAttribute<XmlIgnoreAttribute>(item, true) != null) continue; var elmName = item.Name; var att = AttributeX.GetCustomAttribute <XmlElementAttribute>(item, true); if (att != null && !String.IsNullOrEmpty(att.ElementName)) { elmName = att.ElementName; } var node = doc.SelectSingleNode("//" + elmName); if (node == null) { continue; } //item.SetValue(entity, Convert.ChangeType(node.InnerText, item.PropertyType)); entity.SetValue(item, Reflect.ChangeType(node.InnerText, item.PropertyType)); } return(entity); //XmlSerializer serial = new XmlSerializer(typeof(TEntity)); //using (StringReader reader = new StringReader(xml)) //{ // return serial.Deserialize(reader) as TEntity; //} }
/// <summary>获取实体类的描述名</summary> /// <param name="type">类型</param> /// <returns></returns> static String GetDescription(Type type) { return(desCache.GetItem(type, delegate(Type key) { if (!typeof(IEntity).IsAssignableFrom(key)) { return null; } var att = AttributeX.GetCustomAttribute <BindColumnAttribute>(key, true); if (att != null && !String.IsNullOrEmpty(att.Description)) { return att.Description; } var att2 = AttributeX.GetCustomAttribute <DescriptionAttribute>(key, true); if (att2 != null && !String.IsNullOrEmpty(att2.Description)) { return att2.Description; } return null; })); }
public override void Execute() { #line 8 "..\..\Areas\Admin\Views\Index\Main.cshtml" Layout = NewLife.Cube.Setting.Current.Layout; ViewBag.Title = "服务器信息"; var asm = Assembly.GetExecutingAssembly(); var att = AttributeX.GetCustomAttribute <TargetFrameworkAttribute>(asm); var ver = att.FrameworkDisplayName ?? att.FrameworkName; var req = HttpContext.Current.Request; // GC设置 var gc = "IsServerGC={0},LatencyMode={1}".F(GCSettings.IsServerGC, GCSettings.LatencyMode); #line default #line hidden WriteLiteral("\r\n<table"); WriteLiteral(" class=\"table table-bordered table-hover table-striped table-condensed\""); WriteLiteral(">\r\n <thead>\r\n <tr>\r\n <th"); WriteLiteral(" colspan=\"4\""); WriteLiteral(">\r\n 服务器信息\r\n </th>\r\n </tr>\r\n </thead>\r\n <tb" + "ody>\r\n <tr>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 应用系统:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 35 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(HttpRuntime.AppDomainAppVirtualPath); #line default #line hidden WriteLiteral(" \r\n"); #line 36 "..\..\Areas\Admin\Views\Index\Main.cshtml" #line default #line hidden #line 36 "..\..\Areas\Admin\Views\Index\Main.cshtml" if (this.Has((PermissionFlags)16)) { #line default #line hidden WriteLiteral(" <a"); WriteAttribute("href", Tuple.Create(" href=\"", 1116), Tuple.Create("\"", 1145) #line 38 "..\..\Areas\Admin\Views\Index\Main.cshtml" , Tuple.Create(Tuple.Create("", 1123), Tuple.Create <System.Object, System.Int32>(Url.Action("Restart") #line default #line hidden , 1123), false) ); WriteLiteral(" onclick=\"return confirm(\'仅重启ASP.Net应用程序域,而不是操作系统!\\n确认重启?\')\""); WriteLiteral(">重启应用系统</a>\r\n"); #line 39 "..\..\Areas\Admin\Views\Index\Main.cshtml" } #line default #line hidden WriteLiteral(" "); #line 40 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(req.GetRawUrl()); #line default #line hidden WriteLiteral("\r\n </td>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 目录:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 46 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(HttpRuntime.AppDomainAppPath); #line default #line hidden WriteLiteral("\r\n </td>\r\n </tr>\r\n <tr>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 域名地址:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n <span"); WriteLiteral(" title=\"SERVER_NAME\""); WriteLiteral(">"); #line 54 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Request.ServerVariables["SERVER_NAME"]); #line default #line hidden WriteLiteral("</span>,\r\n <span"); WriteLiteral(" title=\"LOCAl_ADDR\""); WriteLiteral(">"); #line 55 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Request.ServerVariables["LOCAl_ADDR"]); #line default #line hidden WriteLiteral(":"); #line 55 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Request.ServerVariables["Server_Port"]); #line default #line hidden WriteLiteral("</span>\r\n <span"); WriteLiteral(" title=\"REMOTE_HOST\""); WriteLiteral(">["); #line 56 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Request.ServerVariables["REMOTE_HOST"]); #line default #line hidden WriteLiteral("]</span>\r\n <a"); WriteAttribute("href", Tuple.Create(" href=\"", 1953), Tuple.Create("\"", 1989) #line 57 "..\..\Areas\Admin\Views\Index\Main.cshtml" , Tuple.Create(Tuple.Create("", 1960), Tuple.Create <System.Object, System.Int32>(Url.Action("Main/ServerVar") #line default #line hidden , 1960), false) ); WriteLiteral(" target=\"_blank\""); WriteLiteral(" title=\"点击打开服务器变量列表\""); WriteLiteral(">服务器变量列表</a>\r\n </td>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 计算机用户:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 63 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Environment.UserName); #line default #line hidden WriteLiteral("/"); #line 63 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Environment.MachineName); #line default #line hidden WriteLiteral("\r\n </td>\r\n </tr>\r\n <tr>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 应用程序域:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 71 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(AppDomain.CurrentDomain.FriendlyName); #line default #line hidden WriteLiteral("\r\n <a"); WriteAttribute("href", Tuple.Create(" href=\"", 2456), Tuple.Create("\"", 2491) #line 72 "..\..\Areas\Admin\Views\Index\Main.cshtml" , Tuple.Create(Tuple.Create("", 2463), Tuple.Create <System.Object, System.Int32>(Url.Action("Main/Assembly") #line default #line hidden , 2463), false) ); WriteLiteral(" target=\"_blank\""); WriteLiteral(" title=\"点击打开进程程序集列表\""); WriteLiteral(">程序集列表</a>\r\n </td>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n .Net 版本:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 78 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Environment.Version); #line default #line hidden WriteLiteral(" "); #line 78 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(ver); #line default #line hidden WriteLiteral("\r\n </td>\r\n </tr>\r\n <tr>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 操作系统:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n "); WriteLiteral(" "); #line 86 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Environment.OSVersion); #line default #line hidden WriteLiteral("\r\n </td>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n Web服务器:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 92 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(ViewBag.WebServerName); #line default #line hidden WriteLiteral("\r\n </td>\r\n </tr>\r\n <tr>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 处理器:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 100 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Environment.ProcessorCount); #line default #line hidden WriteLiteral("\r\n 核心,"); #line 101 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(gc); #line default #line hidden WriteLiteral("\r\n </td>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 时间:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(" title=\"这里使用了服务器默认的时间格式!后面是开机时间。\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 107 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(DateTime.Now); #line default #line hidden WriteLiteral(",开机"); #line 107 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(new TimeSpan(0, 0, 0, 0, Environment.TickCount)); #line default #line hidden WriteLiteral("\r\n </td>\r\n </tr>\r\n <tr>\r\n"); #line 111 "..\..\Areas\Admin\Views\Index\Main.cshtml" #line default #line hidden #line 111 "..\..\Areas\Admin\Views\Index\Main.cshtml" var process = Process.GetCurrentProcess(); #line default #line hidden WriteLiteral("\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 内存:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n "); WriteLiteral("\r\n 工作集: "); #line 117 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write((process.WorkingSet64 / 1024 / 1024).ToString("n0")); #line default #line hidden WriteLiteral("M\r\n 提交: "); #line 118 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write((process.PrivateMemorySize64 / 1024 / 1024).ToString("n0")); #line default #line hidden WriteLiteral("M\r\n GC: "); #line 119 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write((GC.GetTotalMemory(false) / 1024 / 1024).ToString("n0")); #line default #line hidden WriteLiteral("M\r\n <a"); WriteAttribute("href", Tuple.Create(" href=\"", 4140), Tuple.Create("\"", 4181) #line 120 "..\..\Areas\Admin\Views\Index\Main.cshtml" , Tuple.Create(Tuple.Create("", 4147), Tuple.Create <System.Object, System.Int32>(Url.Action("Main/ProcessModules") #line default #line hidden , 4147), false) ); WriteLiteral(" target=\"_blank\""); WriteLiteral(" title=\"点击打开进程模块列表\""); WriteLiteral(">模块列表</a>\r\n <a"); WriteAttribute("href", Tuple.Create(" href=\"", 4246), Tuple.Create("\"", 4278) #line 121 "..\..\Areas\Admin\Views\Index\Main.cshtml" , Tuple.Create(Tuple.Create("", 4253), Tuple.Create <System.Object, System.Int32>(Url.Action("MemoryFree") #line default #line hidden , 4253), false) ); WriteLiteral(" title=\"点击释放进程内存\""); WriteLiteral(">释放内存</a>\r\n </td>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n 进程时间:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 127 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(process.TotalProcessorTime.TotalSeconds.ToString("N2")); #line default #line hidden WriteLiteral("秒 启动于 "); #line 127 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(process.StartTime.ToFullString()); #line default #line hidden WriteLiteral("\r\n </td>\r\n </tr>\r\n <tr>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n Session:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 135 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Session.Contents.Count); #line default #line hidden WriteLiteral(" 个,"); #line 135 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Session.Timeout); #line default #line hidden WriteLiteral(" 分钟,SessionID:"); #line 135 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Session.Contents.SessionID); #line default #line hidden WriteLiteral("\r\n <a"); WriteAttribute("href", Tuple.Create(" href=\"", 4818), Tuple.Create("\"", 4852) #line 136 "..\..\Areas\Admin\Views\Index\Main.cshtml" , Tuple.Create(Tuple.Create("", 4825), Tuple.Create <System.Object, System.Int32>(Url.Action("Main/Session") #line default #line hidden , 4825), false) ); WriteLiteral(" target=\"_blank\""); WriteLiteral(" title=\"点击打开Session列表\""); WriteLiteral(">Session列表</a>\r\n </td>\r\n <td"); WriteLiteral(" class=\"name\""); WriteLiteral(">\r\n Cache:\r\n </td>\r\n <td"); WriteLiteral(" class=\"value\""); WriteLiteral(">\r\n"); WriteLiteral(" "); #line 142 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(Cache.Count); #line default #line hidden WriteLiteral(" 个,可用:"); #line 142 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write((Cache.EffectivePrivateBytesLimit / 1024).ToString("n0")); #line default #line hidden WriteLiteral("KB\r\n <a"); WriteAttribute("href", Tuple.Create(" href=\"", 5147), Tuple.Create("\"", 5179) #line 143 "..\..\Areas\Admin\Views\Index\Main.cshtml" , Tuple.Create(Tuple.Create("", 5154), Tuple.Create <System.Object, System.Int32>(Url.Action("Main/Cache") #line default #line hidden , 5154), false) ); WriteLiteral(" target=\"_blank\""); WriteLiteral(" title=\"点击打开Cache列表\""); WriteLiteral(">Cache列表</a>\r\n </td>\r\n </tr>\r\n </tbody>\r\n</table>\r\n<table"); WriteLiteral(" class=\"table table-bordered table-hover table-striped table-condensed\""); WriteLiteral(">\r\n <thead>\r\n <tr>\r\n <th>名称</th>\r\n <th>标题</th>\r\n " + " <th>文件版本</th>\r\n <th>内部版本</th>\r\n <th>编译时间</th>\r\n" + " <th>描述</th>\r\n </tr>\r\n </thead>\r\n <tbody>\r\n"); #line 160 "..\..\Areas\Admin\Views\Index\Main.cshtml" #line default #line hidden #line 160 "..\..\Areas\Admin\Views\Index\Main.cshtml" foreach (AssemblyX item in ViewBag.MyAsms) { #line default #line hidden WriteLiteral(" <tr>\r\n <td>"); #line 163 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(item.Name); #line default #line hidden WriteLiteral("</td>\r\n <td>"); #line 164 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(item.Title); #line default #line hidden WriteLiteral("</td>\r\n <td>"); #line 165 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(item.FileVersion); #line default #line hidden WriteLiteral("</td>\r\n <td>"); #line 166 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(item.Version); #line default #line hidden WriteLiteral("</td>\r\n <td>"); #line 167 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(item.Compile.ToFullString()); #line default #line hidden WriteLiteral("</td>\r\n <td>"); #line 168 "..\..\Areas\Admin\Views\Index\Main.cshtml" Write(item.Description); #line default #line hidden WriteLiteral("</td>\r\n </tr>\r\n"); #line 170 "..\..\Areas\Admin\Views\Index\Main.cshtml" } #line default #line hidden WriteLiteral(" </tbody>\r\n</table>"); }
protected void gvTable_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { GridView gv = sender as GridView; Type type = null; if (gv.DataSource is DataSet || gv.DataSource is DataTable) { String sql = txtSql.Text; if (String.IsNullOrEmpty(sql)) { return; } sql = sql.Trim(); if (String.IsNullOrEmpty(sql)) { return; } DAL dal = GetDAL(); if (dal == null) { return; } String tableName = ddlTable.SelectedValue; String fsql = String.Format("Select * From {0}", dal.Db.FormatName(tableName)); if (!sql.ToLower().StartsWith(fsql.ToLower())) { return; } IDataTable table = dal.Tables.Find(delegate(IDataTable item) { return(item.TableName == ddlTable.SelectedValue); }); if (table == null) { return; } // 更新表头 foreach (TableCell item in e.Row.Cells) { String name = item.Text; if (String.IsNullOrEmpty(name)) { continue; } //XField field = table.Fields.Find(delegate(XField elm) { return elm.Name == name; }); //if (field == null) continue; IDataColumn field = null; foreach (IDataColumn elm in table.Columns) { if (elm.ColumnName == name) { field = elm; break; } } if (field == null) { continue; } if (!String.IsNullOrEmpty(field.Description)) { item.Text = field.Description; } } } else { IEnumerable ie = gv.DataSource as IEnumerable; if (ie == null) { return; } IEnumerator iet = ie.GetEnumerator(); if (!iet.MoveNext()) { return; } type = iet.Current.GetType(); // 更新表头 foreach (TableCell item in e.Row.Cells) { String name = item.Text; if (String.IsNullOrEmpty(name)) { continue; } PropertyInfo pi = type.GetProperty(name); if (pi == null) { continue; } DescriptionAttribute att = AttributeX.GetCustomAttribute <DescriptionAttribute>(pi, false); if (att == null) { continue; } if (!String.IsNullOrEmpty(att.Description)) { item.Text = att.Description; } } } } }
/// <summary>写入</summary> /// <param name="writer"></param> /// <param name="value"></param> /// <param name="writeDefaultValueMember">是否写数值为默认值的成员。为了节省空间,默认不写。</param> public static void WriteXml(XmlWriter writer, Object value, Boolean writeDefaultValueMember = false) { var type = value.GetType(); Object def = GetDefault(type); String name = null; // 基本类型,输出为特性 foreach (var item in GetProperties(type)) { if (!item.Property.CanWrite) { continue; } if (AttributeX.GetCustomAttribute <XmlIgnoreAttribute>(item.Member, false) != null) { continue; } var code = Type.GetTypeCode(item.Type); var obj = item.GetValue(value); // 默认值不参与序列化,节省空间 if (!writeDefaultValueMember) { var dobj = item.GetValue(def); if (Object.Equals(obj, dobj)) { continue; } if (code == TypeCode.String && "" + obj == "" + dobj) { continue; } } if (code == TypeCode.String) { // 如果别名与名称相同,则跳过 if (item.Name == "Name") { name = (String)obj; } else if (item.Name == "Alias") { if (name == (String)obj) { continue; } } } else if (code == TypeCode.Object) { if (item.Type.IsArray || typeof(IEnumerable).IsAssignableFrom(item.Type) || obj is IEnumerable) { var sb = new StringBuilder(); var arr = obj as IEnumerable; foreach (Object elm in arr) { if (sb.Length > 0) { sb.Append(","); } sb.Append(elm); } obj = sb.ToString(); } else if (item.Type == typeof(Type)) { obj = (obj as Type).Name; } else { // 其它的不支持,跳过 if (XTrace.Debug) { XTrace.WriteLine("不支持的类型[{0} {1}]!", item.Type.Name, item.Name); } continue; } //if (item.Type == typeof(Type)) obj = (obj as Type).Name; } writer.WriteAttributeString(item.Name, obj == null ? null : obj.ToString()); } }