简体   繁体   English

c#泛型方法

[英]c# generic method

I have a method that receives messages from a queue of msmq. 我有一个从msmq队列接收消息的方法。

I have 6 different queues in msmq and id like a single generic method that would receive the messages. 我在msmq中有6个不同的队列,id就像一个接收消息的通用方法。 THis work, but i need to write 6 methods for each queue. 这是工作,但我需要为每个队列编写6个方法。 I d like to make it more generic. 我想让它更通用。

public List<QueMessage> getMessagesFromObj1Queue()
{
    List<QueMessage> messageList = new List<QueMessage>();

    QueObj1 que = new QueObj1();

    while (que.ReceiveAll().Count > 0)
    {
        varmessage = que.Receive(new TimeSpan(0, 1, 0));
        messageList.Add(message);
    }

    return messageList;
}

I have 5 different object which just extends a single abstract class. 我有5个不同的对象,它只扩展了一个抽象类。 Below doenst work. 下面的工作。

public List<QueMessage> getMessagesFromObj1Queue<T>(T que)
{
    List<QueMessage> messageList = new List<QueMessage>();

    while (que.ReceiveAll().Count > 0)
    {
        varmessage = que.Receive(new TimeSpan(0, 1, 0));
        messageList.Add(message);
    }

    return messageList;
}

Above doesnt work 以上不起作用

how to fix this? 怎么解决这个问题?

If T in your example is some base class that all queue objects inherit, then you can just pass that to the method instead of T : 如果你的例子中的T是所有队列对象继承的基类,那么你可以将它传递给方法而不是T

public List<QueMessage> getMessagesFromObj1Queue<T>(QueueObjBase que) { ... }

Otherwise, if there's come common interface that all T 's will implement, use that as a generic constraint: 否则,如果有所有T将实现的通用接口,请将其用作通用约束:

public List<QueMessage> getMessagesFromObj1Queue<T>(T que) 
   where T : [yourInterface]
{
}

Without a generic constraint on T , the compiler doesn't have any information to know what method or properties are available, and so can only treat T as object - which, of course, doesn't have a RecieveAll() method. 如果没有对T的通用约束,编译器就没有任何信息可以知道哪些方法或属性可用,因此只能将T视为object - 当然,它没有RecieveAll()方法。

The problem here is that you've told the C# compiler nothing about the type of que and hence it must treat it as an object. 这里的问题是你告诉C#编译器没有关于que的类型,因此它必须把它当作一个对象。 You must provide more information so that it can properly bind the Receive method. 您必须提供更多信息,以便它可以正确绑定Receive方法。

This can be done by adding a generic constraint to the method. 这可以通过向方法添加通用约束来完成。 For example 例如

public List<QueMessage> getMessagesFromObj1Queue<T>(T que) 
  where T : [InsertBaseTypeOfQues]

Add a where contraint onto the method and delcare the type param to be your base class. 在方法上添加一个where contraint,并将类型参数作为基类进行delcare。

where T : The type argument must be or derive from the specified base class. 其中T:type参数必须是或从指定的基类派生。

see generic type constrains from msdn 从msdn看到泛型类型约束

As it is, the type T has no ReceiveAll method. 实际上,类型T没有ReceiveAll方法。 You need to add a constraint, so that the compiler knows that the method exists, for any instantiation of T. So, you'd get: 您需要添加一个约束,以便编译器知道该方法存在,对于T的任何实例化。因此,您将获得:

public List getMessagesFromObj1Queue(T que) : where T : XXX public List getMessagesFromObj1Queue(T que):其中T:XXX

I don't know MSMQ enough to know what XXX should be, though. 我不知道MSMQ足以知道XXX应该是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM