Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want System.Reflection.ParameterInfo.ParameterType.IsGenericParameter. Here's a VS2008 unit test that passes that illustrates this:</p> <p>Class:</p> <pre><code>public class Foo&lt;T&gt; { public Foo(T val) { this.Value = val.ToString(); } public Foo(string val) { this.Value = "--" + val + "--"; } public string Value { get; set; } } </code></pre> <p>Test method:</p> <pre><code>Foo&lt;string&gt; f = new Foo&lt;string&gt;("hello"); Assert.AreEqual("--hello--", f.Value); Foo&lt;int&gt; g = new Foo&lt;int&gt;(10); Assert.AreEqual("10", g.Value); Type t = typeof(Foo&lt;string&gt;); t = t.GetGenericTypeDefinition(); Assert.AreEqual(2, t.GetConstructors().Length); System.Reflection.ConstructorInfo c = t.GetConstructors()[0]; System.Reflection.ParameterInfo[] parms = c.GetParameters(); Assert.AreEqual(1, parms.Length); Assert.IsTrue(parms[0].ParameterType.IsGenericParameter); c = t.GetConstructors()[1]; parms = c.GetParameters(); Assert.AreEqual(1, parms.Length); Assert.IsFalse(parms[0].ParameterType.IsGenericParameter); </code></pre> <p>The notable point here is the parms[0].ParameterType.IsGenericParameter check which checks if the parameter is a generic or not.</p> <p>Once you've found your constructor then you've got the ConstructorInfo to pass to Emit.</p> <pre><code>public System.Reflection.ConstructorInfo FindStringConstructor(Type t) { Type t2 = t.GetGenericTypeDefinition(); System.Reflection.ConstructorInfo[] cs = t2.GetConstructors(); for (int i = 0; i &lt; cs.Length; i++) { if (cs[i].GetParameters()[0].ParameterType == typeof(string)) { return t.GetConstructors()[i]; } } return null; } </code></pre> <p>Not exactly sure what your intention is though.</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. 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