第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將類(lèi)注冊(cè)為接口但調(diào)用方法?

如何將類(lèi)注冊(cè)為接口但調(diào)用方法?

C#
胡說(shuō)叔叔 2023-07-23 16:51:43
我復(fù)制了下面的代碼,它工作得很好,但我已經(jīng)修改了它以滿(mǎn)足我的需要,通過(guò)將 I 添加到當(dāng)前名稱(chēng)來(lái)創(chuàng)建一個(gè)具有DatabaseSettings相同UserSettings后綴的接口。但問(wèn)題是它試圖將接口注冊(cè)為接口,這是錯(cuò)誤的?在進(jìn)行更改之前,settings變量只有兩個(gè)條目,現(xiàn)在我已經(jīng)添加了接口,settings正在拾取接口(因?yàn)楹缶Y),因此它現(xiàn)在具有條目,而不是將它們添加為條目,我想將它們與相應(yīng)的類(lèi)一起使用并仍然調(diào)用.LoadSection(type)public class SettingsModule : Module{? ? private readonly string _configurationFilePath;? ? private readonly string _sectionNameSuffix;? ? public AeSettingsModule(string configurationFilePath, string sectionNameSuffix = "Settings")? ? {? ? ? ? _configurationFilePath = configurationFilePath;? ? ? ? _sectionNameSuffix = sectionNameSuffix;? ? }? ? protected override void Load(ContainerBuilder builder)? ? {? ? ? ? var settings = Assembly.Load(nameof(DataLayer))? ? ? ? ? ? .GetTypes()? ? ? ? ? ? .Where(t => t.Name.EndsWith(_sectionNameSuffix, StringComparison.InvariantCulture))? ? ? ? ? ? .ToList();? ? ? ? ?settings.ForEach(type =>? ? ? ? ?{? ? ? ? ? ? ?builder.Register(c => c.Resolve<ISettingsReader>().LoadSection(type))? ? ? ? ? ? ?.As(type)? ? ? ? ? ? ?.SingleInstance();? ? ? ? ?});? ? }}public class DatabaseSettings: IDatabaseSettings{? ? public string ConnectionString { get;? set; }? ? public int TimeoutSeconds { get; set; }}public interface IDatabaseSettings{? ? string ConnectionString { get; set; }? ? int TimeoutSeconds { get; set; }}我收到的錯(cuò)誤消息是:`System.MissingMethodException: 'Cannot create an instance of an interface.'`因?yàn)槲乙呀?jīng)將`構(gòu)造函數(shù)注入從類(lèi)更改為接口:public UserService(IDatabaseSettings databaseSettings, IUserSettings userSettings){? ? ...}因?yàn)槲乙呀?jīng)添加了接口并且它具有相同的前綴&ldquo;設(shè)置&rdquo;,所以它選擇了我不想要的接口,而是我想將它與相應(yīng)的類(lèi)一起使用?我正在嘗試執(zhí)行此操作(但使用上面的語(yǔ)法,因?yàn)槲襆oadSection也想調(diào)用):builder.RegisterType<DatabaseSettings>().As<IDatabaseSettings>();
查看完整描述

1 回答

?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊

我通過(guò)反復(fù)試驗(yàn)找到了一個(gè)解決方案,該應(yīng)用程序按預(yù)期工作,但我不確定它是否高效、可靠等。


我發(fā)帖只是為了獲得反饋,如果它有效,其他人也許可以使用它。


var settings = Assembly.Load(nameof(DataLayer))

    .GetTypes()

    .Where(t => t.Name.EndsWith(_sectionNameSuffix, StringComparison.InvariantCulture ) && !t.IsInterface)

    .ToList();


    settings.ForEach(type =>

    {

        builder.Register(c => c.Resolve<ISettingsReader>().LoadSection(type))

            .As(type.GetInterfaces())

            .AsImplementedInterfaces()

            .InstancePerLifetimeScope();

    });


查看完整回答
反對(duì) 回復(fù) 2023-07-23
  • 1 回答
  • 0 關(guān)注
  • 117 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)