您的位置: 游戏资讯 > 手游攻略

。Net6基本功能包共享4 (Profile Reading)

来源:头条 浏览:0 2022-12-19 10:49:01

在开发过程中,无论如何都需要配置文件。 为了便于获取配置文件的参数数据,我们根据自己的使用习惯封装了配置文件参数的读取。 首先贴上配置文件

来看一下. net 6中配置参数的读取方法,首先注入IConfiguration界面

。Net6基本功能包共享4 (Profile Reading)

[自动]公共配置配置{ get; set; //配置文件参数的获取方法1

boolisprintlog=configuration.getvalue ( is printlog ); string name3=configuration.getvalue ( student:name ); //轮廓参数获取方法2

string name=configuration.getsection ( ' student ' ) ['name']; string name2=configuration [ ' student:name ' ]; studentconfigurationstu2=configuration.getsection ( ' student ' ).get ); list strlist=configuration.getsection ( ' strlist ' ).get ); list book list=configuration.getsection ( student:books ).get ); 其实使用起来很方便了。 我自己进行包的读取方法

//读取对象studentconfigurationstu3=configuration.gett (; //集合list class list=configuration.getlistt (; 共享封装步骤。 1 .定义配置参数标签的特性

[ attribute usage ( attribute targets.class ) ] publicclassconfigtagattribute:attribute ) publicstringconfigkey{get; set; } publicconfigtagattribute ( string key ) { ConfigKey=key; }2.根据配置文件参数模板定义目标模型

//ConfigTag设置配置文件中的参数级别。 参数级别按[configtag('student:books )//config tag ( ' student ' ) ]publicclassstudentconnt )的方式进行拼接使用; } public int age { get; set; }公共列表books { get; set; } public ClassModel classInfo { get; set; }3.定义I configuration的扩展方法,扩展获取对象的方法和获取对象集合的方法

publicstaticclassconfigurationextension { publicstatictgett ( thisiconfigurationconfiguration ) tt=default; typetype=typeof(t; configtagattributeconfigtagattribute=type.getcustomattribute (; if ( configtagattribute==null ( { returnt; } stringconfigpath=configtagattribute.config key; 配置路径==空| |配置路径==' ' ( { returnt; } t=configuration.getsection ( config path ).get ); 返回t; } publicstaticlistgetlistt ( thisiconfigurationconfiguration ) { List list=null; typetype=typeof(t; configtagattributeconfigtagattribute=type.getcustomattribute (; if ( configtagattribute==null ( { return list; } stringconfigpath=configtagattribute.config key; 配置路径==空| |配置路径==' ' ( { return list; } list=configuration.getsection ( config path ).get ); 返回列表; }4.获取基础类型( int、string、bool等)的方法没有封装,本机可以获取的使用位置可以注入IConfiguration,然后调用扩展方法获取

和平精英体验服官网「V3.02」IOS版

和平精英体验服官网「V3.02」IOS版

  • 分类:资讯阅读
  • 大小:17MB
  • 语言:简体中文
  • 版本:V3.02