龙井建设局网站17做网店这个网站好不好

张小明 2025/12/22 14:30:02
龙井建设局网站,17做网店这个网站好不好,wordpress 科技公司,今年最流行的装修风格第一部分#xff1a;特性是什么#xff1f;#xff08;类比贴标签#xff09;1.1 最简单的理解想象一下你在图书馆看书#xff0c;你可能会#xff1a;在重要的页面贴书签#xff08;标记重要内容#xff09;在书封面上贴标签#xff08;如新书、推…第一部分特性是什么类比贴标签1.1 最简单的理解想象一下你在图书馆看书你可能会在重要的页面贴书签标记重要内容在书封面上贴标签如新书、推荐在书的扉页写备注如张三借阅C#特性就像这些书签、标签和备注它们为代码添加额外的信息。1.2 实际代码对比csharp// 没有特性只有代码本身 public void Calculate() { int result 1 2; Console.WriteLine(result); } // 有特性为代码添加额外信息 [Obsolete(这个方法已弃用请使用新版CalculateNew)] public void Calculate() { int result 1 2; Console.WriteLine(result); }上面代码中[Obsolete]就像贴在方法上的注意标签告诉开发者这个方法过时了。第二部分内置特性的实际应用2.1 最常用的三个内置特性示例1Obsolete过时警告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); // 这里会显示警告 int result2 calc.Add(5, 3); // 这是推荐方式 } }false参数只是警告代码还能运行true参数会报错代码无法编译示例2Conditional条件编译csharpusing System.Diagnostics; class Logger { [Conditional(DEBUG)] // 只在DEBUG模式下才执行 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模式下编译这行会执行 // 如果在RELEASE模式下编译这行代码就像不存在一样 logger.LogDebug(程序启动); // 这行无论什么模式都会执行 logger.LogInfo(程序启动); } }示例3Serializable序列化标记csharp[Serializable] // 告诉.NET这个类可以转换成字节流保存 public class Person { public string Name { get; set; } public int Age { get; set; } [NonSerialized] // 告诉.NET这个字段不要保存 public string TemporaryData; } // 使用 class Program { static void Main() { Person person new Person { Name 张三, Age 25, TemporaryData 临时值 }; // 保存到文件只能保存Name和Age不会保存TemporaryData // 读取时TemporaryData会是null或默认值 } }第三部分如何创建自己的特性3.1 最简单的自定义特性csharp// 第一步创建特性类 // 1. 必须继承System.Attribute // 2. 按惯例类名以Attribute结尾 public class MyNoteAttribute : Attribute { // 可以有一些属性 public string Note { get; set; } public DateTime Created { get; set; } } // 第二步使用特性 [MyNote(Note 这是一个重要的类, 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; } // 构造函数定义必需信息 public AuthorInfoAttribute(string author) { Author author; } } // 使用 [AuthorInfo(张三, Version 1.0)] public class Document { [AuthorInfo(李四)] public void Save() { Console.WriteLine(保存文档); } // 下面这行会报错因为特性不支持属性 // [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(优化性能, Priority Priority.High)] public class GameEngine { [Todo(添加错误处理)] public void LoadLevel(string levelName) { Console.WriteLine($加载关卡: {levelName}); } [Todo(实现物理碰撞, 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}, 优先级: {todo.Priority}); } Console.WriteLine(\n方法待办列表:); // 读取所有方法上的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($ 任务: {todo.Task}); Console.WriteLine($ 优先级: {todo.Priority}); } } } }4.2 实用的示例验证用户输入csharpusing System; using System.Reflection; // 验证特性 [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; } } // 验证器 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}); } // 检查数值范围 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(验证失败:); foreach (string error in errors) { Console.WriteLine($ - {error}); } } else { Console.WriteLine(验证通过); } } }第五部分逐步练习练习1文档生成器csharp// 创建文档特性 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property)] public class DocumentationAttribute : Attribute { public string Description { get; } public DocumentationAttribute(string description) { Description description; } } // 标记代码 [Documentation(表示一个用户账户)] public class UserAccount { [Documentation(用户名必须是唯一的)] public string Username { get; set; } [Documentation(获取用户欢迎信息)] public string GetWelcomeMessage() { return $欢迎, {Username}!; } } // TODO: 写一个程序读取这些文档信息生成API文档练习2权限控制csharp// 权限特性 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: 写一个安全检查器在执行方法前检查用户权限第六部分特性使用技巧和注意事项6.1 技巧csharp// 1. 多个特性可以叠加 [Serializable] [AuthorInfo(张三)] [Todo(添加序列化测试)] public class MyClass { } // 2. 可以缩写去掉Attribute后缀 [AuthorInfo(李四)] // 等同于 [AuthorInfoAttribute(李四)] // 3. 可以放在同一行 [Serializable][AuthorInfo(张三)] public class MyClass { }6.2 注意事项特性只是元数据它们不会改变代码逻辑只是添加信息需要反射读取要使用特性信息需要通过反射性能考虑反射比较慢不要在频繁调用的地方使用编译时特性有些特性如Conditional是给编译器看的总结对比用途类比代码示例标记过时方法贴过期标签[Obsolete]条件编译写仅调试使用[Conditional]序列化控制标可存档[Serializable]添加文档写备注自定义文档特性权限控制贴权限等级自定义角色特性关键理解特性就像代码的标签和备注不会直接影响代码运行但可以通过反射获取这些信息内置特性解决常见问题自定义特性解决特定问题特性让代码更加声明式和自描述从最简单的[Obsolete]开始逐步理解特性如何工作然后创建自己的特性来解决实际问题这是掌握C#特性的最佳路径。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

免费申请企业网站网站开发 工作量评估

PDown作为一款专为百度网盘用户设计的下载加速工具,通过创新的技术架构实现了无需登录个人账号的高速下载体验。该工具采用服务器中转模式,有效规避了传统下载方式的限速问题,为用户提供了安全高效的下载服务。 【免费下载链接】pdown 百度网…

张小明 2025/12/22 14:29:02 网站建设

商丘网站制作电话电子商务网站建设 下载

导语 【免费下载链接】Kimi-Dev-72B 探索开源编程新境界,Kimi-Dev-72B模型惊艳亮相!基于大规模强化学习优化,此编码LLM在软件工程任务中表现出色,勇夺开源模型新标杆。真实仓库自主修复,严格遵循开发标准,成…

张小明 2025/12/22 14:28:01 网站建设

企业做网站好处四川网站营销seo费用

欢迎大家加入开源鸿蒙跨平台开发者社区,一起共建开源鸿蒙跨平台生态。 # 概述 车辆管理是汽车保养应用的核心功能之一。用户需要能够添加、编辑、删除和查看多辆车辆的信息。本文将详细讲解如何在Cordova&OpenHarmony框架中实现一个完整的车辆管理系统&#…

张小明 2025/12/22 14:26:58 网站建设

大英做网站免费设计图

第一章:Docker动态服务发现的挑战与演进在容器化应用广泛部署的背景下,Docker动态服务发现成为构建弹性微服务架构的核心环节。随着容器实例频繁启停、IP地址动态变化,传统静态配置的服务注册与发现机制难以满足实时性需求,催生了…

张小明 2025/12/22 14:25:51 网站建设