有没有一种方法可以获取我可以在通用方法中使用的类型的列表?

时间:2020-07-03 15:17:33

标签: c#

嗨,我想要的是类似的东西,在C#中可能吗? 〜

(是否不使用反射?)

当前:

    protected override void AddTelegrams()
    {
         TelegramPublisher.Subscribe<Telegram1>(TelegramReceived);
         TelegramPublisher.Subscribe<Telegram2>(TelegramReceived);
         TelegramPublisher.Subscribe<Telegram3>(TelegramReceived);
    }

    protected override void RemoveTelegrams()
    {
         TelegramPublisher.UnSubscribe<Telegram1>(TelegramReceived);
         TelegramPublisher.UnSubscribe<Telegram2>(TelegramReceived);
         TelegramPublisher.UnSubscribe<Telegram3>(TelegramReceived);
    }

目标:

    private IEnumerable<Type> GetListTelegramTypes()
    {
        return new List<Type>()
        {
            typeof(Telegram1),
            typeof(Telegram2),
            typeof(Telegram3)
        };
    }

    protected override void AddTelegrams()
    {
        foreach (var type in GetListTelegramTypes())
            TelegramPublisher.Subscribe<type>(TelegramReceived);
    }

    protected override void RemoveTelegrams()
    {
        foreach (var type in GetListTelegramTypes())
            TelegramPublisher.UnSubscribe<type>(TelegramReceived);
    }

也可以订阅和取消订阅通用类型,可以是“继承”或“继承”。

1 个答案:

答案 0 :(得分:4)

如果您不想使用反射,则可以执行以下操作:

        string phoneNumber = txtPhoneNumber.Text;
        string cleanPhoneNumber = string.Empty;

        foreach (char c in phoneNumber)
        {
            if (c.Equals('0') || c.Equals('1') || c.Equals('2') || 
                c.Equals('3') || c.Equals('4') || c.Equals('5') ||
                c.Equals('6') || c.Equals('7') || c.Equals('8') || 
                c.Equals('9'))
                cleanPhoneNumber += Convert.ToString(c);
        }

然后:

public interface ITelegramSubscriber
{
    void Subscribe(TelegramReceivedHandler handler);
    void Unsubscribe(TelegramReceivedHandler handler);
}

public class TelegramSubscriber<T> : ITelegramSubscriber
{
    public void Subscribe(TelegramReceivedHandler handler) =>
        TelegramPublisher.Subscribe<T>(handler);
    public void Unsubscribe(TelegramReceivedHandler handler) =>
        TelegramPublisher.Unsubscribe<T>(handler);
}

这假定您具有一种private IEnumerable<ITelegramSubscriber> GetListTelegramSubscribers() { return new List<ITelegramSubscriber>() { new TelegramSubscriber<Telegram1>(), new TelegramSubscriber<Telegram2>(), new TelegramSubscriber<Telegram3>(), new TelegramSubscriber<Telegram4>(), }; } protected override void AddTelegrams() { foreach (var subscriber in GetListTelegramSubscribers()) subscriber.Subscribe(TelegramReceived); } protected override void RemoveTelegrams() { foreach (var subscriber in GetListTelegramSubscribers()) subscriber.Unsubscribe(TelegramReceived); } 方法类型,该方法类型可以处理所有电报。如果实际上您有多个重载(问题尚不清楚),那么您可能想要这样的东西:

TelegramReceivedHandler

然后:

public interface ITelegramSubscriber
{
    void Subscribe();
    void Unsubscribe();
}

public class TelegramSubscriber<T> : ITelegramSubscriber
{
    private readonly TelegramReceivedHandler<T> handler;
    public TelegramSubscriber(TelegramReceivedHandler<T> handler) => this.handler = handler;
 
    public void Subscribe() => TelegramPublisher.Subscribe<T>(handler);
    public void Unsubscribe() => TelegramPublisher.Unsubscribe<T>(handler);
}