反射学习笔记之遍历System.Drawing.Color结构(转载)
2010-02-24 22:08 爱研究源码的javaer 阅读(349) 评论(0) 编辑 收藏 举报前台:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Reflection_Color.aspx.cs" Inherits="Reflection_Color" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
#pnColors div
{
float:left;
width:140px;
text-align:center;
margin:3px;
border:1px solid #aaa;
font-size:11px;
font-family: Verdana,Arial
}
</style>
</head>
<body>
<h1>Coding4Fun 使用反射遍历System.Drawing.Color结构</h1>
<form id="form1" runat="server">
<asp:RadioButtonList ID="rblSort" runat="server" AutoPostBack="true" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem Selected="True">Name</asp:ListItem>
<asp:ListItem>Color</asp:ListItem>
</asp:RadioButtonList>
<asp:Panel ID="pnColors" runat="server"></asp:Panel>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
#pnColors div
{
float:left;
width:140px;
text-align:center;
margin:3px;
border:1px solid #aaa;
font-size:11px;
font-family: Verdana,Arial
}
</style>
</head>
<body>
<h1>Coding4Fun 使用反射遍历System.Drawing.Color结构</h1>
<form id="form1" runat="server">
<asp:RadioButtonList ID="rblSort" runat="server" AutoPostBack="true" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem Selected="True">Name</asp:ListItem>
<asp:ListItem>Color</asp:ListItem>
</asp:RadioButtonList>
<asp:Panel ID="pnColors" runat="server"></asp:Panel>
</form>
</body>
</html>
后台:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public partial class Reflection_Color : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SortBy sort;
if (!IsPostBack)
{
sort = SortBy.Name;
}
else
{
sort = (SortBy)Enum.Parse(typeof(SortBy), rblSort.SelectedValue);
}
List<Div> list = new List<Div>();
Type t = typeof(System.Drawing.Color);
System.Reflection.PropertyInfo[] properties = t.GetProperties( System.Reflection.BindingFlags.Static| System.Reflection.BindingFlags.Public);
Div div;
foreach (System.Reflection.PropertyInfo p in properties)
{
System.Drawing.Color c;
c = (System.Drawing.Color)t.InvokeMember(p.Name, System.Reflection.BindingFlags.GetProperty, null, typeof(System.Drawing.Color), null);
div = new Div(c);
list.Add(div);
}
list.Sort(Div.GetComparer(sort));//排序
foreach (Div item in list)
{
pnColors.Controls.Add(item);
}
}
}
public class Div : System.Web.UI.HtmlControls.HtmlGenericControl
{
private int colorValue;
private string name;
public Div(System.Drawing.Color color)
: base("div") //调用基类构造函数,创建一个Div
{
this.name = color.Name;
this.colorValue = color.R * 256 + color.G * 256 + color.B * 256;
this.InnerHtml = String.Format("{0}<br/>RGB({1},{2},{3})",name,color.R,color.G,color.B);
int total = color.R + color.G + color.B;
if(total <= 255)
{
this.Style.Add("color", "#eee");
}
this.Style.Add("background", String.Format("rgb({0},{1},{2})", color.R, color.G, color.B));
}
public static ColorComparer GetComparer(SortBy sort)
{
return new ColorComparer(sort);
}
public static ColorComparer GetComparer()
{
return GetComparer(SortBy.Name);
}
public class ColorComparer:IComparer<Div>
{
private SortBy sort;
public ColorComparer(SortBy sort)
{
this.sort = sort;
}
//实现IComparer<T>接口,根据Sort判断以何为依据进行排序
public int Compare(Div x, Div y)
{
if (sort == SortBy.Name)
{
return String.Compare(x.name, y.name);
}
else
{
return x.colorValue.CompareTo(y.colorValue);
}
}
}
}
public enum SortBy
{
Name,Color
}
{
protected void Page_Load(object sender, EventArgs e)
{
SortBy sort;
if (!IsPostBack)
{
sort = SortBy.Name;
}
else
{
sort = (SortBy)Enum.Parse(typeof(SortBy), rblSort.SelectedValue);
}
List<Div> list = new List<Div>();
Type t = typeof(System.Drawing.Color);
System.Reflection.PropertyInfo[] properties = t.GetProperties( System.Reflection.BindingFlags.Static| System.Reflection.BindingFlags.Public);
Div div;
foreach (System.Reflection.PropertyInfo p in properties)
{
System.Drawing.Color c;
c = (System.Drawing.Color)t.InvokeMember(p.Name, System.Reflection.BindingFlags.GetProperty, null, typeof(System.Drawing.Color), null);
div = new Div(c);
list.Add(div);
}
list.Sort(Div.GetComparer(sort));//排序
foreach (Div item in list)
{
pnColors.Controls.Add(item);
}
}
}
public class Div : System.Web.UI.HtmlControls.HtmlGenericControl
{
private int colorValue;
private string name;
public Div(System.Drawing.Color color)
: base("div") //调用基类构造函数,创建一个Div
{
this.name = color.Name;
this.colorValue = color.R * 256 + color.G * 256 + color.B * 256;
this.InnerHtml = String.Format("{0}<br/>RGB({1},{2},{3})",name,color.R,color.G,color.B);
int total = color.R + color.G + color.B;
if(total <= 255)
{
this.Style.Add("color", "#eee");
}
this.Style.Add("background", String.Format("rgb({0},{1},{2})", color.R, color.G, color.B));
}
public static ColorComparer GetComparer(SortBy sort)
{
return new ColorComparer(sort);
}
public static ColorComparer GetComparer()
{
return GetComparer(SortBy.Name);
}
public class ColorComparer:IComparer<Div>
{
private SortBy sort;
public ColorComparer(SortBy sort)
{
this.sort = sort;
}
//实现IComparer<T>接口,根据Sort判断以何为依据进行排序
public int Compare(Div x, Div y)
{
if (sort == SortBy.Name)
{
return String.Compare(x.name, y.name);
}
else
{
return x.colorValue.CompareTo(y.colorValue);
}
}
}
}
public enum SortBy
{
Name,Color
}