Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li>Getting a type: <a href="http://msdn.microsoft.com/en-us/library/system.type.gettype.aspx" rel="nofollow noreferrer"><code>Type.GetType()</code></a></li> <li>Instantiating a type from a Type object: <a href="http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx" rel="nofollow noreferrer"><code>Activator.CreateInstance</code></a> (You don't actually need this in your code.)</li> <li>Converting from a string: <a href="http://msdn.microsoft.com/en-us/library/system.convert.changetype.aspx" rel="nofollow noreferrer"><code>Convert.ChangeType</code></a></li> </ul> <p>Note that if the type isn't in <code>mscorlib</code> or the currently executing assembly, you'll need to include the assembly name (and version information if it's strongly named).</p> <p>Here's a complete example using your original code. Note that <code>GetValue</code> doesn't need a normal parameter, as you've already given the type parameter (T).</p> <pre><code>using System; using System.Collections.Generic; public class Foo { Dictionary&lt;Type, Object&gt; _dict = new Dictionary&lt;Type, Object&gt;(); public void Create(string myType, string myValue) { Type type = Type.GetType(myType); object value = Convert.ChangeType(myValue, type); _dict[type] = value; } public T GetValue&lt;T&gt;() { return (T)_dict[typeof(T)]; } } class Test { static void Main() { Foo foo = new Foo(); // Populate with values foo.Create("System.Int32", "15"); foo.Create("System.String", "My String"); foo.Create("System.Boolean", "False"); Console.WriteLine(foo.GetValue&lt;int&gt;()); Console.WriteLine(foo.GetValue&lt;string&gt;()); Console.WriteLine(foo.GetValue&lt;bool&gt;()); } } </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