c# - 根据构造函数中的条件向基构造函数传递不同的值
问题描述
我有一个构造函数如下
public class Person : Animal
{
public Person(Settings settings) : base(settings.ActionProperty1)
{
}
}
public class Animal
{
public Animal(Action a)
{
}
}
public class Settings
{
public bool Flag { get; set; }
public Action ActionProperty1 { get; set; }
public Action ActionProperty2 { get; set; }
}
如何根据某些条件向基本构造函数传递不同的值。即类似于以下内容。
public Person(Settings settings) : base(settings.flag ? settings.ActionProperty1 : settings.ActionProperty2)
{
}
以上给了我一个编译错误:CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between 'method group' and 'method group'
此外,我无法通过在基类中添加属性设置器来解决此问题。我需要在对基础的构造函数调用中执行此操作
编辑:答案在此处链接的第二个副本中提供。无法确定条件表达式的类型 (Func)我不得不将可选参数之一强制转换为操作。谢谢
解决方案
推荐阅读
- rust - 意外的“找不到方法”编译器错误
- sql - 使用 SQL 最小化图形
- python - 使用 docker-compose 的 Python 应用程序的 Nginx 反向代理
- python - 如何在python中绘制生存树
- ios - iOS 中浏览器的 navigator.getUserMedia 的原生替代方案是什么?
- ios - UIScrollView 内容插图无法移除?
- ios - 如何在 SwiftUI 中排序(按:)列出正确的方式?
- groovy - spock 在这个测试中是如何调用这个函数的?
- flutter - 如何删除网格视图生成的空白
- docker - Docker 失败并显示“无法启动容器:超时等待容器启动”