Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric Types with Abstract classes and Interfaces
    primarykey
    data
    text
    <p>I have three classes, one base class which takes a generic type that must be a subclass of an abstract class which in turn takes a generic type that must implement an interface.</p> <p>For testing I have a form which will initiate the base class giving a subclass of the abstract class and an implementation of the interface. Here are my class declarations, and call to the base class constructor.</p> <pre><code> public class BaseClass&lt;AC_IC&gt; where AC_IC : AbstractClass&lt;InterfaceClass&gt; public abstract class AbstractClass&lt;IC&gt; where IC: InterfaceClass public interface InterfaceClass public class ExtendsAbstractClass&lt;II&gt; : AbstractClass&lt;InterfaceClass&gt; public class ImplementsInterface : InterfaceClass vBC = new BaseClass&lt;ExtendsAbstractClass&lt;ImplementsInterface&gt;&gt;(); </code></pre> <p>I believed I would be able to create instances of both of the inheriting/implementing classes from within the BaseClass and AbstractClass using the following calls (only one class will have parameters in the constructor)</p> <pre><code> (AC_IC) Activator.CreateInstance(typeof(AC_IC), new object[] { vParameter}); (II) Activator.CreateInstance(typeof(II)); </code></pre> <p>EDIT: The above calls are within the BaseClass and AbstractClass and therefore do not know which classes I will use subsequently, so cannot be specified by class name, there may be many different pairs of classes for a range of implementations.</p> <p>For both of these constructor calls I get the errors 'Cannot create an instance of an abstract class' &amp; 'Cannot create an instance of an interface'. How can I let the compiler know that the classes that I am passing as the generic types are subclassess of the classes in the <code>where</code> clauses and so can be created.</p> <p>Any ideas community???</p>
    singulars
    1. This table or related slice is empty.
    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.
    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