Effective C#中文版 有一篇文章,代码怎么运行不了呢??

xyzvalue 2009-07-27 12:59:57
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)));
============================================

以上代码,怎么没有结果呢?
...全文
79 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
virus7581 2009-07-27
  • 打赏
  • 举报
回复
[DefaultSort( "Name" )]

public class Customer

{

public string Name

{

get { return _name; }

set { _name = value; }

}

public decimal CurrentBalance

{

get { return _balance; }

}

public decimal AccountValue

{

get

{

return calculateValueOfAccount();

}

}

}
你给的解释中有相关步骤,根据这段代码补全Customer类;
另如果数组的sort方法没有以比较器ICompare接口为参数类型的重载,那就用泛型集合(list<T>)
yymycabbage 2009-07-27
  • 打赏
  • 举报
回复
可以运行的楼主,首先把Customer 类补完整

[DefaultSort("Name")]
public class Customer
{
private string name;

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

private decimal currentBalace;

public decimal CurrentBalance
{
get
{
return currentBalace;
}
}

private decimal accountValue;

public decimal AccountValue
{
get
{
return accountValue;
}
}
}


然后CustomerList不应该是Customer[],这个东西继承于System.Array,它的sort方法好像没有只接受一个ICompare类型参数的重载。
这是我的测试代码,供楼主参考。

Customer c1 = new Customer();
c1.Name = "bac";
Customer c2 = new Customer();
c2.Name = "bbc";
Customer c3 = new Customer();
c3.Name = "vcs";
Customer c4 = new Customer();
c4.Name = "ggd";

ArrayList CustomerList = new ArrayList();
CustomerList.Add(c1);
CustomerList.Add(c2);
CustomerList.Add(c3);
CustomerList.Add(c4);

CustomerList.Sort(new GenericComparer(typeof(Customer)));

foreach (Object o in CustomerList)
{
Customer c = (Customer)o;
Console.WriteLine(c.Name);
}


xyzvalue 2009-07-27
  • 打赏
  • 举报
回复
YO
wuyq11 2009-07-27
  • 打赏
  • 举报
回复
检查代码,调试运行
mbh0210 2009-07-27
  • 打赏
  • 举报
回复
也太长了把?
十八道胡同 2009-07-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cdd1012 的回复:]
这么长啊!
[/Quote]
cdd1012 2009-07-27
  • 打赏
  • 举报
回复
这么长啊!
xyzvalue 2009-07-27
  • 打赏
  • 举报
回复
UP
xyzvalue 2009-07-27
  • 打赏
  • 举报
回复
条款24:声明式编程优于命令式编程

和命令式编程相比,声明式编程可能是一种更简单、更精炼的描述软件程序行为的方式。声明式(declarative)编程意味着使用声明、而非指令的方式来定义程序的行为。和许多其他程序语言一样,C#中绝大多数编程都是命令式(imperative)编程:通过编写方法来定义程序的行为。通过使用特性(attribute),我们也可以在C#中实现声明式编程。我们可以将特性应用在类、属性、数据成员或者方法上,.NET运行时则会为我们添加适当的行为。声明式编程更易于实现、阅读和维护。

让我们从一个大家已经使用过的典型示例开始。当编写第1个ASP.NET Web服务时,向导程序会产生如下的代码:

[WebMethod]

public string HelloWorld()

{

return "Hello World";

}

VS.NET Web服务向导程序会为HelloWorld()方法添加一个[WebMethod]特性。这会将HelloWorld()方法声明为一个Web方法。由于对该特性的使用,ASP.NET运行时会为我们创建一些相关的代码。首先,它会为我们创建Web服务描述语言(Web Service Description Language,简称WSDL)文档,其中包含一个对“调用HelloWorld()方法的SOAP文档”的描述。其次,ASP.NET运行时还会添加对HelloWorld()方法SOAP请求的路由支持,并且会动态创建HTML页面来支持我们在IE中测试新的Web服务。这些都有赖于对WebMethod特性的应用。该特性声明了我们的意图,ASP.NET运行时则确保对这种意图给予适当的支持。使用这样的特性可以节省许多开发时间,也可以避免许多错误。

这并没有什么神奇的。ASP.NET运行时在后台使用反射来确定类中的哪些方法为Web方法。在找到Web方法之后,ASP.NET运行时会添加所有必要的框架代码,从而将我们编写的函数转换为Web方法。

[WebMethod]特性只是.NET类库中定义的许多特性中的一个,这些特性可以帮助我们更快捷地创建正确的应用程序。例如,有的特性可以帮助我们创建支持序列化的类型(参见条款25)。有的特性可以控制条件编译(参见条款4)。通过使用特性所支持的声明式编程,我们可以更快地创建代码,并降低犯错的几率。我们应该使用.NET框架中的特性来声明我们的意图,而不是自己编写代码。这样的做法花费的时间较少,也更容易,且编译器不会犯错。

如果预定义特性不能满足我们的需要,我们可以通过定义自己的特性并使用反射来创建声明式的编程构造。作为示例,我们可以创建一个特性及相关的代码,从而允许用户创建定义有默认排序顺序的类型。下面的代码示例演示了如何通过添加特性来定义对Customer集合排序的规则。

[DefaultSort( "Name" )]

public class Customer

{

public string Name

{

get { return _name; }

set { _name = value; }

}

public decimal CurrentBalance

{

get { return _balance; }

}

public decimal AccountValue

{

get

{

return calculateValueOfAccount();

}

}

}

DefaultSort特性为Customer类定义了默认的排序属性:Name。其隐含意思是任何Customer的集合都要使用Customer的Name进行排序。DefaultSort特性并不是.NET框架的一部分。若要实现它,我们需要自己创建DefaultSortAttribute类:

[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;

}

}

随后编写的代码必须根据DefaultSoft特性来对Customer集合进行排序。首先要使用反射找到正确的属性,然后比较两个不同对象上该属性的值。好在,我们只需要编写一次这样的代码就可以了。

接下来,我们需要创建一个实现了IComparer的类(有关IComparer,条款26有详细讨论)。IComparer有一个CompareTo()方法,用于比较给定类型的两个对象,从而允许目标类(即实现了IComparable接口的类型)定义排序顺序。GenericComparer类的构造器会根据被比较的类型,查找默认的排序属性描述符。Compare()方法则使用默认的排序属性来比较任意类型的两个对象。

internal class GenericComparer : IComparer

{

// 有关默认属性的信息:

private readonly PropertyDescriptor _sortProp;

// 升序或者降序。

private readonly bool _reverse = false;

// 类型构造。

public GenericComparer( Type t ) :

this( t, false )

{

}

// 类型构造,以及排序方向。

public GenericComparer( Type t, bool reverse )

{

_reverse = reverse;

// 查找特性,以及排序属性的名称:

// 获取类型上的默认排序特性:

object [] a = t.GetCustomAttributes(

typeof( DefaultSortAttribute ),

false );

// 获取属性的PropertyDescriptor:

if ( a.Length > 0 )

{

DefaultSortAttribute sortName = a[ 0 ] as

DefaultSortAttribute;

string name = sortName.Name;

// 初始化排序属性:

PropertyDescriptorCollection props =

TypeDescriptor.GetProperties( t );

if ( props.Count > 0 )

{

foreach ( PropertyDescriptor p in props )

{

if ( p.Name == name )

{

// 找到了默认排序属性:

_sortProp = p;

break;

}

}

}

}

}

// Compare 方法。

int IComparer.Compare( object left,

object right )

{

// null 比任何对象都小:

if (( left == null ) && ( right == null ))

return 0;

if ( left == null )

return -1;

if ( right == null )

return 1;

if ( _sortProp == null )

{

return 0;

}

// 获取每个对象的排序属性:

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;

}

}

GenericComparer类会根据DefaultSort特性中声明的属性,对Customer集合进行排序:

CustomerList.Sort( new GenericComparer(

typeof( Customer )));

GenericComparer的实现代码使用了一些高级技巧,比如反射(参见条款43)。但是我们只需要编写一次就可以了。自此之后,我们需要做的就是在类上添加DefaultSort特性,然后便可以使用GenericComparer对这些对象的集合进行排序了。如果我们更改了DefaultSort特性上的参数,也就更改了类的行为。我们不需要在代码中更改任何算法。

当一个简单的声明便可以表明我们的意图时,采用这种声明式的做法能够有效地避免重复性代码的编写。再来看一下GenericComparer类。我们可以为创建的每一个类型编写不同版本(也更为简单)的排序算法。使用声明式编程的好处在于,我们可以编写一个通用的类,然后使用一个简单的声明为每个类型创建不同的行为。这里的关键在于行为的改变是基于一个声明,而不是基于任何算法的改变。GenericComparer类适用于任何应用了DefaultSort特性的类型。如果在应用程序中只需要一两次排序功能,那么编写一些简单的函数就可以了。但是,如果我们的程序中有许多不同的类型都需要相同的行为,那么从长远来看,通用的算法加声明式的解决方案将会为我们节省大量的时间和精力。例如,我们永远也不用编写由WebMethod特性产生的所有代码。我们应该利用此技术来为我们的算法服务。条款42也讨论了一个例子:如何使用特性来构建附加的命令处理器。其他的一些例子包括从定义附加包(add-on package)到构建动态的网页UI。

综上所述,声明式编程是一个强大的工具。当可以使用特性来声明我们的意图时,实际上也就避免了在多个类似的手工编写(hand-coded)的算法中,犯逻辑错误的可能。声明式编程会创建更为可读和清晰的代码。这意味着更少的错误。如果可以使用.NET框架中定义的特性,那么我们就应该积极地使用。如果不能,则可以考虑选择创建我们自己的特性类,然后在将来使用它创建相同的行为。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧