Custom metadata (attribute) to identify objects (attributes, methods, classes)

Source: Internet
Author: User
Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. reflection;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Using system. Web. UI;
Using system. Windows. forms;

Namespace customcontrol
{
Public class custompropertycontrol: webcontrol
{
System. Web. UI. webcontrols. textbox TB;
Int intnum = 0;
[Category ("appearance")]
[Numvalidate (0, 10)]
[Description ("input value range (0 ~ 10) ")]
Public int num
{
Get
{
Return intnum;
}
Set
{
Intnum = value;
}
}
Protected override void render (htmltextwriter writer)
{
Table T = new table ();
T. cellpadding = 0;
T. cellspacing = 0;
Tablerow TR = new tablerow ();
Tablecell td_left = new tablecell ();
TB = new system. Web. UI. webcontrols. Textbox ();
TB. Text = This. intnum. tostring ();
Td_left.controls.add (TB );
Tr. Controls. Add (td_left );
Numvalidateattribute = This. getnumvalidatearribute ();
If (numvalidateattribute. validateresult (this. Num) = false)
{
Tablecell td_right = new tablecell ();
System. Web. UI. webcontrols. Label lB = new system. Web. UI. webcontrols. Label ();
LB. forecolor = system. Drawing. color. Red;
LB. Text = "value input range must be:" + numvalidateattribute. minvalue.
Tostring
() + "~ "+ Numvalidateattribute. maxvalue. tostring () +! ";
Td_right.controls.add (LB );
Tr. Controls. Add (td_right );
}
T. Controls. Add (TR );
T. rendercontrol (writer );
}
Private numvalidateattribute getnumvalidatearribute ()
{
System. Type type = This. GetType ();
Propertyinfo property = type. getproperty ("num ");
Object [] attrs = (object []) property. getcustomattributes (true );
Foreach (attribute ATTR in attrs)
{
If (ATTR is numvalidateattribute)
{
Return ATTR as numvalidateattribute;
}
} Return NULL;
}
}
[Attributeusage (attributetargets. Property, allowmultiple = true, inherited = true)]
Public class numvalidateattribute: attribute
{
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "intminvalue"> Minimum value </param>
/// <Param name = "intmaxvalue"> maximum value </param>
Public numvalidateattribute (INT intminvalue, int intmaxvalue)
{
This. intminvalue = intminvalue;
This. intmaxvalue = intmaxvalue;
}
Private int intminvalue;
/// <Summary>
/// Maximum value
/// </Summary>
Public int minvalue
{
Get
{
Return intminvalue;
}
}
Private int intmaxvalue;
/// <Summary>
/// Minimum value
/// </Summary>
Public int maxvalue
{
Get
{
Return intmaxvalue;
}
}
/// <Summary>
/// Perform verification
/// </Summary>
/// <Param name = "value"> </param>
/// <Returns> </returns>
Public bool validateresult (INT value)
{
If (this. intminvalue <= Value & value <= This. intmaxvalue)
{
Return true;
}
Else
{
Return false;
}
}
}
}

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.