형식이 특정 일반 인터페이스 형식을 구현하는지 확인하는 방법
다음 유형 정의를 가정하십시오.
public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}
맹 글링 된 형식 만 사용할 수있는 경우 형식 Foo
이 일반 인터페이스를 구현 하는지 여부를 어떻게 알 IBar<T>
수 있습니까?
TcK의 답변을 사용하여 다음 LINQ 쿼리로 수행 할 수도 있습니다.
bool isBar = foo.GetType().GetInterfaces().Any(x =>
x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(IBar<>));
상속 트리를 살펴보고 트리에서 각 클래스의 모든 인터페이스를 찾아 인터페이스가 일반 인지 여부typeof(IBar<>)
를 호출 한 결과 와 비교 해야합니다 . 확실히 조금 아 ful니다.Type.GetGenericTypeDefinition
참조 이 대답 하고 이러한 것들을 더 많은 정보와 코드를.
public interface IFoo<T> : IBar<T> {}
public class Foo : IFoo<Foo> {}
var implementedInterfaces = typeof( Foo ).GetInterfaces();
foreach( var interfaceType in implementedInterfaces ) {
if ( false == interfaceType.IsGeneric ) { continue; }
var genericType = interfaceType.GetGenericTypeDefinition();
if ( genericType == typeof( IFoo<> ) ) {
// do something !
break;
}
}
헬퍼 메소드 확장
public static bool Implements<I>(this Type type, I @interface) where I : class
{
if(((@interface as Type)==null) || !(@interface as Type).IsInterface)
throw new ArgumentException("Only interfaces can be 'implemented'.");
return (@interface as Type).IsAssignableFrom(type);
}
사용법 예 :
var testObject = new Dictionary<int, object>();
result = testObject.GetType().Implements(typeof(IDictionary<int, object>)); // true!
약간 간단한 버전의 @GenericProgrammers 확장 방법을 사용하고 있습니다.
public static bool Implements<TInterface>(this Type type) where TInterface : class {
var interfaceType = typeof(TInterface);
if (!interfaceType.IsInterface)
throw new InvalidOperationException("Only interfaces can be implemented.");
return (interfaceType.IsAssignableFrom(type));
}
용법:
if (!featureType.Implements<IFeature>())
throw new InvalidCastException();
You have to check against a constructed type of the generic interface.
You will have to do something like this:
foo is IBar<String>
because IBar<String>
represents that constructed type. The reason you have to do this is because if T
is undefined in your check, the compiler doesn't know if you mean IBar<Int32>
or IBar<SomethingElse>
.
To tackle the type system completely, I think you need to handle recursion, e.g. IList<T>
: ICollection<T>
: IEnumerable<T>
, without which you wouldn't know that IList<int>
ultimately implements IEnumerable<>
.
/// <summary>Determines whether a type, like IList<int>, implements an open generic interface, like
/// IEnumerable<>. Note that this only checks against *interfaces*.</summary>
/// <param name="candidateType">The type to check.</param>
/// <param name="openGenericInterfaceType">The open generic type which it may impelement</param>
/// <returns>Whether the candidate type implements the open interface.</returns>
public static bool ImplementsOpenGenericInterface(this Type candidateType, Type openGenericInterfaceType)
{
Contract.Requires(candidateType != null);
Contract.Requires(openGenericInterfaceType != null);
return
candidateType.Equals(openGenericInterfaceType) ||
(candidateType.IsGenericType && candidateType.GetGenericTypeDefinition().Equals(openGenericInterfaceType)) ||
candidateType.GetInterfaces().Any(i => i.IsGenericType && i.ImplementsOpenGenericInterface(openGenericInterfaceType));
}
First of all public class Foo : IFoo<T> {}
does not compile because you need to specify a class instead of T, but assuming you do something like public class Foo : IFoo<SomeClass> {}
then if you do
Foo f = new Foo();
IBar<SomeClass> b = f as IBar<SomeClass>;
if(b != null) //derives from IBar<>
Blabla();
In case you wanted an extension method that would support generic base types as well as interfaces, I've expanded sduplooy's answer:
public static bool InheritsFrom(this Type t1, Type t2)
{
if (null == t1 || null == t2)
return false;
if (null != t1.BaseType &&
t1.BaseType.IsGenericType &&
t1.BaseType.GetGenericTypeDefinition() == t2)
{
return true;
}
if (InheritsFrom(t1.BaseType, t2))
return true;
return
(t2.IsAssignableFrom(t1) && t1 != t2)
||
t1.GetInterfaces().Any(x =>
x.IsGenericType &&
x.GetGenericTypeDefinition() == t2);
}
Method to check if the type inherits or implements a generic type:
public static bool IsTheGenericType(this Type candidateType, Type genericType)
{
return
candidateType != null && genericType != null &&
(candidateType.IsGenericType && candidateType.GetGenericTypeDefinition() == genericType ||
candidateType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == genericType) ||
candidateType.BaseType != null && candidateType.BaseType.IsTheGenericType(genericType));
}
Try the following extension.
public static bool Implements(this Type @this, Type @interface)
{
if (@this == null || @interface == null) return false;
return @interface.GenericTypeArguments.Length>0
? @interface.IsAssignableFrom(@this)
: @this.GetInterfaces().Any(c => c.Name == @interface.Name);
}
To test it. create
public interface IFoo { }
public interface IFoo<T> : IFoo { }
public interface IFoo<T, M> : IFoo<T> { }
public class Foo : IFoo { }
public class Foo<T> : IFoo { }
public class Foo<T, M> : IFoo<T> { }
public class FooInt : IFoo<int> { }
public class FooStringInt : IFoo<string, int> { }
public class Foo2 : Foo { }
and the test method
public void Test()
{
Console.WriteLine(typeof(Foo).Implements(typeof(IFoo)));
Console.WriteLine(typeof(FooInt).Implements(typeof(IFoo)));
Console.WriteLine(typeof(FooInt).Implements(typeof(IFoo<>)));
Console.WriteLine(typeof(FooInt).Implements(typeof(IFoo<int>)));
Console.WriteLine(typeof(FooInt).Implements(typeof(IFoo<string>)));
Console.WriteLine(typeof(FooInt).Implements(typeof(IFoo<,>)));
Console.WriteLine(typeof(FooStringInt).Implements(typeof(IFoo<,>)));
Console.WriteLine(typeof(FooStringInt).Implements(typeof(IFoo<string,int>)));
Console.WriteLine(typeof(Foo<int,string>).Implements(typeof(IFoo<string>)));
}
There shouldn't be anything wrong the following:
bool implementsGeneric = (anObject.Implements("IBar`1") != null);
For extra credit you could catch AmbiguousMatchException if you wanted to provide a specific generic-type-parameter with your IBar query.
'Programing' 카테고리의 다른 글
Visual Studio 2013에서 기존 솔루션을 GitHub에 추가하는 방법 (0) | 2020.05.05 |
---|---|
튜플 목록을 개별 목록으로 압축 해제하는 방법은 무엇입니까? (0) | 2020.05.05 |
PHP에서 컬 타임 아웃 설정 (0) | 2020.05.05 |
파이썬에서 동적 (매개 변수화 된) 단위 테스트를 어떻게 생성합니까? (0) | 2020.05.05 |
Java에서 int에서 Long으로 어떻게 변환합니까? (0) | 2020.05.05 |