c# - 公开包含不同类型的相同接口的属性的通用接口
问题描述
我有一个通用接口,它需要一个属性(父),它是相同的接口,但类型不同。我将如何实现这一目标?
public interface IConfigurator<T1>
{
string TableName { get; }
PropertyMapper<T1> PropertyMap { get; }
IConfigurator<T2> ParentConfigurator {get;set;} // this line is not valid c# code
}
我可以通过以下方式实现相同的目标,但如果适用,我想利用 c# 的属性:
ITypeConfigurator<T> GetParent<T>();
void AddParent<T>(ITypeConfigurator<T> parent);
解决方案
最好只使用一个object
public interface IConfigurator<T>
{
string TableName { get; }
PropertyMapper<T> PropertyMap { get; }
object ParentConfigurator {get;set;}
}
因为如果你在这条通用路径上走得太多了,它会变得有点混乱
public interface IConfigurator<T,TParent>
{
string TableName { get; }
PropertyMapper<T> PropertyMap { get; }
IConfigurator<TParent,??> ParentConfigurator { get; } // what are you going to put here for ?? object maybe
}
除非你制作另一个界面
public interface IConfigurator<T>
{
string TableName { get; }
PropertyMapper<T> PropertyMap { get; }
}
public interface IConfigurator<T,TParent> : IConfigurator<T>
{
IConfigurator<TParent> ParentConfigurator { get; } // what are you going to put here for ?? object maybe
}
推荐阅读
- c++ - 在 Torch C++ API 中,如何快速写入张量的内部数据?
- mamp - Mac OS Catalina 中的 MAMP 5.5 安装失败
- postman - 如何在 Postman 中标记测试
- css - z-index 在动画容器的子元素上被忽略
- javascript - 如何从包含来自另一个应用程序的 DB 查询的 Node.js 函数获取返回值
- macos - 当我 `brew update` 发现 `https://github.com/Homebrew/homebrew-dupes/` 未找到
- mysql - 在 MySQL 中获取每月总计
- pagination - Yii2 Kartik Gridview Editable 不适用于分页
- mysql - 在 Account 中拥有唯一的组名,拥有子账户
- grails - 如何从groovy中的字符串中提取带有数字的给定字符串数组