I got a generic Interface like this :
public interface IResourceDataType<T>
{
void SetResourceValue(T resValue);
}
Then I got this class that implements my Interface :
public class MyFont : IResourceDataType<System.Drawing.Font>
{
//Ctor + SetResourceValue + ...
}
And finally I got a :
var MyType = typeof(MyFont);
I, now, want to get the System.Drawing.Font Type from MyType !
At the moment, I got this code :
if (typeof(IResourceDataType).IsAssignableFrom(MyType))
{
//If test is OK
}
But I don’t manage to “extract” my Type here…
I tried a couple of things with GetGenericArguments() and other things but they either don’t compile or return a null value/List…
What do I have to do ?
EDIT :
Here is the solution that fit my code for those who will get the same problem :
if (typeof(IResourceDataType).IsAssignableFrom(MyType))
{
foreach (Type type in MyType.GetInterfaces())
{
if (type.IsGenericType)
Type genericType = type.GetGenericArguments()[0];
}
}
}
Since your
MyFontclass only implements one interface, you can write:If your class implements several interfaces, you can call the GetInterface() method with the mangled name of the interface you’re looking for: