使用Attribute注册所有特性
来源:互联网 发布:朝鲜核试验的影响知乎 编辑:程序博客网 时间:2024/06/05 20:56
一、定义一个拥有Attribute特性的基类
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]public class ClassTypeRegisterAttributeBase : System.Attribute{public virtual void Register(System.Type ClassType){}}
二、真正执行操作的子类
子类A
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]public class GameEffectRegister : ClassTypeRegisterAttributeBase{public override void Register(Type AttachedClass){base.Register(AttachedClass);GameEffectFactory.GameEffectClassList.Add(new System.Collections.Generic.KeyValuePair<PKG.EffectType, Type>(EffectType, AttachedClass));}public PKG.EffectType EffectType { get; set; }}
子类B
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]public class ItemEffectRegister : ClassTypeRegisterAttributeBase{public override void Register(Type ClassType){base.Register(ClassType);ItemEffectTypes.Types.Add(ClassType);}public PKG.EffectType EffectType;}
三、注册所有拥有继承了ClassTypeRegisterAttributeBase 特性的类,这里只列举了其中一个
public class ClassTypeRegister{/// <summary>/// 只处理一次/// </summary>static ClassTypeRegister(){var Asm = Assembly.GetExecutingAssembly();//获取所有程序集var AllTypes = Asm.GetTypes(); //获取所有类型for (int i = 0; i < AllTypes.Length; i++){var Type = AllTypes[i];var Processors = Type.GetCustomAttributes(typeof(ClassTypeRegisterAttributeBase), true) as ClassTypeRegisterAttributeBase[];//获取继承了基类<span style="font-family: Arial, Helvetica, sans-serif;">ClassTypeRegisterAttributeBase的类的类型</span>if (Processors.Length > 0) //判断是否存在{for (int j = 0; j < Processors.Length; j++){Processors[j].Register(Type); //调用子类的register接口}mClassList.Add(Type);}}}public static void StaticRegister(){//留空即可}public static IEnumerator<System.Type> GetEnumerator(){return mClassList.GetEnumerator();}protected static List<System.Type> mClassList = new List<Type>();}
0 0
- 使用Attribute注册所有特性
- 特性的简单使用(Attribute)
- C# 使用自定义的特性Attribute
- 特性Attribute
- Attribute特性
- c# 特性/属性(Attribute) 以及使用反射查看自定义特性
- c# 特性/属性(Attribute) 以及使用反射查看自定义特性
- ASP.NET MVC5 新特性:Attribute路由使用详解
- ASP.NET MVC5 新特性:Attribute路由使用详解
- unity中的c# Attribute特性的使用记录
- .NET MVC5 新特性:Attribute路由使用详解
- C# 特性(Attribute)(一)
- C# 特性(Attribute)(二)
- 反射特性(Attribute
- C#特性(Attribute)
- C#特性(Attribute)
- Attribute特性的原理
- 特性(Attribute)了解
- 在windows下利用Git配合Visual Studio实现版本控制
- java编程思想学习----第一章----对象入门
- 关于自执行函数
- [leetcode-97]Interleaving String(java)
- backbonejs快速入门(三
- 使用Attribute注册所有特性
- poj 2226 Muddy Fields 二分图最小点覆盖
- BeanUtils.copyProperties
- android sudio 如何获取sha1与md5值
- IntelliJ 常用快捷键
- 语义分析的一些方法(二)
- 19-集合框架工具类-20-常用对象API(集合框架-JDK5.0特性-ForEach循环)
- EditText属性详情
- mysqldump参数之数据一致性