Reputation: 1885
I have the following:
public interface IContract
{
//...
}
public interface IContractChannel : IContract, IClientChannel
{
//....
}
public class myClient : System.ServiceModel.ClientBase<IContractChannel>
{
///...
}
I need to determine if my client is of type ClientBase at runtime. I'm trying
myClientType.IsSubClassOf(ClientBase<IClientChannel>);
it returns false. I don't have the IContractChannel type in hand. How can I know if my type inherits of ClientBase?
Upvotes: 0
Views: 133
Reputation: 6050
ClientBase<IContractChannel>
is not the same type as ClientBase<IClientChannel>
, also IContractChannel
inherits from IClientChannel
, it doesn't mean ClientBase<IContractChannel>
inherits from ClientBase<IClientChannel>
. when the template class instantiates, they're different types.
So it should be false.
What you want to achieve, this is a term for this: Contravariance. In MSDN, there's a topic about this: Covariance and Contravariance in Generics. Contravariance enables you to use a more generic (less derived) type than originally specified, such as You can assign an instance of IEnumerable<Base>
to a variable of type IEnumerable<Derived>
.
In C#, you can create Variant Generic Interfaces to support Contravariance, but for you case, the generic interface is already there.
Upvotes: 2
Reputation: 54638
You are probably looking for is:
Console.WriteLine(myClientType.BaseType.GetGenericTypeDefinition()
== typeof(ClientBase<>));
Upvotes: 2
Reputation: 17327
You can either look at the type of the Channel myClient.Channel.GetType()
, or you can look at myClientType.GenericTypeArguments
to get the TChannel type, then check it.
Upvotes: 0