Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to find classes that use the interface, and I need to access the properties within those classes. How can I follow a path? Thank you for your help.


C#
using System.Drawing;

namespace HesapMakinesi.Interfaces
{
    public interface IHesaplama
    {
        byte Genişlik { get; set; }
        byte Yukseklik { get; set; }
        string Butonİsmi { get; set; }
        Color ButonRengi { get; set; }
        Color ButonİsimRengi { get; set; }
        int LocacationX { get; set; }
        int LocationY { get; set; }

        float Sayi1 { get; set; }
        float Sayi2 { get; set; }
        float IslemYap();
    }
}

  public class Toplama : IHesaplama
    {
        public float Sayi1 { get; set; }
        public float Sayi2 { get; set; }

        public byte Genişlik { get; set; }
        public byte Yukseklik { get; set; }
        public string Butonİsmi { get; set; }
        public Color ButonRengi { get; set; }
        public Color ButonİsimRengi { get; set; }
        public int LocacationX { get; set; }
        public int LocationY { get; set; }

        public Toplama()
        {
            Genişlik = 30;
            Yukseklik = 30;
            Butonİsmi = "+";
            ButonRengi = Color.Gray;
            ButonİsimRengi = Color.Black;
            LocacationX = 100;
            LocationY = 100;
        }
        
        public float IslemYap()
        {
            return Sayi1 + Sayi2;
        }
    }


There is a separate class in other operations, such as the aggregation class.

All of them have a common interface.

Well;


C#
private void Form1_Load(object sender, EventArgs e)
       {

       }


What should I write between these blocks that all classes using the interface should be added to the automatic calculation machine.


What I have tried:

C#
var result = new List<Type>();
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (typeof(IHesaplama).IsAssignableFrom(type))
                    {
                        result.Add(type);
                    }
                }
            }


I tried this code but couldn't reach the classes using the interface.
Posted
Updated 29-Oct-18 14:29pm

Sample usage:

var ifaces = (typeof(IYourInterface)).GetInterfaceImplementors();
// returns an IEnumerable of Interface implementors

public static class TypeExtensions
{
    public static IEnumerable<Type> GetInterfaceImplementors(this Type itype)
    {
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            foreach (Type type in assembly.GetTypes())
            {
                if (itype.IsAssignableFrom(type) && ! type.IsInterface)
                {
                    yield return type;
                }
            }
        }
    }
}
Using Linq directly:
Type interfaceType = typeof(yourInterfaceName);

var implementors = AppDomain.CurrentDomain.GetAssemblies()
    // .Select(asm => asm) removed as suggested by Richard Deeming
    .SelectMany(typs => typs.GetTypes())
    .Where(typ => interfaceType.IsAssignableFrom(typ) && ! typ.IsInterface);
 
Share this answer
 
v3
Comments
Afzaal Ahmad Zeeshan 30-Oct-18 13:38pm    
5ed.
BillWoodruff 30-Oct-18 14:23pm    
thank you, Afzaal !
Richard Deeming 2-Nov-18 11:32am    
On your LINQ example, you don't need the .Select(asm => asm) line. :)
BillWoodruff 2-Nov-18 20:33pm    
Thanks, Richard !
Try this:

C#
var result = new List<Type>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
    foreach (var type in assembly.GetTypes())
    {
        if (type is IHesaplama)
        {
            result.Add(type);
        }
    }
}
 
Share this answer
 
Comments
BillWoodruff 29-Oct-18 20:30pm    
Have you tried to compile this, and use it ?

See: https://stackoverflow.com/a/26768/133321
#realJSOP 30-Oct-18 5:33am    
No, it was off the top of my head, and I was on a Linux system that didn't have (monodevelop on it) when I answered.
BillWoodruff 30-Oct-18 10:15am    
I'd say it's quite rare for anything off the top of your head not to be spot-on :) Your code, as is, will never find a match.
#realJSOP 30-Oct-18 11:35am    
once I got onto a windows box, I wrote a real simple console app (defined an interface object, inherited from it, and then di "is ...", and in that case, it worked. However, I didn't get the assembly types.
BillWoodruff 30-Oct-18 14:21pm    
Hi John, possibly you are seeing 'is work with an instance of a Type ... but, here, imho, the issue is to find Types, not instances (?)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900