So, I have the following code:
public interface ICommand<out TResponse> {...}
public interface IOperationResult {...}
public interface IOperationResult<out T> : IOperationResult {...}
public struct OperationResult : IOperationResult {...}
public struct OperationResult<T> : IOperationResult<T> {...}
public sealed class AuthorizationBehavior<TRequest, TResponse>
where TRequest : class, ICommand<IOperationResult>
where TResponse : IOperationResult {...}
public class CreateCommand : ICommand<OperationResult<string>>{...}
Now, if I try do do a new AuthorizationBehavior<CreateCommand, OperationResult<string>>()
the compiler errors "The type 'CreateCommand' cannot be used as type parameter 'TRequest' in the generic type or method 'AuthorizationBehavior'. There is no implicit reference conversion..."
But, if instead of structs I make both OperationResult
a class, everything is fine. Why?
I would like to maintain the structs as those results are usually very short lived and small, but also I can't change the constrains on AuthorizationBehavior
to use the concrete type instead of the interface (because that constrain let me use both types of OperationResult
)