我有以下代码:
public interface IMenuItem
{
IWebElement Parent { get; }
}
Run Code Online (Sandbox Code Playgroud)
public class MenuItems
{
public T GetMenuItem<T>() where T : IMenuItem, new()
{
var menuItem = new T();
return menuItem;
}
}
Run Code Online (Sandbox Code Playgroud)
public abstract class Page : IEnumerable<IComponent>
{
private Func<IMenuItem> _getMenuItems = new MenuItems().GetMenuItem<IMenuItem>;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将该new MenuItems().GetMenuItem<IMenuItem>函数存储在_getMenuItems字段中,但由于这是一个通用函数,因此无法正常工作。如何将泛型函数存储到变量中?
这样做是new MenuItems().GetMenuItem<IMenuItem>行不通的,它告诉我:
“IMenuItem”必须是具有公共无参数构造函数的非抽象类型,以便将其用作泛型类型或方法“MenuItems.GetMenuItem()”中的参数“T”
现在我有许多具体的类型来实现IMenuItem我希望委托接受这些类型。我不想为每个实现IMenuItem. 相反,我有一个可以接受实现IMenuItem.
在 C# 中,Math.Max() 的实现如下:
[NonVersionable]
public static int Max(int val1, int val2)
{
return (val1 >= val2) ? val1 : val2;
}
Run Code Online (Sandbox Code Playgroud)
虽然我相信它也可以实现为:
C# 的实现是否比数学实现有任何优势,例如效率?
(a + b + Math.Abs(a-b)) /2
我的项目根目录中有一个 _config.yml YAML 文件。
我在 Startup.cs 中这样做:
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddYamlFile("_config.yml");
Configuration = builder.Build();
Run Code Online (Sandbox Code Playgroud)
当我构建并运行该项目时,出现以下错误:
System.IO.FileNotFoundException: The configuration file '_config.yml' was not found and is not optional. The physical path is 'C:\Users\user\OneDrive\Documenten\Descent.Bot\bin\Debug\n
etcoreapp3.1\_config.yml'.
难道不应该通过构建项目来自动添加 YAML 文件吗bin\Debug?有没有办法添加 YAML 文件而bin\Debug无需我自己手动添加?