English 中文(简体)
在C# 4.0的汇辑中具体指明了T的,我怎么能够立即使用诸如名单<T”等通用集装箱?
原标题:How can I instantiate a generic container such as List<T> where T is specified in config in C# 4.0?
  • 时间:2011-11-16 18:01:42
  •  标签:
  • .net
  • c#-4.0

我有一份申请,我想在座标上。 因此,我想在简讯中具体指出一些信息类型,如“申请.msg.UpdateMsg”或“app.msg.SnapshotMsg”等,用以制造问题。

Say my message queue class looks like this:

public class MsgQueue<T> : where T: MsgBase, new()
{
    private readonly Action<T> _queueListener;

    public MsgQueue(Action<T> queueListener)
    {
        _queueListener = queueListener;
    }
    ...
}

现在我要说的是,我还有另一类人想从座标的那几类人听起来,把他们放在集装箱内。 与此类似:

public class QueueManager
{
    // We know T is a MsgBase, but not much else :(
    private List<MsgQueue<MsgBase>> _msgQueues = new List<MsgQueue<MsgBase>>();

    public QueueManager()
    {
        List<string> configuredQueueTypes = GetQueueTypesFromConfig();

        PopulateMsgQueues(configuredQueueTypes);
    }

    private void PopulateMsgQueues(List<string> qTypes)
    {
        foreach (string qType in qTypes)
        {
            Action<MsgBase> listener = GetListener(qType);

            // What goes here? How do I create a MsgQueue<qType>?
        }
    }
    ...
}

How do I define PopulateMsgQueues(), if that s even possible?

如果我能够在“评估”中具体指出“最新信息”等内容,这是否可行(而且会有所帮助)?

Does anyone know of any other way to instantiate a bunch of MsgQueue of T where T is specified by a string at runtime?

我用C# 4.0,这样,动态关键词会帮助我吗?

感谢!

问题回答

您可使用<代码>Type.GetType (string),然后使用该代码来显示您的一般类别。

例如,这将立即列出一份插图清单:

Type type = typeof(List<>).MakeGenericType(Type.GetType("String"));
Activator.CreateInstance(type);

我不认为有活力。 你们需要采用老的反思。

这样做是可能的,因为这种类型需要在汇编时间而不是操作时间时知道。

你可以进行反思,但你失去强势,业绩受到打击(可能或可能无关紧要)。

Action<MsgBase> listener = GetListener(qType);
Type type = typeof(MsgQueue<>).MakeGenericType(Type.GetType("String"));
dynamic msgQueue = Activator.CreateInstance(type, listener);
//do whatever with msgQueue.




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签