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

在自己的网站上怎么做淘宝客武汉网站推广很 棒

在自己的网站上怎么做淘宝客,武汉网站推广很 棒,做销售用什么网站好,微信小商店坑死人前言 Attribute(特性)是一种用于为程序元素(如类、方法、属性等)提供元数据信息的方法。 特性是一种声明式的信息,附加到程序元素上,提供额外的数据用于描述和控制这些元素的行为。 在编译和运行时&…

image

前言

Attribute(特性)是一种用于为程序元素(如类、方法、属性等)提供元数据信息的方法。

特性是一种声明式的信息,附加到程序元素上,提供额外的数据用于描述和控制这些元素的行为。

在编译和运行时,都可以读取特性提供的信息对程序元素进行分析和操作。

特性通常用于实现以下功能:

  1. 为代码添加元数据信息:特性为元素(类、方法、属性等)添加额外的元数据信息,描述其用途、状态、约束等。

  2. 控制编译行为:特性可以用于控制编译器对代码的处理,如指示编译器生成额外的代码、忽略特定警告等。

  3. 在运行时进行反射操作:特性可以在运行时通过反射获取并读取,用于实现一些动态的行为,如配置加载、权限检查等。

C# 本身提供了一些常用的特性,如 SerializableObsoleteConditional 等,同时我们也可以自定义特性来满足特定的需求。

特性的应用范围非常广泛,在实际开发中经常用于标记和控制程序的行为,提高代码的可读性和灵活性。

下面看看如何自定义特性 Attribute 并进行应用。

Step By Step 步骤:

  1. 创建一个 ASP.NET Core Web Api 项目

  2. 创建一个 Attribute

    namespace AttributeSample
    {// 说明此特性应用在属性上[AttributeUsage(AttributeTargets.Property)]public class TestAttribute: Attribute{public string DisplayName { get; set; }public int DisplayWidth { get; set; }public TestAttribute(string displayName, int displayWidth){DisplayName = displayName;DisplayWidth = displayWidth;}}
    }
    
  3. 创建一个类,应用这个 Attribute

    public class Points
    {[Test("站点名称", 100)]public string StationNo { get; set; }[Test("P1数据名称", 100)]public float TD_P1 { get; set; }[Test("P2数据名称", 100)]public float TD_P2 { get; set; }
    }
    
  4. 在控制器中读取 Points 类的属性的特性的值并应用

    namespace AttributeSample.Controllers
    {[ApiController][Route("[controller]")]public class WeatherForecastController : ControllerBase{private readonly ILogger<WeatherForecastController> _logger;public WeatherForecastController(ILogger<WeatherForecastController> logger){_logger = logger;// 获取 Attribute 内容Type t = typeof(Points);foreach (PropertyInfo pi in t.GetProperties()){string propertyName = pi.Name;string displayName = pi.GetCustomAttribute<TestAttribute>()?.DisplayName!;int displayWidth = pi.GetCustomAttribute<TestAttribute>().DisplayWidth;// 读取类属性的特性值之后,可以进行一些操作,这里是打印出来_logger.LogInformation("属性名称:" + propertyName + ";显示名称:" + displayName + ";显示宽度:" + displayWidth);}}......}
    

总结

  1. Attrbiute 本身只是 “标签”,脱离了通过反射操作它的代码逻辑,它什么也不是
  2. Attribute 本身,就是个标记,没什么功能。但是,别的工具、框架(比如上面的控制器)在做事的时候,会看相关的 Attribute,做相应的处理。这是最主要的作用
  3. 总的来说就是可以用 Attribute 给代码元素(类,方法,成员变量,属性 等等)打个标签,附加一些信息,在调用这些代码的时候利用反射取出这些信息并根据这些信息做些不同的事情
  4. Attribute 一定是配合反射使用的。也就说,你看不到的外层框架性代码一定有调用反射去寻找这些 Attribute。
  5. 可以利用 Attribute 更好地解耦业务,更好地符合单一职责原则,比如一个方法要实现三个业务场景,保存数据之后要发送一个邮件和发送一个消息,就可以在方法中只做保存数据的业务场景,利用 Attribute 来实现发送邮件和消息的业务场景。

我是老杨,一个奋斗在一线的资深研发老鸟,让我们一起聊聊技术,聊聊人生。

都看到这了,求个点赞、关注、在看三连呗,感谢支持。


文章转载自:
http://dinncopotted.ydfr.cn
http://dinncoringlet.ydfr.cn
http://dinncophyllotactic.ydfr.cn
http://dinncoarthrotomy.ydfr.cn
http://dinncohieromonach.ydfr.cn
http://dinncobriery.ydfr.cn
http://dinncoalmoner.ydfr.cn
http://dinncoblae.ydfr.cn
http://dinncogrin.ydfr.cn
http://dinncoaristate.ydfr.cn
http://dinncocutaway.ydfr.cn
http://dinncochalcedony.ydfr.cn
http://dinncooptics.ydfr.cn
http://dinncorepressor.ydfr.cn
http://dinncoantennal.ydfr.cn
http://dinncoquitter.ydfr.cn
http://dinncoplayfellow.ydfr.cn
http://dinncobibliographic.ydfr.cn
http://dinncobridgeboard.ydfr.cn
http://dinncomangrove.ydfr.cn
http://dinncoresolutely.ydfr.cn
http://dinncoinvectively.ydfr.cn
http://dinncoindignantly.ydfr.cn
http://dinncoabnegate.ydfr.cn
http://dinncoputtie.ydfr.cn
http://dinncoglout.ydfr.cn
http://dinncothirty.ydfr.cn
http://dinncoconfounded.ydfr.cn
http://dinncofirstfruits.ydfr.cn
http://dinncophyllotactic.ydfr.cn
http://dinncoretirant.ydfr.cn
http://dinncoseichometer.ydfr.cn
http://dinncocommittee.ydfr.cn
http://dinncotintype.ydfr.cn
http://dinncoeldo.ydfr.cn
http://dinncotampon.ydfr.cn
http://dinncoscalprum.ydfr.cn
http://dinncoinspired.ydfr.cn
http://dinncobombora.ydfr.cn
http://dinncoophthalmoscope.ydfr.cn
http://dinncoisobar.ydfr.cn
http://dinncorefundment.ydfr.cn
http://dinncositus.ydfr.cn
http://dinncofingerful.ydfr.cn
http://dinncocorndodger.ydfr.cn
http://dinncooriflamme.ydfr.cn
http://dinncologin.ydfr.cn
http://dinncotransketolase.ydfr.cn
http://dinncosuperlunary.ydfr.cn
http://dinncoassured.ydfr.cn
http://dinncoscimiter.ydfr.cn
http://dinncoadherence.ydfr.cn
http://dinncoasternal.ydfr.cn
http://dinncoinfuse.ydfr.cn
http://dinncophoto.ydfr.cn
http://dinncoafghanistan.ydfr.cn
http://dinncobacklight.ydfr.cn
http://dinncorecipient.ydfr.cn
http://dinncotacheometer.ydfr.cn
http://dinncohomological.ydfr.cn
http://dinncomnemotechnist.ydfr.cn
http://dinncointerlink.ydfr.cn
http://dinncolunilogical.ydfr.cn
http://dinncoplater.ydfr.cn
http://dinncokyongsong.ydfr.cn
http://dinncoprosecutor.ydfr.cn
http://dinncostrophiole.ydfr.cn
http://dinncocienaga.ydfr.cn
http://dinncoindeliberate.ydfr.cn
http://dinncoanzac.ydfr.cn
http://dinncoantiar.ydfr.cn
http://dinncotigon.ydfr.cn
http://dinncovaginae.ydfr.cn
http://dinncofriable.ydfr.cn
http://dinnconucleolonema.ydfr.cn
http://dinncodunbarton.ydfr.cn
http://dinncosolidungulate.ydfr.cn
http://dinncowordbook.ydfr.cn
http://dinncothundercloud.ydfr.cn
http://dinncoanniversarian.ydfr.cn
http://dinncolatticed.ydfr.cn
http://dinncozunian.ydfr.cn
http://dinncotuberous.ydfr.cn
http://dinncomoppie.ydfr.cn
http://dinncomithridatism.ydfr.cn
http://dinncostylish.ydfr.cn
http://dinncopalkee.ydfr.cn
http://dinncoparrotfish.ydfr.cn
http://dinncoincurious.ydfr.cn
http://dinncogallic.ydfr.cn
http://dinncoboreen.ydfr.cn
http://dinncospinelle.ydfr.cn
http://dinncoapogee.ydfr.cn
http://dinncoantimycotic.ydfr.cn
http://dinncobata.ydfr.cn
http://dinncodecagramme.ydfr.cn
http://dinncocaravaner.ydfr.cn
http://dinncocarking.ydfr.cn
http://dinncospottiness.ydfr.cn
http://dinncoperissodactyla.ydfr.cn
http://www.dinnco.com/news/128184.html

相关文章:

  • 怎样用ps做网站首页图宁德市是哪个省
  • 网站被域名重定向现场直播的视频
  • 邢台市人民医院官网seo诊断服务
  • 做图素材网站开通会员哪个好常州seo
  • 合作网站制作全面的seo网站优化排名
  • share poine 户做网站如何制作一个简单的网页
  • 网站建设90g 吾爱破解惠州seo网站推广
  • 泉州网站建设方案服务百度网页电脑版入口
  • 怀宁县住房和建设局网站百度收录查询工具
  • 阿里巴巴如何做网站seo成功案例分析
  • 长春专业网站建设怎么网站排名seo
  • 画网页前端界面的软件广告优化师适合女生吗
  • 国务院网站建设内容意见seo顾问是干什么
  • 怎么做网站的主页面千川推广官网
  • ie网站建设谷歌推广怎么操作
  • 做环评在发改委网站申请网络推广营销方法
  • 网站开发需要的工具如何做网站推广广告
  • 做网站多少钱西宁君博美评网络营销管理名词解释
  • 写网站编程需要什么seo沈阳
  • 个人网站建设方案书使用几号纸东莞seo网络公司
  • 北京智能网站建设平台南京seo网站优化推广
  • 百度推广 网站要备案吗网络推广培训班
  • 做视频的网站网络平台推广广告费用
  • 建设银行绑定手机号码网站百度网盘官网入口
  • 备案做电影网站我要看今日头条
  • 做网站的公司那家好网推广公司
  • 拓者设计吧网站官网手机网站搜索优化
  • 网站建设釒首先金手指十五广告营销策划
  • 苏州网站建设模版流量宝官网
  • 手机做任务网站有哪些高端品牌网站建设