97色伦色在线综合视频,无玛专区,18videosex性欧美黑色,日韩黄色电影免费在线观看,国产精品伦理一区二区三区,在线视频欧美日韩,亚洲欧美在线中文字幕不卡

龍井建設(shè)局網(wǎng)站17做網(wǎng)店這個(gè)網(wǎng)站好不好

鶴壁市浩天電氣有限公司 2026/01/22 06:54:41
龍井建設(shè)局網(wǎng)站,17做網(wǎng)店這個(gè)網(wǎng)站好不好,wordpress 科技公司,今年最流行的裝修風(fēng)格第一部分#xff1a;特性是什么#xff1f;#xff08;類比貼標(biāo)簽#xff09;1.1 最簡單的理解想象一下你在圖書館看書#xff0c;你可能會(huì)#xff1a;在重要的頁面貼書簽#xff08;標(biāo)記重要內(nèi)容#xff09;在書封面上貼標(biāo)簽#xff08;如新書、推…第一部分特性是什么類比貼標(biāo)簽1.1 最簡單的理解想象一下你在圖書館看書你可能會(huì)在重要的頁面貼書簽標(biāo)記重要內(nèi)容在書封面上貼標(biāo)簽如新書、推薦在書的扉頁寫備注如張三借閱C#特性就像這些書簽、標(biāo)簽和備注它們?yōu)榇a添加額外的信息。1.2 實(shí)際代碼對比csharp// 沒有特性只有代碼本身 public void Calculate() { int result 1 2; Console.WriteLine(result); } // 有特性為代碼添加額外信息 [Obsolete(這個(gè)方法已棄用請使用新版CalculateNew)] public void Calculate() { int result 1 2; Console.WriteLine(result); }上面代碼中[Obsolete]就像貼在方法上的注意標(biāo)簽告訴開發(fā)者這個(gè)方法過時(shí)了。第二部分內(nèi)置特性的實(shí)際應(yīng)用2.1 最常用的三個(gè)內(nèi)置特性示例1Obsolete過時(shí)警告csharpclass Calculator { // 舊版本 - 不推薦使用 [Obsolete(請使用新版Add方法, false)] public int AddOld(int a, int b) { return a b; } // 新版本 public int Add(int a, int b) { return a b; } } // 使用 class Program { static void Main() { Calculator calc new Calculator(); int result calc.AddOld(5, 3); // 這里會(huì)顯示警告 int result2 calc.Add(5, 3); // 這是推薦方式 } }false參數(shù)只是警告代碼還能運(yùn)行true參數(shù)會(huì)報(bào)錯(cuò)代碼無法編譯示例2Conditional條件編譯csharpusing System.Diagnostics; class Logger { [Conditional(DEBUG)] // 只在DEBUG模式下才執(zhí)行 public void LogDebug(string message) { Console.WriteLine($[DEBUG] {DateTime.Now}: {message}); } public void LogInfo(string message) { Console.WriteLine($[INFO] {DateTime.Now}: {message}); } } // 使用 class Program { static void Main() { Logger logger new Logger(); // 如果在DEBUG模式下編譯這行會(huì)執(zhí)行 // 如果在RELEASE模式下編譯這行代碼就像不存在一樣 logger.LogDebug(程序啟動(dòng)); // 這行無論什么模式都會(huì)執(zhí)行 logger.LogInfo(程序啟動(dòng)); } }示例3Serializable序列化標(biāo)記csharp[Serializable] // 告訴.NET這個(gè)類可以轉(zhuǎn)換成字節(jié)流保存 public class Person { public string Name { get; set; } public int Age { get; set; } [NonSerialized] // 告訴.NET這個(gè)字段不要保存 public string TemporaryData; } // 使用 class Program { static void Main() { Person person new Person { Name 張三, Age 25, TemporaryData 臨時(shí)值 }; // 保存到文件只能保存Name和Age不會(huì)保存TemporaryData // 讀取時(shí)TemporaryData會(huì)是null或默認(rèn)值 } }第三部分如何創(chuàng)建自己的特性3.1 最簡單的自定義特性csharp// 第一步創(chuàng)建特性類 // 1. 必須繼承System.Attribute // 2. 按慣例類名以Attribute結(jié)尾 public class MyNoteAttribute : Attribute { // 可以有一些屬性 public string Note { get; set; } public DateTime Created { get; set; } } // 第二步使用特性 [MyNote(Note 這是一個(gè)重要的類, Created 2024-01-01)] public class ImportantClass { [MyNote(Note 核心方法)] public void ImportantMethod() { Console.WriteLine(做一些重要的事情); } }3.2 添加一些控制csharp// 限制特性只能用于類和方法 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class AuthorInfoAttribute : Attribute { public string Author { get; } public string Version { get; set; } // 構(gòu)造函數(shù)定義必需信息 public AuthorInfoAttribute(string author) { Author author; } } // 使用 [AuthorInfo(張三, Version 1.0)] public class Document { [AuthorInfo(李四)] public void Save() { Console.WriteLine(保存文檔); } // 下面這行會(huì)報(bào)錯(cuò)因?yàn)樘匦圆恢С謱傩?// [AuthorInfo(王五)] // public string Title { get; set; } }第四部分如何讀取和使用特性4.1 基本讀取方法csharpusing System; using System.Reflection; // 定義特性 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class TodoAttribute : Attribute { public string Task { get; } public Priority Priority { get; set; } public TodoAttribute(string task) { Task task; } } public enum Priority { Low, Medium, High } // 使用特性 [Todo(優(yōu)化性能, Priority Priority.High)] public class GameEngine { [Todo(添加錯(cuò)誤處理)] public void LoadLevel(string levelName) { Console.WriteLine($加載關(guān)卡: {levelName}); } [Todo(實(shí)現(xiàn)物理碰撞, Priority Priority.Medium)] public void UpdatePhysics() { Console.WriteLine(更新物理); } } // 讀取特性 class Program { static void Main() { // 獲取類型 Type gameType typeof(GameEngine); // 讀取類上的Todo特性 var classTodos gameType.GetCustomAttributes(typeof(TodoAttribute), false); foreach (TodoAttribute todo in classTodos) { Console.WriteLine($類待辦: {todo.Task}, 優(yōu)先級(jí): {todo.Priority}); } Console.WriteLine( 方法待辦列表:); // 讀取所有方法上的Todo特性 foreach (MethodInfo method in gameType.GetMethods()) { var methodTodos method.GetCustomAttributes(typeof(TodoAttribute), false); foreach (TodoAttribute todo in methodTodos) { Console.WriteLine($方法: {method.Name}); Console.WriteLine($ 任務(wù): {todo.Task}); Console.WriteLine($ 優(yōu)先級(jí): {todo.Priority}); } } } }4.2 實(shí)用的示例驗(yàn)證用戶輸入csharpusing System; using System.Reflection; // 驗(yàn)證特性 [AttributeUsage(AttributeTargets.Property)] public class ValidateAttribute : Attribute { public int MinLength { get; set; } public int MaxLength { get; set; } public bool Required { get; set; } } // 用戶類 public class User { [Validate(Required true, MinLength 2, MaxLength 50)] public string Name { get; set; } [Validate(Required true, MinLength 6)] public string Password { get; set; } [Validate(MinLength 0, MaxLength 120)] public int Age { get; set; } } // 驗(yàn)證器 public class Validator { public static Liststring Validate(object obj) { Liststring errors new Liststring(); Type type obj.GetType(); // 檢查所有屬性 foreach (PropertyInfo property in type.GetProperties()) { // 獲取Validate特性 ValidateAttribute validate property.GetCustomAttributeValidateAttribute(); if (validate ! null) { object value property.GetValue(obj); string propertyName property.Name; // 檢查必填 if (validate.Required) { if (value null || string.IsNullOrEmpty(value.ToString())) { errors.Add(${propertyName} 不能為空); continue; } } // 檢查字符串長度 if (value is string strValue) { if (strValue.Length validate.MinLength) errors.Add(${propertyName} 長度不能小于 {validate.MinLength}); if (validate.MaxLength 0 strValue.Length validate.MaxLength) errors.Add(${propertyName} 長度不能大于 {validate.MaxLength}); } // 檢查數(shù)值范圍 if (value is int intValue) { if (intValue validate.MinLength) errors.Add(${propertyName} 不能小于 {validate.MinLength}); if (validate.MaxLength 0 intValue validate.MaxLength) errors.Add(${propertyName} 不能大于 {validate.MaxLength}); } } } return errors; } } // 使用 class Program { static void Main() { User user new User { Name A, // 太短 Password 123, // 太短 Age 150 // 太大 }; var errors Validator.Validate(user); if (errors.Count 0) { Console.WriteLine(驗(yàn)證失敗:); foreach (string error in errors) { Console.WriteLine($ - {error}); } } else { Console.WriteLine(驗(yàn)證通過); } } }第五部分逐步練習(xí)練習(xí)1文檔生成器csharp// 創(chuàng)建文檔特性 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property)] public class DocumentationAttribute : Attribute { public string Description { get; } public DocumentationAttribute(string description) { Description description; } } // 標(biāo)記代碼 [Documentation(表示一個(gè)用戶賬戶)] public class UserAccount { [Documentation(用戶名必須是唯一的)] public string Username { get; set; } [Documentation(獲取用戶歡迎信息)] public string GetWelcomeMessage() { return $歡迎, {Username}!; } } // TODO: 寫一個(gè)程序讀取這些文檔信息生成API文檔練習(xí)2權(quán)限控制csharp// 權(quán)限特性 public enum UserRole { Guest, User, Admin } [AttributeUsage(AttributeTargets.Method)] public class RequireRoleAttribute : Attribute { public UserRole RequiredRole { get; } public RequireRoleAttribute(UserRole role) { RequiredRole role; } } // 使用 public class AdminPanel { [RequireRole(UserRole.Admin)] public void DeleteUser(string username) { Console.WriteLine($刪除用戶: {username}); } [RequireRole(UserRole.User)] public void ChangePassword(string newPassword) { Console.WriteLine(修改密碼); } } // TODO: 寫一個(gè)安全檢查器在執(zhí)行方法前檢查用戶權(quán)限第六部分特性使用技巧和注意事項(xiàng)6.1 技巧csharp// 1. 多個(gè)特性可以疊加 [Serializable] [AuthorInfo(張三)] [Todo(添加序列化測試)] public class MyClass { } // 2. 可以縮寫去掉Attribute后綴 [AuthorInfo(李四)] // 等同于 [AuthorInfoAttribute(李四)] // 3. 可以放在同一行 [Serializable][AuthorInfo(張三)] public class MyClass { }6.2 注意事項(xiàng)特性只是元數(shù)據(jù)它們不會(huì)改變代碼邏輯只是添加信息需要反射讀取要使用特性信息需要通過反射性能考慮反射比較慢不要在頻繁調(diào)用的地方使用編譯時(shí)特性有些特性如Conditional是給編譯器看的總結(jié)對比用途類比代碼示例標(biāo)記過時(shí)方法貼過期標(biāo)簽[Obsolete]條件編譯寫僅調(diào)試使用[Conditional]序列化控制標(biāo)可存檔[Serializable]添加文檔寫備注自定義文檔特性權(quán)限控制貼權(quán)限等級(jí)自定義角色特性關(guān)鍵理解特性就像代碼的標(biāo)簽和備注不會(huì)直接影響代碼運(yùn)行但可以通過反射獲取這些信息內(nèi)置特性解決常見問題自定義特性解決特定問題特性讓代碼更加聲明式和自描述從最簡單的[Obsolete]開始逐步理解特性如何工作然后創(chuàng)建自己的特性來解決實(shí)際問題這是掌握C#特性的最佳路徑。
版權(quán)聲明: 本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請聯(lián)系我們進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

漯河建設(shè)網(wǎng)站有域名怎么做網(wǎng)站

漯河建設(shè)網(wǎng)站,有域名怎么做網(wǎng)站,wordpress頁面排序,阿里云小程序開發(fā)Windows 8 設(shè)備、應(yīng)用安裝與配置全攻略 1. 設(shè)備與驅(qū)動(dòng)配置 在使用 Windows 8 系統(tǒng)時(shí),設(shè)備和驅(qū)動(dòng)的配

2026/01/21 16:50:01

江蘇手機(jī)網(wǎng)站建設(shè)公司soho設(shè)計(jì)網(wǎng)站

江蘇手機(jī)網(wǎng)站建設(shè)公司,soho設(shè)計(jì)網(wǎng)站,深圳平臺(tái)網(wǎng)站開發(fā),泰安網(wǎng)站建設(shè)方案書屏幕設(shè)計(jì):FoxPro 與 Visual Basic .NET 的全方位對比 在軟件開發(fā)中,屏幕設(shè)計(jì)是至關(guān)重要的一環(huán),它直

2026/01/21 16:10:01

湖南營銷型網(wǎng)站建設(shè)黃驊貼吧

湖南營銷型網(wǎng)站建設(shè),黃驊貼吧,網(wǎng)站設(shè)計(jì)流程步驟,摘抄一則新聞為什么說Wan2.2-T2V-5B定義了下一代T2V生產(chǎn)力工具 你有沒有過這樣的經(jīng)歷#xff1f;凌晨兩點(diǎn)#xff0c;為了趕一條短視頻焦頭

2026/01/21 20:02:01

網(wǎng)站主關(guān)鍵詞如何優(yōu)化視頻下載軟件

網(wǎng)站主關(guān)鍵詞如何優(yōu)化,視頻下載軟件,網(wǎng)站建設(shè)費(fèi)屬于什么稅目,軟件開發(fā)app開發(fā)定制外包騰訊混元開源HunyuanVideo-Foley#xff1a;端到端視頻音效生成新突破 在影視制作的幕后#xff0

2026/01/21 17:03:01