如何进行定义特性(Attribute),并且在程序中如何检索定义的特性,可以根据用户定义的不同特性进行不同的操作。
2006-07-31 11:19
786 查看
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
namespace Test
{
public class Myclass
{
public static void Main()
{
HelpAttribute HelpAttr;
string assemblyName;
Process p = Process.GetCurrentProcess();
assemblyName = p.ProcessName + ".exe";
Assembly a = Assembly.LoadFrom(assemblyName);
Type b = a.GetType("Test.Class1");
foreach (Attribute attr in b.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
Type c = a.GetType("Test.Class2");
foreach (Attribute attr in c.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
Type d = a.GetType("Test.Class3");
foreach (Attribute attr in d.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
}
}
[Help("This is Class1")]
public class Class1
{
}
[Help("This is Class2", Version = "1.0")]
public class Class2
{
}
[Help("This is Class3", Version = "2.0",
Description = "This is do-nothing class")]
public class Class3
{
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true,
Inherited = true)]
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
this.version = "No Version is defined for this class";
}
protected string description;
public string Description
{
get
{
return this.description;
}
set
{
this.description = value;
}
}
protected string version;
public string Version
{
get
{
return this.version;
}
set
{
this.version = value;
}
}
}
}
结果为:
Description of ConsoleApplication7.exe:
This is Class1,Version=No Version is defined for this class
Description of ConsoleApplication7.exe:
This is Class2,Version=1.0
Description of ConsoleApplication7.exe:
This is do-nothing class,Version=2.0
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
namespace Test
{
public class Myclass
{
public static void Main()
{
HelpAttribute HelpAttr;
string assemblyName;
Process p = Process.GetCurrentProcess();
assemblyName = p.ProcessName + ".exe";
Assembly a = Assembly.LoadFrom(assemblyName);
Type b = a.GetType("Test.Class1");
foreach (Attribute attr in b.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
Type c = a.GetType("Test.Class2");
foreach (Attribute attr in c.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
Type d = a.GetType("Test.Class3");
foreach (Attribute attr in d.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
}
}
[Help("This is Class1")]
public class Class1
{
}
[Help("This is Class2", Version = "1.0")]
public class Class2
{
}
[Help("This is Class3", Version = "2.0",
Description = "This is do-nothing class")]
public class Class3
{
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true,
Inherited = true)]
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
this.version = "No Version is defined for this class";
}
protected string description;
public string Description
{
get
{
return this.description;
}
set
{
this.description = value;
}
}
protected string version;
public string Version
{
get
{
return this.version;
}
set
{
this.version = value;
}
}
}
}
结果为:
Description of ConsoleApplication7.exe:
This is Class1,Version=No Version is defined for this class
Description of ConsoleApplication7.exe:
This is Class2,Version=1.0
Description of ConsoleApplication7.exe:
This is do-nothing class,Version=2.0
相关文章推荐
- sd--如何控制定价条件根据用户不同而操作不同(有的可以输入有的不可以)
- sd--如何控制定价条件根据用户不同而操作不同(有的可以输入有的不可以)
- sd--如何控制定价条件根据用户不同而操作不同(有的可以输入有的不可以)
- sd--如何控制定价条件根据用户不同而操作不同(有的可以输入有的不可以)
- 同一个表单可以根据用户的选择,提交给不同的后台处理程序
- 如何编写一条SQL实现根据某个字段的条件对某个字段进行不同的update操作
- 如何让DEV PropertyGridControl根据自定义Attribute特性来自定义输入显示
- 如何用最简单的方法让WPF程序支持多国语言,并且语言文字用户可以随意编辑?
- 网页下载文件期间如何防止用户对网页进行其他操作
- VB.net | 如何在程序中执行用户脚本以及进行代数式计算
- 如何让input 只读 可以返回值 但是用户无法操作
- 如何实现在程序中进行网络计算机的用户验证
- GridView下进行编辑操作,根据某项指标判断该行是否可以进行编辑.
- 用vs制作安装程序的时候 如何获取用户输入的序列号,并进行判定
- OA权限模块根据用户权限显示不同的操作按钮
- vue-router如何根据不同的用户给不同的权限
- 如何根据用户所在分组进行身份验证
- IOS开发中如何判断程序第一次启动(根据判断结果决定是否显示新手操作引导)
- DAU 统计,日活跃用户数 (DAU) 是衡量一个产品表现的重要指标。 需要编写程序,根据给定的某一天的 N 条访问记录,对当天的用户总数 M 进行统计。