オープンジェネリック型定義がオープンジェネリックインタフェースを実装しているかどうかを確認しようとしています。私はType.GetInterfaces()
メソッドから返される型はtypeof(IService<>)
から返された型と一致しないことが判明ジェネリック型定義が別のジェネリック型定義から継承するかどうかを確認する方法
public interface IService<T> { }
public class ServiceImpl<T> : IService<T> { }
private static bool OpenGenericTypeImplementsOpenGenericInterface(
Type derivedType, Type interfaceType)
{
return derivedType.GetInterfaces().Contains(interfaceType);
}
[TestMethod]
public void Verify()
{
Type openGenericImplementation = typeof(ServiceImpl<>);
Type expectedInterfaceType = typeof(IService<>);
bool implDoesImplementInterface = OpenGenericTypeImplementsOpenGenericInterface(
openGenericImplementation, expectedInterfaceType);
// This assert fails. Why?
Assert.IsTrue(implDoesImplementInterface);
}
:以下のサンプルを見てください。私はそれがなぜ、そして何かジェネリック型定義が他のジェネリック型定義を継承し実装するかどうかを正しく検証する方法を理解できません。
ここでは何が起こっていますか、どのように解決すればこの問題を解決できますか?
In Line:Assert.IsTrue(OpenGenericTypeImplements); OpenGenericTypeImplementsとは何ですか? – HABJAN
@ハブヤン:そうです。タイプミス。修正しました。 – Anne