Code
[Attributeusage (attributetargets. Property, inherited = False )]
[Comvisible ( True )]
Public Class Propertykeyattribute: attribute
{
Public Propertykeyattribute ( Int Length)
{
This . Length = Length;
This . Nonebuild = True ;
}
Public Propertykeyattribute ()
{
This . Nonebuild = True ;
}
Public Int Length { Get ; Set ;}
Public System. Data. sqldbtype { Get ; Set ;}
Public String Name { Get ; Set ;}
Public Int Index { Get ; Set ;}
Public Bool Nonebuild { Get ; Set ;}
Public Static Int Getlength < T > ( String Name)
{
Propertykeyattribute ATTR = Getpropertykeyattribute < T > (Name );
If (ATTR ! = Null )
Return ATTR. length;
Return 20 ;
}
Public Static System. Data. sqldbtype getsqldbtype < T > ( String Name)
{
Propertykeyattribute ATTR = Getpropertykeyattribute < T > (Name );
If (ATTR ! = Null )
Return ATTR. sqldbtype;
Return System. Data. sqldbtype. varchar;
}
Public Static Bool Getnonebuild < T > ( String Name)
{
Propertykeyattribute ATTR = Getpropertykeyattribute < T > (Name );
If (ATTR ! = Null )
Return ATTR. nonebuild;
Return False ;
}
Public Static Int Getlength ( Object Model, String Name)
{
Propertykeyattribute ATTR = Getpropertykeyattribute (model, name );
If (ATTR ! = Null )
Return ATTR. length;
Return 20 ;
}
Public Static System. Data. sqldbtype getsqldbtype ( Object Model, String Name)
{
Propertykeyattribute ATTR = Getpropertykeyattribute (model, name );
If (ATTR ! = Null )
Return ATTR. sqldbtype;
Return System. Data. sqldbtype. varchar;
}
Public Static Bool Getnonebuild ( Object Model, String Name)
{
Propertykeyattribute ATTR = Getpropertykeyattribute (model, name );
If (ATTR ! = Null )
Return ATTR. nonebuild;
Return False ;
}
# Region Propertykeyattribute
Private Static Propertykeyattribute getpropertykeyattribute < T > ( String Name)
{
T Model = (T) activator. createinstance ( Typeof (T ));
Type type = Typeof (Propertykeyattribute );
Propertyinfo pi = Model. GetType (). getproperty (name );
Object [] Attributes = Pi. getcustomattributes ( False );
Foreach ( Object O In Attributes)
{
If (O Is Propertykeyattribute)
{
// Forcibly convert the obtained attribute to the personattribute
Propertykeyattribute Pa = (Propertykeyattribute) O;
Return Pa;
}
}
Return Null ;
}
Private Static Propertykeyattribute getpropertykeyattribute ( Object Model, String Name)
{
Try
{
Type type = Typeof (Propertykeyattribute );
Propertyinfo pi = Model. GetType (). getproperty (name );
Object [] Attributes = Pi. getcustomattributes ( False );
Foreach ( Object O In Attributes)
{
If (O Is Propertykeyattribute)
{
// Forcibly convert the obtained attribute to the personattribute
Propertykeyattribute Pa = (Propertykeyattribute) O;
Return Pa;
}
}
}
Catch
{
}
Return Null ;
}
# Endregion
}