Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a variable that can store different instances of generic types and calling a given method on the variable, regardless of the type
    primarykey
    data
    text
    <p>I am trying to create a generic simulation runner. Each simulation implements a variety of interfaces. Eventually, it will pick up simulation types via DLLs at run time, so I will have no way of knowing the types beforehand. </p> <p>My Currrent Code:</p> <pre><code>public class SimulationRunner&lt;TSpace, TCell&gt; where TSpace : I2DState&lt;TCell&gt; where TCell : Cell { public TSpace InitialState { get; set; } public IStepAlgorithm&lt;TSpace,TCell&gt; StepAlgorithm { get; set; } public IDisplayStateAlgorithm&lt;TSpace,TCell&gt; DisplayStateAlgorithm { get; set; } public int MaxStepCount { get; set; } ... public void Run() {...} public void Step() {...} public void Stop() {...} } </code></pre> <p>I want my UI class to store a generic instance of the simulation runner (Eg </p> <pre><code>public partial class UI : Window { SimulationRunner&lt;TSpace,TCell&gt; simulation; ... } </code></pre> <p>so that i can assign different types of simulations to it. Eg </p> <pre><code>simulation = new SimulationRunner&lt;2DSpace, SimpleCell&gt;(); // do stuff // start new simulation of different type simulation = new SimulationRunner&lt;3DSpace, ComplexCell&gt;(); </code></pre> <p>I want to have my UI controls wired to the simulation variable so i can do things like </p> <pre><code>private void mnuiSimulate_Click(object sender, RoutedEventArgs e) { if (simulation != null) simulation.RunSimulation(); } </code></pre> <p>and have it work regardless of what types are currently bound to TSpace and TCell.</p> <p>Currently I am getting errors saying "Error 10 The type or namespace name 'U' could not be found (are you missing a using directive or an assembly reference?)", and the same again for T.</p> <p>I have tried creating a controller class that wraps the SimulationRunner, but I still have the same problem in that I have to pass in the types of TSpace and TCell when I create it, so the problem just gets moved to a different class. </p> <p>How can I store any type of simulation in a variable? How can I bind the controls to work on any type of simulation?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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