This is my opening speech. I hope you can join me more. Haha.
C # It's a few years later than Java. It's not in Java. Don't look at this little thing, it's just likeProgramThe same soul. Why is C # a self-described attribute indispensable. Even the version information of the Assembly is described in the key file.
As Microsoft said: the target of a property can be an assembly, class, constructor, Delegate, enumeration, event, field, interface, method, executable file module, parameter, and property), return value, structure, or other attributes ). It is omnipotent.
What is described is like being injected into the soul.
The information provided by the feature is also called metadata. Metadata can be checked by the application at runtime to control the way the program processes data, or by external tools before running to control the way the application processes or maintains itself. For example, if. NET Framework predefines a property type and uses the property type to control runtime behaviorProgramming LanguageThe property type is used to indicate the language functions that are not directly supported by the. NET Framework Public type system.
All feature types are derived directly or indirectly from the attribute class. Features can be applied to any target element. Multiple features can be applied to the same target element. features can be inherited from elements derived from the target element. You can use the attributetargets class to specify the target element to which the feature applies.
Attributetargets Enumeration:
| All |
You can apply attributes to any application element. |
|
Assembly |
You can apply attributes to an assembly. |
|
Class |
You can apply attributes to a class. |
|
Constructor |
You can apply properties to constructors. |
|
Delegate |
You can delegate application attributes. |
|
Enum |
You can enumerate application attributes. |
|
Event |
You can apply attributes to events. |
|
Field |
You can apply attributes to fields. |
|
Genericparameter |
You can apply attributes to generic parameters. |
|
Interface |
You can apply attributes to interfaces. |
|
Method |
You can apply attributes to methods. |
|
Module |
You can apply attributes to a module.
| Note: |
ModuleIt refers to portable executable files (. dll or. EXE), rather than visual basic standard modules. |
|
|
Parameter |
You can apply attributes to parameters. |
|
Property |
You can apply an attribute to a property ). |
|
Returnvalue |
You can apply attributes to the returned values. |
|
Struct |
You can apply properties to the structure, that is, the value type. |
Attributetargets is used as a parameter for attributeusageattribute to specify the element type for which attributes can be applied.
The attributetargets enumeration value can be combined by bitwise OR to obtain the preferred combination.
The following table lists the members exposed by attributeusageattribute.
| |
Name |
Description |
|
Allowmultiple |
Gets or sets a Boolean value indicating whether multiple indicator attribute instances can be specified for a program element. |
|
Inherited |
Gets or sets a Boolean value indicating whether the property can be inherited by a derived class or override member. |
|
Typeid |
When implemented in a derived class, obtain the unique identifier of this attribute. (Inherited from attribute .) |
|
Validon |
Gets a set of values that indicate the program elements that can be applied to the property. |
After reading this, I feel dizzy. Here is an example (first, an entity class ):
[Talbe ("student")]
Public class student: entitybase
{
Private int Sid;
[Primarykey (primarykeytype. Identity, "studentid")]
Public int Sid
{
Get {return Sid ;}
Set {SID = value ;}
}
Private string sname;
[Primarykey (primarykeytype. Assign, "sname")]
Public String sname
{
Get {return sname ;}
Set {sname = value ;}
}
Private datetime intime = datetime. now;
[Colum ("intime")]
Public datetime intime
{
Get {return intime ;}
Set {intime = value ;}
}
Private double classid;
[Colum ("classid")]
Public double classid
{
Get {return classid ;}
Set {classid = value ;}
}
}
As you can see, the "[]" on the top of the class and the attribute are the feature classes. Let's take a look at the features of these feature classes.CodeRight:
# Region table feature class
[Attributeusageattribute (attributetargets. Class, inherited = false, allowmultiple = false), serializable]
Public class talbeattribute: attribute
{
// Save the Table Name field
Private string _ tablename;
Public talbeattribute ()
{
}
Public talbeattribute (string tablename)
{
This. _ tablename = tablename;
}
/// <Summary>
/// Ing table name (full name: Mode name. Table name)
/// </Summary>
Public String tablename
{
Set
{
This. _ tablename = value;
}
Get
{
Return this. _ tablename;
}
}
}
# Endregion
# Region column feature class
/// <Summary>
/// Ing features of columns in the database
/// </Summary>
[Attributeusageattribute (attributetargets. Property, inherited = false, allowmultiple = false), serializable]
Public class columattribute: attribute
{
Private string _ columname;
Private int _ columsize;
Private specildbtype _ dbtype = specildbtype. default;
Public columattribute ()
{
}
Public columattribute (string columname): this ()
{
This. _ columname = columname;
}
Public columattribute (string columname, int columsize): This (columname)
{
This. _ columsize = columsize;
}
Public columattribute (string columname, int columsize, specildbtype dbtype): This (columname, columsize)
{
This. _ dbtype = dbtype;
}
Public columattribute (string columname, specildbtype dbtype): This (columname)
{< br> This. _ dbtype = dbtype;
}
// size of columns in the database
Public int columsize
{< br> set
{< br> This. _ columsize = value;
}< br> Get
{< br> return this. _ columsize;
}< BR >}
// column name
Public Virtual string columname
{< br> set
{< br> This. _ columname = value;
}< br> Get
{< br> return this. _ columname;
}< BR >}
// Describes some special database types
Public specildbtype dbtype
{
Get {return _ dbtype ;}
Set {_ dbtype = value ;}
}
}
# Endregion
# Region features of primary key columns
[Attributeusageattribute (attributetargets. Property, inherited = false, allowmultiple = false), serializable]
Public class primarykey: columattribute
{
Private primarykeytype _ primarykeytype = primarykeytype. identity;
// Private specildbtype _ dbtype = specildbtype. default;
Public primarykey ()
{
}
Public primarykey (primarykeytype, string columname)
{
This. _ primarykeytype = primarykeytype;
This. columname = columname;
}
Public primarykey (primarykeytype, string columname, specildbtype dbtype)
: This (primarykeytype, columname)
{< br> This. dbtype = dbtype;
}
// Public override string columname
//{
// Set
//{
// This. _ columname = value;
//}
// Get
//{
// Return this. _ columname;
//}
//}
/// <Summary>
/// Primary key type. The default value is auto-increment.
/// </Summary>
Public primarykeytype
{
Set
{
This. _ primarykeytype = value;
}
Get
{
Return this. _ primarykeytype;
}
}
}
# Endregion
Now, we can use reflection technology to read the attributes of each feature class instance. In this way, you can map a class to a table in the database. The compiled technology can be used to spell out SQL statements.
In this example, we will introduce you to an ORM persistent layer framework. With this feature class, you can build a framework like hibernate in Java without writing any xml configuration files about tables and object classes. This is just the beginning. In the future, I will introduce the reflection technology and lead you to build an ORM framework. Now, let's write this example here. I will introduce the reflection technology next time.