Unity游戏开发——C#特性Attribute与自动化
這篇文章主要講一下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類可以把目標元素和一個預定義的信息或者是用戶自定義信息關聯起來。這里的目標元素可以是assembly,class,constructor,delegate,enum,event,field,interface,method,可執行文件模塊,parameter,property,return value,struct或其它的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
注意:如果含中文的代碼編譯報“Newline in constant”的錯。那么請將文件的編碼保存為“帶BOM的UTF-8”。VS中可以在“文件”-“高級保存選項”,然后選擇編碼下拉中選擇。
?
然后你現在想要根據枚舉來獲得中文描述:比如傳入:
Properties.MagDef返回“法術防御”。
最原始的做法:
[csharp]?view plain?copy
這樣確實可以解決問題,但是我們可以用Attribute來做的更好。可以做的更好干嘛不呢?
先定義一個用于存儲描述文本的Attribute。[csharp]?view plain?copy
沒錯,看起來是不是覺得很簡單。
?
?
然后我們就可以把上面定義的PropertiesDesc加到Properties上面,像這樣:
[csharp]?view plain?copy
OK。這樣,我們相當于就把一個文本描述信息通過Attribute關聯到我們的枚舉屬性了。
那么怎樣獲取?我們來重寫之前的PropertiesUtils類。
[csharp]?view plain?copy
可以看到。這里面已經不用自己去判斷哪個枚舉值返回哪個字符串描述了。而是獲取這個枚舉域的PropertiesDesc對象。然后返回它的Desc屬性。
當然,你還可以把上面的代碼改成通用的,把Properties改成一個Type,這樣就可以處理所有的枚舉。然后還可以在查找PropertiesDesc的位置增加一個緩存。根據Type和字段的Name做緩存。改完后代碼如下:
[csharp]?view plain?copy
3、還能干什么?
? ? Attribute能干的事情太多了,比如你寫了個類,想做ORM映射,里面有些字段不想映射到表,有些想映射到表。有些字段可能名字和表的字段不一樣。這時候你就可以通過Attribute來標識哪個字段需要被映射,映射到數據庫哪個字段。等等。
? ? 做過網絡框架的同學也應該比較熟悉的一個應用,使用Attribute來做自動的消息派發。
? ? 總之,Attribute可以做很多自動化的事情,就看你怎么用了。
總結
以上是生活随笔為你收集整理的Unity游戏开发——C#特性Attribute与自动化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# Job System
- 下一篇: 双十一福利大放送!各银行信用卡羊毛尽情薅