Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, there's no individual signature that can do this - there's no way of saying "the nullable type of <code>R</code>, which is either <code>R</code> itself for a reference type, or <code>Nullable&lt;R&gt;</code> for a non-nullable value type".</p> <p>You can have different methods, each with a different constraint on <code>R</code> - but then you either have to provide different names, or use <a href="http://codeblog.jonskeet.uk/2010/11/02/evil-code-overload-resolution-workaround/" rel="nofollow"><em>horrible hacks</em></a> to <em>effectively</em> overload by type parameter constraints.</p> <p>I suspect you should basically have two different method names here, for simplicity. So signatures of:</p> <pre><code>public static Nullable&lt;R&gt; GetValue&lt;T, R&gt;(this T a, Expression&lt;Func&lt;T, R&gt;&gt; expression) where T : Attribute where R : struct public static R GetReference&lt;T, R&gt;(this T a, Expression&lt;Func&lt;T, R&gt;&gt; expression) where T : Attribute where R : class </code></pre> <p>Two asides:</p> <ul> <li>Conventionally type parameters start with <code>T</code>, e.g. <code>TInput</code> and <code>TResult</code></li> <li>Why are you using expression trees at all for <code>GetValue</code>? Why not just take a <code>Func&lt;T, R&gt;</code> and execute it? </li> </ul> <p>As an example of the second bullet point:</p> <pre><code>public static Nullable&lt;R&gt; GetValue&lt;T, R&gt;(this T a, Func&lt;T, R&gt; func) where T : Attribute where R : struct { if (a == null) return null; return func(a); } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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