1,提醒使用新方法
class Wolf
{
[Obsolete("该方法已过时,推荐使用:NewDrawMySelf()方法",false)]
public void DrawMySelf()
{
Console.WriteLine(@"
* * * /\__/\ * --- *
* / \ / \
* * | - - | | |*
* __________| \ /| | |
/ \ T / | \ /
/ | * ---
| || | | / *
| || /______\ / |* *
| | \ | / \ / |
\/ | |\ \ | | \ \
| | \ \ | | \ \
| | \ \ | | \ \
''' ''' ''' ''' ");
}
public void NewDrawMySelf()
{
Console.WriteLine(@"");
}
}
class Program
{
static void Main(string[] args)
{
Wolf wolf = new Wolf();
wolf.DrawMySelf();
//wolf.NewDrawMySelf();
Console.ReadKey();
}
}
2,使用特性修饰自定义特性
class AnimalInfoAttribute : Attribute
{
public AnimalInfoAttribute(String name)
{
this.name = name;
}
public string name;
private string phylum;
public string Phylum
{
get { return phylum; }
set { phylum = value; }
}
private String classis;
public String Classis
{
get { return classis; }
set { classis = value; }
}
private String family;
public String Family
{
get { return family; }
set { family = value; }
}
}
[AnimalInfo("狼", Family = "犬科", Phylum = "脊索动物门", Classis = "哺乳纲")]
class Wolf
{
}
class Program
{
static void Main(string[] args)
{
//获取附着在Wolf类上的特性信息
Attribute attr =
Attribute.GetCustomAttribute(typeof(Wolf), typeof(AnimalInfoAttribute));
AnimalInfoAttribute aia = attr as AnimalInfoAttribute;
if (aia == null)
{
Console.WriteLine("Wolf类不存在特性...");
}
else
{
Console.WriteLine(aia.name + ":");
Console.WriteLine("门:" + aia.Phylum);
Console.WriteLine("纲:" + aia.Classis);
Console.WriteLine("科:" + aia.Family);
}
Console.ReadKey();
}
}
总结:
特性是被用来修饰程序元素如:方法,变量,属性,类,接口,结构体等等;
自定义的特性本质也是一个类,创建自定义特性需要集成:System.Attribute类。
程序集(Assembly)是组织程序的逻辑单元,我们编写好的代码最终会被编译器编译为若干个程序集。
两种常见的程序集:可执行文件(。exe);动态链接文件(。dll)。