Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this situation:</p> <pre><code>// We have an interface... interface InterfaceB {} // And this class implements the interface. class ImplementsB : InterfaceB {} // But this class does not. class DoesNotImplementB {} </code></pre> <p>You could define <code>MethodA</code> as:</p> <pre><code>static Type MethodA&lt;TClass, TInterface&gt;() where TClass : TInterface { return typeof(TClass); } </code></pre> <p>Then the following will work:</p> <pre><code>Type t = MethodA&lt;ImplementsB, InterfaceB&gt;(); </code></pre> <p>But this gives a compile-time error:</p> <pre><code>Type t = MethodA&lt;DoesNotImplementB, InterfaceB&gt;(); </code></pre> <blockquote> <p>The type 'DoesNotImplementB' cannot be used as type parameter 'TClass' in the generic type or method 'MethodA&lt;TClass,TInterface&gt;()'. There is no implicit reference conversion from 'DoesNotImplementB' to 'InterfaceB'.</p> </blockquote> <p>So, this way you are sure that the result of <code>MethodA</code> is a <code>Type</code> of a class that implements <code>TInterface</code>. Given that <code>Type</code> object, you can instantate it later like this:</p> <pre><code>public object Instantiate(Type type) { // Call the default constructor. // You can change this to call any constructor you want. var constructor = type.GetConstructor(Type.EmptyTypes); var instance = constructor.Invoke(new object[0]); return instance; } </code></pre> <p>If you know that your <code>Type</code> is compatible with some interface <code>TInterface</code>, then you can avoid a cast with an additional method like this:</p> <pre><code>public TInterface Instantiate&lt;TInterface&gt;(Type type) { return (TInterface)Instantiate(type); } </code></pre> <p>However, if <code>type</code> is a <code>Type</code> that somehow does not implement <code>TInterface</code>, you'll get an <code>InvalidCastException</code> at run-time. There is no way to constrain <code>Type</code> to be a type that implements a particular interface at compile-time. However, at run-time you can check it to avoid the <code>InvalidCastException</code> exception:</p> <pre><code>public TInterface Instantiate&lt;TInterface&gt;(Type type) { if (!typeof(TInterface).IsAssignableFrom(type)) throw new Exception("Wrong type!"); return (TInterface)Instantiate(type); } </code></pre> <p>Note that <code>typeof(TType)</code> is an expression that results in a <code>Type</code> object, so everywhere you see <code>typeof()</code> you could replace it with any <code>Type</code> variable and vice versa.</p> <p>Is this what you wanted to know?</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.
 

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