如何检查某个类以任何方式实现通用接口?C#检查通用接口的实现
我有以下几点:
public class SomeQueryObject : IQueryObject<SomeQueryDto>
{
public SomeQueryDto query { get; set; } = new SomeQueryDto();
}
public class SomeQueryDto : BaseQueryDto
{
// some properties
}
public class BaseQueryDto
{
// some properties
}
public interface IQueryObject<T> where T : BaseQueryDto
{
T query { get; set; }
}
是否有使用这个接口来检查参数实现,而无需提供T上的通用接口的方法吗? 传递基类的不匹配,并使用SomeQueryDto类会破坏点
private static string BuildQueryObjectString<T>(T dto)
where T : IQueryObject<?>
{
//use query field in method body
dto.query= foo;
}
我可以改变的接口来实现另一个非通用接口,并检查,但随后类可以只使用这个,而不是具有通用接口都:
public interface IQueryObject<T> : IQueryObject where T : BaseQueryDto
{
T query { get; set; }
}
public interface IQueryObject { }
public class SomeQueryObject : IQueryObject
{
// no query field
}
private static string BuildQueryObjectString<T>(T dto)
where T : IQueryObject // kind of pointless, the above class would pass this check but doesn't implement the field
{
//method body, no access to query field
dto.query= foo; // error
}
什么是IUDGroupQueryObject? – Scrobi
我的不好,我是为了清晰而重新命名的东西,并错过了 – lanky393