related to Getting all types that implement an interface we can easily get all Types in the Assembly that implements a specific interface.
Example:
interface IFace
{
}
class Face : IFace
{
}
class TwoFace : Face
{
}
For this structure, we will find both classes via reflection, i.e. all classes that are derived from the first implementation too, using
GetTypes().Where(
type => type.GetInterfaces().Contains(typeof(IFace))
)
So the question is: how can I restrict the result to the base class that initially implements the interface?! In this example: only class type Face is relevant.
The C library function int getc(FILE *stream) gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream. Declaration. Following is the declaration for getc() function. Parameters.
Programs that use ‘gets’ can actually be a security problem on your computer. Since it is sometimes used in older code (which is why the GNU C Library still provides it), we need to understand how it is used. The gets function takes one parameter, the string in which to store the data which is read.
The getdelim function is a more general form. getline stops reading input at the first newline character it encounters, however the getdelim function enables you to specify other delimiter characters than newline. In fact, getline simply calls getdelim and specifies that the delimiter character is a newline.
It is dangerous because it provides no protection against buffer overflow; overflowing the string into which it is saving data. Programs that use ‘gets’ can actually be a security problem on your computer. Since it is sometimes used in older code (which is why the GNU C Library still provides it), we need to understand how it is used.
Firstly, I'd use Type.IsAssignableFrom
rather than GetInterfaces
, but then all you need to do is exclude types whose parent type is already in the set:
var allClasses = types.Where(type => typeof(IFace).IsAssignableFrom(type))
.ToList(); // Or use a HashSet, for better Contains perf.
var firstImplementations = allClasses
.Except(allClasses.Where(t => allClasses.Contains(t.BaseType)));
Or as noted in comments, equivalently:
var firstImplementations = allClasses.Where(t => !allClasses.Contains(t.BaseType));
Note that this will not return a class which derives from a class which implements an interface, but reimplements it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With