How do I check that a class implements a generic interface in any way?
I have the following:
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; }
}
Is there a way to use this interface to check that a parameter implements the generic interface without supplying T? Passing the base class doesn't match, and using the SomeQueryDto class would defeat the point
private static string BuildQueryObjectString<T>(T dto)
where T : IQueryObject<?>
{
//use query field in method body
dto.query= foo;
}
I could change the interface to implement another non generic interface and check that but then classes could just use this and not have the generic interface at all:
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
}