C# Method Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, you can get all types that implement an interface using reflection. Here's how to do it:
public static IEnumerable<Type> GetTypesImplementingInterface<T>() { return AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => typeof(T).IsAssignableFrom(p) && p.IsClass); }
In this example, the GetTypesImplementingInterface
method takes a generic type parameter T
representing the interface to be searched for. The method then uses the AppDomain.CurrentDomain.GetAssemblies()
method to get all assemblies currently loaded in the application domain, and the SelectMany
method to get all types from all assemblies. The Where
method is then used to filter the types based on whether they implement the specified interface and are classes. Finally, the matching types are returned as an IEnumerable<Type>
.
public interface IExampleInterface { } public class ExampleClass : IExampleInterface { } public void MethodA(object obj) { if (obj is IExampleInterface) { Console.WriteLine("Object implements interface"); } }
In this example, IExampleInterface
is defined as an interface, and ExampleClass
is defined as a class that implements the interface. The MethodA
method takes an object
parameter obj
, and checks whether the object implements the IExampleInterface
interface using the is
keyword. If the object implements the interface, the method prints a message to the console.
C# get all types implementing interface:
using System; using System.Linq; using System.Reflection; public interface IMyInterface { void MyMethod(); } public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Get types implementing IMyInterface var implementingTypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(assembly => assembly.GetTypes()) .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface: {type.Name}"); } } }
Get types that implement specific interface in C#:
using System; using System.Linq; using System.Reflection; public interface IMyInterface { void MyMethod(); } public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Get types implementing IMyInterface var implementingTypes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface: {type.Name}"); } } }
C# get all implementations of an interface:
using System; using System.Linq; using System.Reflection; public interface IMyInterface { void MyMethod(); } public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Get all implementations of IMyInterface var implementingTypes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface: {type.Name}"); } } }
C# find all types implementing an interface using reflection:
using System; using System.Linq; using System.Reflection; public interface IMyInterface { void MyMethod(); } public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Find all types implementing IMyInterface using reflection var implementingTypes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface: {type.Name}"); } } }
C# reflection to get types implementing an interface:
using System; using System.Linq; using System.Reflection; public interface IMyInterface { void MyMethod(); } public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Use reflection to get types implementing IMyInterface var implementingTypes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface: {type.Name}"); } } }
using System; using System.Linq; using System.Reflection; public interface IMyInterface { void MyMethod(); } public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Get all classes implementing IMyInterface var implementingTypes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface: {type.Name}"); } } }
using System; using System.Linq; using System.Reflection; public interface IMyInterface { void MyMethod(); } public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Discover types implementing IMyInterface using LINQ var implementingTypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(assembly => assembly.GetTypes()) .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface: {type.Name}"); } } }
using System; using System.Linq; using System.Reflection; public interface IMyInterface { void MyMethod(); } public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Get implementing types using reflection var implementingTypes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface: {type.Name}"); } } }
using System; using System.Linq; using System.Reflection; [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class CustomAttribute : Attribute { } public interface IMyInterface { void MyMethod(); } [Custom] public class MyClass1 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass1 implementing IMyInterface"); } } public class MyClass2 : IMyInterface { public void MyMethod() { Console.WriteLine("MyClass2 implementing IMyInterface"); } } public class TypeDiscoveryExample { public static void Main() { // Find classes implementing IMyInterface with a custom attribute var implementingTypes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => typeof(IMyInterface).IsAssignableFrom(type) && !type.IsInterface && type.GetCustomAttributes(typeof(CustomAttribute), false).Any()); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface with custom attribute: {type.Name}"); } } }/
using System; using System.Linq; using System.Reflection; public interface IMyInterface1 { void MyMethod1(); } public interface IMyInterface2 { void MyMethod2(); } public class MyClass : IMyInterface1, IMyInterface2 { public void MyMethod1() { Console.WriteLine("MyClass implementing IMyInterface1"); } public void MyMethod2() { Console.WriteLine("MyClass implementing IMyInterface2"); } } public class TypeDiscoveryExample { public static void Main() { // Get types implementing multiple interfaces var implementingTypes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => typeof(IMyInterface1).IsAssignableFrom(type) && typeof(IMyInterface2).IsAssignableFrom(type) && !type.IsInterface); foreach (var type in implementingTypes) { Console.WriteLine($"Type implementing IMyInterface1 and IMyInterface2: {type.Name}"); } } }