Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you pass an instance of a generic type to other strongly-typed methods?
    primarykey
    data
    text
    <p>I have a situation where I have a method that takes a couple of overloads. I have a fairly common scenario where if some condition is true, I call one of the overloads, otherwise I call something else. I decided to try to be clever and refactor the common code into a single generic method that would take an object and a condition, and call the overloaded method or otherwise call the common code.</p> <p>A much simplified example of the code: </p> <pre><code>/// &lt;summary&gt; /// Dummy interface /// &lt;/summary&gt; public interface ITest1 { } /// &lt;summary&gt; /// Dummy interface /// &lt;/summary&gt; public interface ITest2 { } /// &lt;summary&gt; /// Generic Class /// &lt;/summary&gt; public class GenericClass { /// &lt;summary&gt; /// First overload /// &lt;/summary&gt; /// &lt;param name="test1"&gt;&lt;/param&gt; public void TestMethod(ITest1 test1) { } /// &lt;summary&gt; /// Second overload /// &lt;/summary&gt; /// &lt;param name="test2"&gt;&lt;/param&gt; public void TestMethod(ITest2 test2) { } /// &lt;summary&gt; /// method with common logic /// &lt;/summary&gt; /// &lt;typeparam name="TInterfaceType"&gt; /// Type of the test object /// &lt;/typeparam&gt; /// &lt;param name="test"&gt; /// Test object to pass to the method. /// &lt;/param&gt; public void ConditionallyCallTest&lt;TInterfaceType&gt;( TInterfaceType test, bool someLogic) { if (someLogic) { this.TestMethod(test); } else { // .. Perform Common operations here } } } </code></pre> <p>Ignoring the fact that this doesn't do anything, if you compile this code segment, you will get a compiler error that it can't convert TInterfaceType to ITest1.</p> <p>I was hoping for the compiler to wait until I specified the type to do the type checking, so:</p> <pre><code>GenericClass g = new GenericClass(); // We have an overload, so this is OK: g.ConditionallyCallTest&lt;ITest1&gt;(test1); // We have an overload, so this is OK: g.ConditionallyCallTest&lt;ITest2&gt;(test2); // Compiler error, no overload available: g.ConditionallyCallTest&lt;UnknownType&gt;(obj); </code></pre> <p>Is it possible to do something like this using C#?</p> <p>I also tried to use the where clause to specify the allowable types, but I couldn't figure out how to get the where clause to specify an either/or relationship between the specified types, only an AND relationship.</p> <p>EDIT</p> <p>As I <a href="https://stackoverflow.com/questions/644841/can-you-pass-an-instance-of-a-generic-type-to-other-strongly-typed-methods/644883#644883">mentioned in the comments below</a>, I was attempting to avoid having to create matching overload methods, so are there any other suggestions to solve this problem, or am I limited by the language here?</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.
 

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