请教一个有关ObjectList的简单问题 散分20

my_ibm 2004-10-09 04:18:11


//该道题虽然有一点点长,但是思路很简单,但是还是请你看完
//练习属性的用法
//出错信息是(出错的地方已标出)
//:E:\Visual Studio Projects\属性类综合实例\Class1.cs(78): 找不到类型或命名空间名称
//“ObjectList”(是否缺少 using 指令或程序集引用?)

using System;
using System.Reflection ;
using System.Collections ;

namespace 属性类综合实例
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
//定义一个不带参数的类
public class IsTestAttribute:Attribute
{
public override string ToString()
{
return "Is Tested!!";
}


}

//定义一个带参数的特性类,并且只能用于类和结构
[AttributeUsage(AttributeTargets.Class |AttributeTargets.Struct )]
public class AuthorAttribute:Attribute
{
string name;
double version;
public AuthorAttribute(string name)
{
this.name =name;
this .version =version;
}
public string Name
{
get
{
return name;
}
}
public int Version
{
get
{
return version;
}
set
{
version = value;
}

}
public string ToString()
{
string value;
value = "Author"+Name;
if(version!=0)
{
value +="version"+Version.ToString (Version);

}
return value;
}

}
//关联特性类AuthorAttribute和类Account
[Author("guopeixin")]
public class Account
{
[IsTest]
public Account()
{}
public void AddOrder(Order ordertoadd)
{
order.Add(ordertoadd);


}
//下面一行出错

private ObjectList orders =new System.ObjectList();
//上面一行出错

}
//关联AuthorAttribute和类Order
//public class Order
//{}
//关联类Author,Istest和order类
[Author("wang programmer",Version=2.0),IsTest()]
public class Order
{}

class MainClass
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
///

public static bool IsMemberTest(MemberInfo member)
{
foreach(object attribute in member.GetCustomAttributes (typeof(member)))
{
if(attibutes is IsTestAttribute )
return true;
else
return false;

}
}
public static void DumpAttributes(MemberInfo member)
{
Console.WriteLine ("Attrubutes for:"+member.Name );
foreach(object attribute in member.GetCustomAttributes ());
Console.WriteLine ( attribute);
}

[STAThread]
public static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
//显示Account的特性
DumpAttributes(typeof(Account));
//显示测试的成员列表
foreach (MemberInfo method in typeof(Account ))
{
if(IsMemberTest(method))
{
Console.WriteLine ("member {0} is tested!",method.Name );
Console.WriteLine ("\t"+typeof(Account));

}
else
{
Console.WriteLine ("member {0} is not existed!",method.Name );
}
}
Console.WriteLine ();
//显示特性类order
DumpAttributes(typeof(Order));
Console.WriteLine ("**************"+typeof(Order)+"*************************");

//显示Order类方法中的特征
foreach (MethodInfo method in typeof(Order).GetMethods ())
{
if(IsMemberTest(method))
{
Console.WriteLine ("member {0} is tested!",Method.Name );


}
else
{
Console.WriteLine ("member {0} is not tested!",method.Name );

}

}
Console.WriteLine ("you are sucessed !congulations!!!!!!!!!!!!!!!!!");

}
}
}
...全文
60 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiqimaowc 2004-10-27
  • 打赏
  • 举报
回复
ObjectList?System命名空间里没这个类啊,System.Collections命名空间里也没这个类啊,查msdn,只有System.Web.UI.MobileControls这个命名空间里才有这个类,是个控件。(MobileControls?用于移动设备的?)换个类吧,比如ArrayList(System.Collection.ArrayList)?
my_ibm 2004-10-10
  • 打赏
  • 举报
回复
楼上的兄弟,
你不回答我怎么给你分 |?
qingyun1020 2004-10-09
  • 打赏
  • 举报
回复
jiefen

110,567

社区成员

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

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

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