属性的反射

属性的反射
(( System.Web.UI.WebControls.WebParts.WebDisplayNameAttribute)(pty[i].GetCustomAttributes(true)[0])).DisplayName + "(" + pty[i].Name + "):"       //实现反射的代码

  PropertyInfo[] pty = tp.GetProperties();//获得对象所有属性列表


表示层代码

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

/// <summary>
/// MCategory 的摘要说明
/// </summary>

[Serializable] 
public    sealed   class  MCategory
{
    
private fields(Total number 2)

    
public properties

    
public MCategory()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }

    
/// <summary>
    
/// Total field number 2
    
/// </summary>

    public MCategory(int _categoryid, string _categoryname, string _categorycolor)
    
{
        
this._categoryid = _categoryid;
        
this._categoryname = _categoryname;
        
this._categoryColor = _categorycolor;
    }

}


aspx 页面 代码
using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Threading;
using  System.Drawing;
using  System.Reflection;
using  System.Data.SqlClient;
using  System.Collections.Generic;


public  partial  class  Base_Category : BaseWebPage
{
    
/// <summary>
    
/// 当前用户对象
    
/// </summary>

    private MembershipUser mbUser;
    
int lastErrorID = -1;

    
protected void GVCateGory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
try
        
{
            List
<MCategory> acc = new List<MCategory>();
            
string strSQL = "select * from Law_Category where CategoryID = '" + this.GVCateGory.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlDataReader reader 
= SqlHelper.ExecuteReader(SqlHelper.ConnectionStringProfile, CommandType.Text, strSQL, null);
            
if (reader.Read())
            
{
                acc.Add(
new MCategory(reader.GetInt32(0),
                    reader.GetString(
1),
                    reader.GetString(
2)));
            }


            
if (acc.Count > 0)
            
{
                Session[
"MCategory"= acc[0];
            }

            MCategory mac 
= (MCategory)Session["MCategory"];
            opdetails 
= CompareMOject("CategoryID", mac, typeof(MCategory));

            CategoryInfo.DelLaw_Category(Int32.Parse(
this.GVCateGory.DataKeys[e.RowIndex].Value.ToString()));

            HistoryInfo.AddLaw_History(
new MHistory(-1, Convert.ToInt32(this.GVCateGory.DataKeys[e.RowIndex].Value.ToString()), "企业类型", opdetails, User.Identity.Name, "1.0"true, HistoryInfo.BaseTabelNum.Law_Category.ToString(), DateTime.Now, "企业类型删除并记录变更历史"));

            AspNetPager1.RecordCount 
= CategoryInfo.GetCategoryCount();
            GVCateGory.DataSource 
= CategoryInfo.GetPagedLaw_Category(this.AspNetPager1.StartRecordIndex, this.AspNetPager1.PageSize);
            GVCateGory.DataBind();
        }

        
catch (Exception ex)
        
{
            LogInfo.AddLaw_Log(
new MLog(Page.User.Identity.Name,
                              LogInfo.LogDoType.Delete,
                              DateTime.Now,
                              
"数据删除失败!" + ex.Message,
                              Request.UserHostAddress,
                              Request.RawUrl,
                              
false), ref lastErrorID);
            Session[User.Identity.Name] 
= lastErrorID;//保存最近的错误编号
            Response.Redirect("/_layouts/GSEGC/Errors.aspx");
        }

    }



    
protected string CompareMOject(string id, Object first, Type tp)
    
{
        
try
        
{
            PropertyInfo[] pty 
= tp.GetProperties();//获得对象所有属性列表
            for (int i = 0; i < pty.Length; i++)
            
{
                opdetails 
+= "<br/>&nbsp;&nbsp;&nbsp;" + ((System.Web.UI.WebControls.WebParts.WebDisplayNameAttribute)(pty[i].GetCustomAttributes(true)[0])).DisplayName + "(" + pty[i].Name + "):" + pty[i].GetValue(first, null).ToString();
            }

            
if (opdetails.Length > 0)
                opdetails.Substring(
4);//去掉首字母的<br/>
            return opdetails;
        }

        
catch (Exception)
        
{
            
return "";
        }

    }


}

你可能感兴趣的:(反射)