我们如何找到启用接口的接口? [英] How do we find interfaces that enable interfaces?

查看:141
本文介绍了我们如何找到启用接口的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.







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;







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:

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.

推荐答案

样本用法:



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;
                }
            }
        }
    }
}

直接使用Linq:

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);


试试这个:



Try this:

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);
        }
    }
}


这篇关于我们如何找到启用接口的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆