Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing a 'Type' to a Generic Constructor
    text
    copied!<p>Afternoon, evening... whatever it is where you are ;)</p> <p>I'm working on a 'ThreadManager' that will execute a set of 'ManagedThread&lt;>'s. A managed thread is a generic wrapper for a 'ManagableThread', which contains most of the essential methods.</p> <p>The list of 'ManagableThread's to start up, is based on what comes out of a configuration file, and is generated in the ThreadManager.Start method. This list, is meant to be populated with all of the 'ManagedThread's that are to be... managed. Here is the code that I am trying to use to complete this task, and as I'm sure any competent C# developer will quickly realize - I'm not gonna swing it like this.</p> <pre><code>public void Start() { foreach (String ThreadName in this.Config.Arguments.Keys) { Type ThreadType = null; if ((ThreadType = Type.GetType(ThreadName)) == null) { continue; } else { ManagedThread&lt;ThreadType&gt; T = new ManagedThread&lt;ThreadType&gt;( this, this.GetConfiguration(ThreadType) ); this.ManagedThreads.Add(T); continue; } } } </code></pre> <p>I've taken a few stabs at this to no avail. If anyone has any suggestions I'd appreciate them. I'm no Generics expert, so this is slightly out of my realm of expertise, so please do refrain from making me cry, but feel free to catch me if I'm a fool.</p> <p>Thanks in advance to anyone who can offer a hand.</p> <p>Edit: I suppose I should clarify my issue, rather than make you all figure it out... This code will not compile as I cannot pass 'ThreadType' to the generic parameter for my constructor.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload