当前位置: 首页 > news >正文

找别人做网站的注意事项外贸开发产品网站模板

找别人做网站的注意事项,外贸开发产品网站模板,学做网网站论坛,苏州网站小程序app开发公司这篇文章主要讲一下C#里面Attribute的使用方法及其可能的应用场景。 比如你把玩家的血量、攻击、防御等属性写到枚举里面。然后界面可能有很多地方要根据这个枚举获取属性的描述文本。 比如你做网络框架的时候#xff0c;一个协议号对应一个类的处理或者一个方法。 比如你做…这篇文章主要讲一下C#里面Attribute的使用方法及其可能的应用场景。 比如你把玩家的血量、攻击、防御等属性写到枚举里面。然后界面可能有很多地方要根据这个枚举获取属性的描述文本。 比如你做网络框架的时候一个协议号对应一个类的处理或者一个方法。 比如你做ORM一个类的属性是否映射持久化文件中的属性映射过去的属性名是什么。 1、什么是Attribute 如果用过Java的Annotation的同学可以把Attribute当成Annotation来看待。 还不了解Attribute的同学不用急我们看一下官方的解释 The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute. Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system. All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied to any target element; multiple attributes can be applied to the same target element; and attributes can be inherited by an element derived from a target element. Use the AttributeTargets class to specify the target element to which the attribute is applied. The Attribute class provides convenient methods to retrieve and test custom attributes. For more information about using attributes, see Applying Attributes and Extending Metadata Using Attributes. 翻译过来就是 Attribute类可以把目标元素和一个预定义的信息或者是用户自定义信息关联起来。这里的目标元素可以是assemblyclassconstructordelegateenumeventfieldinterfacemethod可执行文件模块parameterpropertyreturn valuestruct或其它的Attribute。 Attribute提供的信息也被称为元数据metadata。元数据能用于在运行时控制怎样访问你的程序数据或者在运行前通过额外的工具来控制怎样处理你的程序或部署它。例如.NET Framework预定义并使用attribute去控制运行时行为一些编程语言使用attribute类型来描述.NET Framework中通用类型不直接支持的语言特性。 所有的Attribute类型直接或间接从Attribute类继承。Attribute能应用到任何target元素多个Attribute能应用到相同的元素 Attribute类提供遍历的方法去取出和测试自定义Attribute。更多关于Attribute的信息可以看Applying Attributes和Extending Metadata Using Attributes。 如果你看了官方的解释不明白看了我的翻译也不明白。也没事。。。我们接下来举个例子看看Attribute能做啥。 2、用Attribute将枚举和一个描述文本绑定在一起 假设有这个枚举 [csharp] view plain copy public enum Properties  {      /// summary      /// 血量      /// /summary      HP  1,        /// summary      /// 物理攻击      /// /summary      PhyAtk  2,        /// summary      /// 物理防御      /// /summary      PhyDef  3,        /// summary      /// 法术攻击      /// /summary      MagAtk  4,        /// summary      /// 法术防御      /// /summary      MagDef  5  }   注意如果含中文的代码编译报“Newline in constant”的错。那么请将文件的编码保存为“带BOM的UTF-8”。VS中可以在“文件”-“高级保存选项”然后选择编码下拉中选择。 然后你现在想要根据枚举来获得中文描述比如传入 Properties.MagDef返回“法术防御”。 最原始的做法 [csharp] view plain copy public class PropertiesUtils  {      public static string GetDescByProperties(Properties p)      {          switch (p)          {              case Properties.HP:                  return 血量;              case Properties.PhyAtk:                  return 物理攻击;              case Properties.PhyDef:                  return 物理防御;              case Properties.MagAtk:                  return 法术攻击;              case Properties.MagDef:                  return 法术防御;              default:                  return 未知属性:  p;          }      }  }  这样确实可以解决问题但是我们可以用Attribute来做的更好。可以做的更好干嘛不呢 先定义一个用于存储描述文本的Attribute。[csharp] view plain copy [System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Enum)]  public class PropertiesDesc : System.Attribute  {      public string Desc { get; private set; }    }   没错看起来是不是觉得很简单。   然后我们就可以把上面定义的PropertiesDesc加到Properties上面像这样 [csharp] view plain copy public enum Properties  {        [PropertiesDesc(血量)]      HP  1,        [PropertiesDesc(物理攻击)]      PhyAtk  2,        [PropertiesDesc(物理防御)]      PhyDef  3,        [PropertiesDesc(法术攻击)]      MagAtk  4,        [PropertiesDesc(法术防御)]      MagDef  5  }   OK。这样我们相当于就把一个文本描述信息通过Attribute关联到我们的枚举属性了。 那么怎样获取我们来重写之前的PropertiesUtils类。 [csharp] view plain copy public class PropertiesUtils  {      public static string GetDescByProperties(Properties p)      {          Type type  p.GetType();          FieldInfo[] fields  type.GetFields();          foreach (FieldInfo field in fields)          {              if (field.Name.Equals(p.ToString()))              {                  object[] objs  field.GetCustomAttributes(typeof(PropertiesDesc), true);                  if (objs ! null  objs.Length  0)                  {                      return ((PropertiesDesc)objs[0]).Desc;                  }                  else                  {                      return p.ToString()  没有附加PropertiesDesc信息;                  }              }          }          return No Such field : p;      }  }   可以看到。这里面已经不用自己去判断哪个枚举值返回哪个字符串描述了。而是获取这个枚举域的PropertiesDesc对象。然后返回它的Desc属性。 当然你还可以把上面的代码改成通用的把Properties改成一个Type这样就可以处理所有的枚举。然后还可以在查找PropertiesDesc的位置增加一个缓存。根据Type和字段的Name做缓存。改完后代码如下 [csharp] view plain copy public class PropertiesUtils  {      private  static DictionaryType, Dictionarystring, string cache  new DictionaryType, Dictionarystring, string();        public static string GetDescByProperties(object p)      {          var type  p.GetType();          if (!cache.ContainsKey(type))          {              Cache(type);          }          var fieldNameToDesc  cache[type];          var fieldName  p.ToString();          return fieldNameToDesc.ContainsKey(fieldName) ? fieldNameToDesc[fieldName] : string.Format(Can not found such desc for field {0} in type {1}, fieldName, type.Name);      }        private static void Cache(Type type)      {          var dict  new Dictionarystring, string();          cache.Add(type, dict);          var fields  type.GetFields();          foreach (var field in fields)          {              var objs  field.GetCustomAttributes(typeof(PropertiesDesc), true);              if (objs.Length  0)              {                  dict.Add(field.Name, ((PropertiesDesc)objs[0]).Desc);              }          }      }  }   3、还能干什么 Attribute能干的事情太多了比如你写了个类想做ORM映射里面有些字段不想映射到表有些想映射到表。有些字段可能名字和表的字段不一样。这时候你就可以通过Attribute来标识哪个字段需要被映射映射到数据库哪个字段。等等。 做过网络框架的同学也应该比较熟悉的一个应用使用Attribute来做自动的消息派发。 总之Attribute可以做很多自动化的事情就看你怎么用了。
http://www.yutouwan.com/news/18888/

相关文章:

  • php网站开发技术优点企业站seo价格
  • iis7.0网站错误代码解决视频图站主题 wordpress
  • 深圳网站建设服务哪个便宜点珠海网站制作推广
  • 深圳极速网站建设电话国内免费推广产品的网站
  • 凡科建站网站怎么保存发给别人数字城市建设网站
  • 智慧团建网站登录入口手机版wordpress 游客
  • 沈阳做网站的设计公司哪家好wordpress字体更换
  • 地方网站做相亲赢利点在哪网站建设dede模板免费
  • 机器人软件开发和网站开发需要一个网站
  • 安徽池州网站制作佛山做网站的
  • 新网站怎么做友情链接网站开发后所有权
  • 怎样创造一个网站建设银行有招投标网站吗
  • 网站内容建设的原则wordpress后台500出错
  • 企业网站建设的困难和问题太原搜索引擎优化招聘信息
  • 做短视频网站收益大学网络推广培训
  • 找做网站的公司网站模板上传
  • 做个小网站 虚拟空间 买服务器莘县聊城做网站
  • 泉州(晋江)网站建设网络小程序开发公司
  • 网站规划与网页设计第四版电子书网站建设 图书
  • 清远市住房和城乡建设局网站网站建设公司 壹宇网络
  • 做海淘网站赚钱吗上哪儿找做网站的客户
  • 金银饰品那家网站做的好定制开发小程序多少钱
  • 支付宝网站支付接口做实体店打折信息网站
  • 查看网站主机网站开发 公司
  • 邯郸市魏县建设局网站局网站建设自查
  • 做网站要用写接口公司注册免费吗
  • 网站优化建设桂林wordpress实例站
  • 网站建设哪些资质两学一做知识竞赛试题网站
  • 外贸业务怎么利用网站开发客户网站免费推广平台有哪些
  • 最新版微信app下载安装百度搜索名字排名优化