Basically, what would be the equivalent of this, but 100% guaranteed to work?
object x = new List<MyTypeWhichImplementsIInterface>(); bool shouldBeTrue = x is IEnumerable<IInterface>;
From some rough testing this works, but I'm not sure.
This will work in C # 4, but not in previous versions.
The operator isuses covariance for type type parameters added in C # 4. Prior to C # 4, the statement will be false.
is
This works because IEnumerable<T>in fact IEnumerable<out T>. Without a dispersion specifier, <T>this would not work.
IEnumerable<T>
IEnumerable<out T>
<T>
, , yout , "T" , .
in/out , T , , , CSJ, .
in/out
x.GetGenericTypeDefinition() == typeof(IEnumerable<>) && typeof(IInterface).IsAssignableFrom(x.GetType().GetGenericArguments()[0]);
Source: https://habr.com/ru/post/1526063/More articles:Ошибка "Parser return unfilled prom" в Dojo - javascriptThe sequence of sending a call in a method causes many simultaneous sendings when the method is called again - iosWhat is a reasonably safe way to store credentials at rest for use in a screen cleaning application? - securityLINQ in Entity Framework 6 with great .Any () - c #Могут ли макросы C быть расширены в gdb, когда программа была скомпилирована с использованием clang? - cGet function variable name has been assigned - javascriptPython: convert utf-8 string to byte string - pythonChange xscreensaver to gnome-screensaver - lockscreenDownload the Java Elastic Beanstalk Java application from the command line - javaorg.gnome.SessionManager on Ubuntu 12.04 chroot - ubuntu-12.04All Articles