在 C# 中键入种姓 Serilog

Type caste Serilog in c#

 public class WorkletSelectionForm : Form
 {  
        private readonly ILogger _logger;

        public WorkletSelectionForm( ILogger<WorkletSelectionForm> logger)
        {
            _logger = logger;
        }

        private void btnClick_click(object sender, System.EventArgs e)
        {
            MainForm _frm = new MainForm((ILogger<MainForm>)_logger);
        }
 }

正在获取 TypeCaste 运行-时间错误:无法将 ILogger 转换为 ILogger

您可能还需要将 ILogger<MainForm> 注入构造函数,这些类型和实例不可互换。

试试这个

public class WorkletSelectionForm : Form
     {  
            private readonly ILogger _logger;
            private readonly ILogger<MainForm> _mainFormLogger;
    
            public WorkletSelectionForm(
                ILogger<WorkletSelectionForm> logger,
                ILogger<MainForm> mainFormLogger)
            {
                _logger = logger;
                _mainFormLogger = mainFormLogger;
            }
    
            private void btnClick_click(object sender, System.EventArgs e)
            {
                MainForm _frm = new MainForm(_mainFormLogger);
            }
     }