Dynamically Retrieve attributes and values through features

Source: Internet
Author: User

 

UsingSystem. Collections. Generic;
UsingSystem. Data. sqlclient;
UsingSystem. Data;
UsingSystem;

Namespace Npetshop. Domain
{
  /* **************************************** **********************************
*
* Function: product entity class
* Author: Li Weiwei
* Date: 2011-5-28
* Description: 11111111111111111111111111
**************************************** ********************************* */
[Serializable]
Public Class Productentity
{
# Region Attribute
///   <Summary>
/// Gets or sets the value of the ID field from the auto-incrementing column system. int32
/// Database Type length: int size: 4 precision: 10
///   </Summary>
[Column ( " ID " )]
Public System. int32 ID { Get ; Set ;}

///   <Summary>
/// Gets or sets the value of the Name field system. String
/// Database Type length: varchar size: 50
///   </Summary>
[Column ( " Name " )]
Public System. string name { Get ; Set ;}

///   <Summary>
/// Gets or sets the value of The USERID field system. int32
/// Database Type length: int size: 4 precision: 10
///   </Summary>
[Column ( " Userid " )]
Public System. int32 userid { Get ; Set ;}

# Endregion

# Region Constructor

///   <Summary>
/// Constructor
///   </Summary>
///   <Param name = "name"> Name </Param>
///   <Param name = "userid"> Userid </Param>
Public Productentity (system. string name, system. int32 userid)
{
This . Name = Name;
This . Userid = Userid;
}

///   <Summary>
/// Constructor
///   </Summary>
Public Productentity ()
{
This . Name =   Null ;
This . Userid =   0 ;
}
# Endregion
}
}

 

//Features

UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. text;
UsingSystem. Data;

Namespace Npetshop. Domain
{
///   <Summary>
/// Associate class attributes with columns in the database table.
///   </Summary>
[Attributeusage (attributetargets. Property)]
Public   Sealed   Class Columnattribute: system. Attribute
{
///   <Summary>
/// Obtains or sets the column name.
///   </Summary>
Public   String Columnname { Get ; Private   Set ;}

///   <Summary>
/// Obtains or sets the column title.
///   </Summary>
Public   String Columncaption { Get ; Private   Set ;}

///   <Summary>
/// Obtains or sets the column type.
///   </Summary>
Public Sqldbtype columntype { Get ; Private   Set ;}

///   <Summary>
/// Initializes a new instance of the magicstar. entity. columnattribute class.
///   </Summary>
///   <Param name = "columncaption"> Table or view title. </Param>
///   <Param name = "columnname"> The name of the table or view. </Param>
///   <Param name = "columntype"> Type. </Param>
Public Columnattribute ( String Columnname, String Columncaption, sqldbtype columntype)
{
Columnname = Columnname;
Columncaption = Columncaption;
Columntype = Columntype;
}

///   <Summary>
/// Initializes a new instance of the magicstar. entity. columnattribute class.
///   </Summary>
///   <Param name = "columncaption"> Table or view title. </Param>
///   <Param name = "columnname"> The name of the table or view. </Param>
///   <Param name = "columntype"> Type. </Param>
Public Columnattribute ( String Columnname)
{
Columnname = Columnname;
}
}
}

 

/// Implementation

Productentity _ roductentity =   New Productentity ();
_ Roductentity. ID =   1 ;
_ Roductentity. Name =   " 2 " ;

type = _ roductentity. getType ();
propertyinfo [] properties = type. getproperties ();

foreach (propertyinfo property in Properties)
{< br> response. write (type. getproperty (property. name ). getvalue (_ roductentity, null ));
response. write (property. name + " " );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.