Unity游戏开发——C#特性Attribute与自动化
這篇文章主要講一下C#里面Attribute的使用方法及其可能的應(yīng)用場景。
比如你把玩家的血量、攻擊、防御等屬性寫到枚舉里面。然后界面可能有很多地方要根據(jù)這個(gè)枚舉獲取屬性的描述文本。
比如你做網(wǎng)絡(luò)框架的時(shí)候,一個(gè)協(xié)議號對應(yīng)一個(gè)類的處理或者一個(gè)方法。
比如你做ORM,一個(gè)類的屬性是否映射持久化文件中的屬性,映射過去的屬性名是什么。
?
1、什么是Attribute
如果用過Java的Annotation的同學(xué),可以把Attribute當(dāng)成Annotation來看待。
還不了解Attribute的同學(xué)不用急,我們看一下官方的解釋:
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類可以把目標(biāo)元素和一個(gè)預(yù)定義的信息或者是用戶自定義信息關(guān)聯(lián)起來。這里的目標(biāo)元素可以是assembly,class,constructor,delegate,enum,event,field,interface,method,可執(zhí)行文件模塊,parameter,property,return value,struct或其它的Attribute。
? ? Attribute提供的信息也被稱為元數(shù)據(jù)(metadata)。元數(shù)據(jù)能用于在運(yùn)行時(shí)控制怎樣訪問你的程序數(shù)據(jù),或者在運(yùn)行前通過額外的工具來控制怎樣處理你的程序或部署它。例如.NET Framework預(yù)定義并使用attribute去控制運(yùn)行時(shí)行為,一些編程語言使用attribute類型來描述.NET Framework中通用類型不直接支持的語言特性。
? ? 所有的Attribute類型直接或間接從Attribute類繼承。Attribute能應(yīng)用到任何target元素;多個(gè)Attribute能應(yīng)用到相同的元素;
? ? Attribute類提供遍歷的方法去取出和測試自定義Attribute。更多關(guān)于Attribute的信息,可以看Applying Attributes和Extending Metadata Using Attributes。
?
如果你看了官方的解釋不明白,看了我的翻譯也不明白。也沒事。。。我們接下來舉個(gè)例子看看Attribute能做啥。
2、用Attribute將枚舉和一個(gè)描述文本綁定在一起
假設(shè)有這個(gè)枚舉
[csharp]?view plain?copy
注意:如果含中文的代碼編譯報(bào)“Newline in constant”的錯(cuò)。那么請將文件的編碼保存為“帶BOM的UTF-8”。VS中可以在“文件”-“高級保存選項(xiàng)”,然后選擇編碼下拉中選擇。
?
然后你現(xiàn)在想要根據(jù)枚舉來獲得中文描述:比如傳入:
Properties.MagDef返回“法術(shù)防御”。
最原始的做法:
[csharp]?view plain?copy
這樣確實(shí)可以解決問題,但是我們可以用Attribute來做的更好。可以做的更好干嘛不呢?
先定義一個(gè)用于存儲描述文本的Attribute。[csharp]?view plain?copy
沒錯(cuò),看起來是不是覺得很簡單。
?
?
然后我們就可以把上面定義的PropertiesDesc加到Properties上面,像這樣:
[csharp]?view plain?copy
OK。這樣,我們相當(dāng)于就把一個(gè)文本描述信息通過Attribute關(guān)聯(lián)到我們的枚舉屬性了。
那么怎樣獲取?我們來重寫之前的PropertiesUtils類。
[csharp]?view plain?copy
可以看到。這里面已經(jīng)不用自己去判斷哪個(gè)枚舉值返回哪個(gè)字符串描述了。而是獲取這個(gè)枚舉域的PropertiesDesc對象。然后返回它的Desc屬性。
當(dāng)然,你還可以把上面的代碼改成通用的,把Properties改成一個(gè)Type,這樣就可以處理所有的枚舉。然后還可以在查找PropertiesDesc的位置增加一個(gè)緩存。根據(jù)Type和字段的Name做緩存。改完后代碼如下:
[csharp]?view plain?copy
3、還能干什么?
? ? Attribute能干的事情太多了,比如你寫了個(gè)類,想做ORM映射,里面有些字段不想映射到表,有些想映射到表。有些字段可能名字和表的字段不一樣。這時(shí)候你就可以通過Attribute來標(biāo)識哪個(gè)字段需要被映射,映射到數(shù)據(jù)庫哪個(gè)字段。等等。
? ? 做過網(wǎng)絡(luò)框架的同學(xué)也應(yīng)該比較熟悉的一個(gè)應(yīng)用,使用Attribute來做自動(dòng)的消息派發(fā)。
? ? 總之,Attribute可以做很多自動(dòng)化的事情,就看你怎么用了。
總結(jié)
以上是生活随笔為你收集整理的Unity游戏开发——C#特性Attribute与自动化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# Job System
- 下一篇: Unity c#中Attribute用法