C# 确定类是否实现了泛型接口,然后调用接口方法
我正在尝试实现一个接口:C# 确定类是否实现了泛型接口,然后调用接口方法,c#,asp.net-core,C#,Asp.net Core,我正在尝试实现一个接口: IListDto<TListDto> IListDto 在我的数据存储库中,检查类是否实现了该接口,如果实现了,则调用 由该接口定义的方法。我没有多大的成功。详情如下 提前感谢您提供的任何帮助 // I have a generic interface public interface IListDto<TListDto> { TListDto ToListDto(); } // This is an exaple of TList
IListDto<TListDto>
IListDto
在我的数据存储库中,检查类是否实现了该接口,如果实现了,则调用
由该接口定义的方法。我没有多大的成功。详情如下
提前感谢您提供的任何帮助
// I have a generic interface
public interface IListDto<TListDto>
{
TListDto ToListDto();
}
// This is an exaple of TListDto
public class ProductDto
{
public Guid ProductId { get; set; }
public string ProductName { get; set; }
}
// The product class implements IListDto<ProductDto>
public class Product : IListDto<ProductDto>
{
public Guid Id { get; set; }
public string ProductName { get; set; }
// ...other properties
// Implementation of IListDto<ProductDto>
public ProductDto ToListDto()
{
return new ProductDto()
{
ProductId = this.Id,
ProductName = this.ProductName
}
}
}
// I want to have a generic method in my Data Repository that can determine
// if a class implements TListDto. If it does, it converts the a list from
// List<TEntity> to List<TListDto> by calling the objects TListDto() implementation
public List<TListDto> ToDtoList(List<TEntity> list)
{
// The following is more or less pseudo-code as I am at a loss of where to take it from here...
// Test to see of TEntity implements IListDto
// ERROR 1: This test fails, even though TEntity implements IListDto
if (list is List<IListDto<TListDto>>)
{
// Yes, IListDto is implemented
// Cast list to List<IListDto>, the next line of code seems to work
List<IListDto<TListDto>> iList = list as List<TListDto>;
// ERROR 2: This line causes a nasty error message (System.NotSupportedException: Serialization
// and deserialization of 'System.Type' instances are not supported and should be avoided
/// since they can lead to security issues. Path: $.TargetSite.DeclaringType. etc...
var dtoList = iList.Select(s => s.ToListDto()).ToList();
return dtoList;
}
else
{
// No, throw not implemented error
throw new ApplicationException("Entity does not implement IListDto");
}
}
//我有一个通用接口
公共接口
{
TListDto ToListDto();
}
//这是TListDto的一个例子
公共类产品
{
公共Guid ProductId{get;set;}
公共字符串ProductName{get;set;}
}
//product类实现了IListDto
公共类产品:IListDto
{
公共Guid Id{get;set;}
公共字符串ProductName{get;set;}
//……其他财产
//IListDto的实施
公共产品ToListTo()
{
将新产品返回到()
{
ProductId=this.Id,
ProductName=this.ProductName
}
}
}
//我希望在我的数据存储库中有一个通用方法,可以确定
//如果类实现了TListDto。如果是,它将从
//通过调用对象TListDto()实现来列出
公共列表TodList(列表列表)
{
//下面是或多或少的伪代码,因为我不知道从哪里开始。。。
//测试看触角和触角
//错误1:此测试失败,即使TEntity实现了IListDto
如果(列表是列表)
{
//是的,IListDto已经实现
//从一个列表到另一个列表,下一行代码似乎有效
List iList=列表为列表;
//错误2:此行导致严重错误消息(System.NotSupportedException:Serialization
//“System.Type”实例的反序列化不受支持,应避免
///因为它们可能导致安全问题。路径:$.TargetSite.DeclaringType.etc。。。
var dtoList=iList.Select(s=>s.ToListDto()).ToList();
返回数据集成商;
}
其他的
{
//否,抛出未实现错误
抛出新的ApplicationException(“实体未实现IListTo”);
}
}
我几乎可以肯定,您所采用的方法并不适合您试图解决的高层次问题,因此,如果您能准确解释问题所在,我将非常高兴,这样我可以给您提供更好的建议
他说,对你提出的问题的回答是:
我希望在我的数据存储库中有一个通用方法,可以确定
如果类实现了TListDto。如果实现了,则将从
通过调用对象TListDto()实现来列出
试试看{
var iList=list.Cast();
返回iList.Select(s=>s.ToListDto()).ToList();
}捕获(无效卡斯特例外){
抛出新的ApplicationException(“实体未实现IListTo”);
}
我几乎可以肯定,您所采用的方法并不适合您试图解决的高层次问题,因此,如果您能准确解释问题所在,我将非常高兴,这样我可以给您提供更好的建议
他说,对你提出的问题的回答是:
我希望在我的数据存储库中有一个通用方法,可以确定
如果类实现了TListDto。如果实现了,则将从
通过调用对象TListDto()实现来列出
试试看{
var iList=list.Cast();
返回iList.Select(s=>s.ToListDto()).ToList();
}捕获(无效卡斯特例外){
抛出新的ApplicationException(“实体未实现IListTo”);
}
在手机上如此简短的回答:查找协方差,这应该可以解释为什么你认为应该相同的东西实际上是不同的。在我看来,这似乎是一个XY问题。相反,你能解释一下你试图解决的是什么高层次问题,而不是你正在寻求的解决方案吗?另外,我不清楚你所能理解的背景是什么ll“数据存储库”.那是一个类吗?TListDto和TEntity都是该类的通用参数吗?我不明白List
与IListTo
或List
有什么关系,它们都是不同的类型,没有通用的接口或基本类型,所以响应很短:查找协方差,这应该可以解释为什么你认为uld是相同的,但实际上是不同的。在我看来,这似乎是一个XY问题。你能解释一下你试图解决的是什么高级问题,而不是你正在寻求的解决方案吗?另外,我不清楚你称之为“数据存储库”的上下文是什么.那是一个类吗?TListDto和TEntity都是该类的通用参数吗?我不明白List
与IListDto
或List
有什么关系。它们都是不同的类型,没有共同的接口或基本类型Rodrigo:我共享的代码是真实版本的一个相当简化的版本。简单地说,我拥有一个包含大约20个实体(类)的数据库以及一个包含数十个端点的API,其中大多数端点发送分页的数据列表。这些列表由实体中属性的子集组成。属性列表由TListDto定义。每个实体负责通过实现ToListDto将其属性映射到其TListDto。Rodrigo:我共享的代码是一个相当简化的版本简单地说,我有一个包含大约20个实体(类)的数据库以及一个包含数十个端点的API,其中大多数端点发送分页的数据列表。这些列表由实体中属性的子集组成。属性列表由TListDto定义。每个实体负责通过实现一个ToListDto将其属性映射到其TListDto。
try {
var iList = list.Cast<IListDto<TListDto>>();
return iList.Select(s => s.ToListDto()).ToList();
} catch (InvalidCastException) {
throw new ApplicationException("Entity does not implement IListDto");
}