Effective C#中文版 有一篇文章,代码怎么运行不了呢??
http://book.csdn.net/bookfiles/295/10029512607.shtml
==============================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;
namespace RefTest
{
internal class GenericComparer : IComparer
{
// Information about the default property:
private readonly PropertyDescriptor _sortProp;
// Ascending or descending.
private readonly bool _reverse = false;
// Construct for a type
public GenericComparer(Type t) :
this(t, false)
{
}
// Construct for a type
// and a direction
public GenericComparer(Type t, bool reverse)
{
_reverse = reverse;
// find the attribute,
// and the name of the sort property:
// Get the default sort attributes on the type:
object[] a = t.GetCustomAttributes(typeof(DefaultSortAttribute),false);
// Get the PropertyDescriptor for that property:
if (a.Length > 0)
{
DefaultSortAttribute sortName = a[0] as DefaultSortAttribute;
string name = sortName.Name;
// Initialize the sort property:
PropertyDescriptorCollection props =TypeDescriptor.GetProperties(t);
if (props.Count > 0)
{
foreach (PropertyDescriptor p in props)
{
if (p.Name == name)
{
// Found the default sort property:
_sortProp = p;
break;
}
}
}
}
}
// Compare method.
int IComparer.Compare(object left,object right)
{
// null is less than any real object:
if ((left == null) && (right == null))
return 0;
if (left == null)
return -1;
if (right == null)
return 1;
if (_sortProp == null)
{
return 0;
}
// Get the sort property from each object:
IComparable lField =_sortProp.GetValue(left) as IComparable;
IComparable rField =_sortProp.GetValue(right) as IComparable;
int rVal = 0;
if (lField == null)
if (rField == null)
return 0;
else
return -1;
rVal = lField.CompareTo(rField);
return (_reverse) ? -rVal : rVal;
}
}
}
=============================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RefTest
{
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct)]
public class DefaultSortAttribute : System.Attribute
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public DefaultSortAttribute(string name)
{
Name = name;
}
}
}
================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RefTest
{
[DefaultSort("Name")]
public class Customer
{
public string Name
{
get;
set;
}
public decimal CurrentBalance
{
get;
}
public decimal AccountValue
{
//get
//{
// return calculateValueOfAccount();
//}
}
}
}
==========================
Customer[] CustomerList=new Customer[4];
CustomerList[0] = new Customer();
CustomerList[0].Name = "bac";
CustomerList[1] = new Customer();
CustomerList[1].Name = "bbc";
CustomerList[2] = new Customer();
CustomerList[2].Name = "vcs";
CustomerList[3] = new Customer();
CustomerList[3].Name = "ggd";
CustomerList.Sort(new GenericComparer(typeof(Customer)));
============================================
以上代码,怎么没有结果呢?