var architect = builder.Configuration.GetSection("AppInfo:Author:Architect").Value;
var features1=builder.Configuration.GetSection("AppInfo:Features:1").Value;
用record綁定
record
public record AppInfoRecord(string? Name, string? Version, AuthorRecord? Author, string[]? Features);
public?record?AuthorRecord(string??Architect,?string??Programmer,?string??Designer);
綁定
var authorRecord = new AuthorRecord(null, null, null);
var appInfoRecord = new AppInfoRecord(null, null, authorRecord, null);
builder.Configuration.GetSection("AppInfo").Bind(appInfoRecord);
結果
用class綁定
class
public class AppInfo
{public string? Name { get; set; }public string? Version { get; set; }public Author? Author { get; set; }public string[]? Features { get; set; }
}
public class Author
{public string? Architect { get; set; }public string? Programmer { get; set; }public string? Designer { get; set; }
}
綁定
var appInfo = new AppInfo();
builder.Configuration.GetSection("AppInfo").Bind(appInfo);
var authorRecord = new AuthorRecord(null, null, null);
var appInfoRecord = new AppInfoRecord(null, null, authorRecord, null);
builder.Configuration.GetSection("AppInfo").Bind(appInfoRecord);
builder.Services.AddSingleton(appInfoRecord);