title: How to cast object to type described by Type class?
body: <p>I have a object:</p>
  
  <pre><code>ExampleClass ex = new ExampleClass();
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>Type TargetType
  </code></pre>
  
  <p>I would like to cast ex to type described by TargetType like this:</p>
  
  <pre><code>Object o = (TargetType) ex;
  </code></pre>
  
  <p>But  when I do this I get:</p>
  
  <blockquote>
    <p>The type or namespace name 't' could
    not be found</p>
  </blockquote>
  
  <p>So how to do this? Am I missing something obious here?</p>
  
  <p>Update:</p>
  
  <p>I would like to obtain something like this:</p>
  
  <pre><code>public CustomClass MyClassOuter
  {
     get
     {
          return (CustomClass) otherClass;
     }
  }
  
  private otherClass;
  </code></pre>
  
  <p>And because I will have many properties like this I would like do this:</p>
  
  <pre><code>public CustomClass MyClassOuter
  {
     get
     {
          return (GetThisPropertyType()) otherClass;
     }
  }
  
  private SomeOtherTypeClass otherClass;
  </code></pre>
  
  <p>Context:</p>
  
  <p>Normally in my context in my class I need to create many properties. And in every one replace casting to the type of property. It does not seem to have sense to me (in my context) because I know what return type is and I would like to write some kind of code that will do the casting for me. Maybe it's case of generics, I don't know yet. </p>
  
  <p>It's like I can assure in this property that I get the right object and in right type and am 100% able to cast it to the property type. </p>
  
  <p>All I need to do this so that I do not need to specify in every one property that it has to "cast value to CustomClass", I would like to do something like "cast value to the same class as this property is".</p>
  
  <p>For example:</p>
  
  <pre><code>class MYBaseClass
  {
     protected List&lt;Object&gt; MyInternalObjects;
  }
  
  class MyClass
  {
     public SpecialClass MyVeryOwnSpecialObject
     {
        get
        {
             return (SpecialClass) MyInteralObjects["MyVeryOwnSpecialObject"];
        }
     }
  }
  </code></pre>
  
  <p>And ok - I can make many properties like this one above - but there is 2 problems: </p>
  
  <p>1) I need to specify name of object on MyInternalObjects but it's the same like property name. This I solved with System.Reflection.MethodBase.GetCurrentMethod().Name.</p>
  
  <p>2) In every property I need to cast object from MyInternalObjects to different types. In MyVeryOwnSpecialObject for example - to SpecialClass. It's always the same class as the property.</p>
  
  <p>That's why I would like to do something like this:</p>
  
  <pre><code>class MYBaseClass
  {
     protected List&lt;Object&gt; MyInternalObjects;
  }
  
  class MyClass
  {
     public SpecialClass MyVeryOwnSpecialObject
     {
        get
        {
             return (GetPropertyType()) MyInteralObjects[System.Reflection.MethodBase.GetCurrentMethod().Name];
        }
     }
  }
  </code></pre>
  
  <p>And now concerns: Ok, what for? Because further in my application I will have all benefits of safe types and so on (intellisense).</p>
  
  <p>Second one: but now you will lost type safety in this place? No. Because I'm very sure that I have object of my type on a list.</p>