如何檢查某個類以任何方式實現通用接口?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