类似
[KeyField("UserName")]
public String UserName
{
get{return this._UserName;}
set{this._UserName=value;}
}
[DataField("UserPsw")]
public String UserPsw
{
get{return this._UserPsw;}
set{this._UserPsw=value;}
}
如何访问 UserName 是 [KeyField("UserName")] 而UserPsw 是[DataField("UserPsw")]

解决方案 »

  1.   

    ms-help://MS.MSDNQTR.2003FEB.2052/csref/html/vclrfRetrievingAttributeInformation.htm
      

  2.   

    是不是帮助里面的,我没有装,搜索也搜索不到,能不能发msdn上的连接
      

  3.   

    假设包含你的属性定义的类为Scan
    Type t = typeof(Scan);
    PropertyInfo info = t.GetProperty("UserName");Attribute[] attrs = (Attribute[])info.GetCustomAttributes(typeof(Attribute), false);
    for (int i = 0; i < attrs.Length; i++)
    {
       Console.WriteLine(attrs[i].ToString());
    }
      

  4.   

    static void Main()
    {
    foreach(PropertyInfo pi in typeof(Person).GetProperties())
    {
    Console.WriteLine(pi.Name);
    foreach(Attribute attr in pi.GetCustomAttributes(false))
    {
    if(attr is KeyFieldAttribute)
    {
    Console.WriteLine("\tKeyField");
    }
    else if(attr is DataFieldAttribute)
    {
    Console.WriteLine("\tDataField");
    }
    }
    }
    }
      

  5.   

    我不知道KeyField是从何而来,假设是自定义的,并且其实例初始化时的那个参数可以通过其实例的FieldName属性读出,那么应用它的代码类似于:Array.ForEach((PropertyInfo[])theType.GetProperties,delegate(PropertyInfo p){
      Attribute attr=System.Attribute.GetCusmtomAttribute(p,typeof(KeyField));
      if(attr!=null)
        ....//现在可以知道当前的p准确对应的数据库字段是attr.FieldName的值,可以对p进行处理
      

  6.   

    对于asp.net1.1的,可以这样看;foreach(PropertyInfo p in theType.GetProperties){
      Attribute attr=System.Attribute.GetCusmtomAttribute(p,typeof(KeyField));
     if(attr!=null)
        ....//现在可以知道当前的p准确对应的数据库字段是attr.FieldName的值,可以对p进行处理
      

  7.   

    GetProperties  -->  GetProperties()
      

  8.   

    我是通过grove的引用 using Grove.ORM 设置的,
    另外请问如何能够设置这种自定义的格式
      

  9.   

    我试试 GetProperties 好象无法获取哪个参数
    我使用
    Model.Game_News dd=new Model.Game_News();
    // Type t = dd.GetType();
    Type objType = dd.GetType();
    PropertyInfo[] objPropertiesArray = objType.GetProperties();
    foreach (PropertyInfo objProperty in objPropertiesArray) 
    {
    System.Web.HttpContext.Current.Response.Write(string.Format("{0}={1};type={2}<br>",objProperty.Attributes,objProperty.GetValue(dd,null),objProperty.PropertyType==typeof(int)?true:false));

    }
      

  10.   

    [KeyField("UserName")]
    请问这个是做什么用的阿,谢谢哦``