c# - Conditional inheritance: base class depend on environment variable
问题描述
I have two abstracts classes, 'ValidationsWithStorage' inherits 'Validations'
public abstract class Validations {
// methods..
}
public abstract class ValidationsWithStorage : Validations {
// ...
}
I also have a class:
public abstract class TestsValidations : T
T should be depend on the environment variable:
Environment.GetEnvironmentVariable("useStorage")
If this variable is null I want that T will be Validations. Else, I want that T will be ValidationsWithStorage.
What is the best way to do it?
Thanks
解决方案
我不认为你可以按照你的方式做你想做的事。
为什么不让您的类TestValidations
在其构造函数中使用类型Validations
或ValidationsWithStorage
. 如果它们都遵循相同的接口,那么您的TestsValidations
类就不需要知道(或关心)它正在使用的两者中的哪一个。
所以基本上:
Validations
为您和ValidationsWithStorage
班级创建一个界面- 检查您的环境变量
TestsValidation
根据环境变量将正确的类传入构造函数
这有帮助吗?
推荐阅读
- spring - @Autowired Bean 返回 null
- nativescript - 导航到尚未使用的框架
- python - 如何取这两个不同形状数组的点积?
- dynamics-crm - PowerApps - 为 CRM 中的约会“设置相关”
- spring-boot - spring boot docker应用我们应该怎么做nginx反向代理配置呢?
- javascript - Material UI 如何为子组件设置外部样式,整个组件使用一种主要的通用样式
- javascript - 如何在 Vue.js 或 Nuxt.js 中的 First Contentful Paint 或第一个元素之后加载组件?
- html - HTML 对象 VBA
- python - 在具有初始条件的连续块上过滤后重新采样(以避免不连续性)
- bokeh - 将 HoloViews 绘图与 Bokeh 自定义链接