title: How to cast some object to generic class of parent of T?
body: <p>The main problem of this question is when we pass T into some function and use it to cast some object like the following statement.</p>
  
  <pre><code>void SomeFunction&lt;T&gt; (object model)
  {
      (SomeClass&lt;T&gt;)model;
  }
  </code></pre>
  
  <p>Everything works fine. But I want to cast model object to generic class object that is parent of T or grand parent of T depend on what is not empty. How to do that?</p>
  
  <p><strong>Updated # 1</strong></p>
  
  <p>For more understanding, please look at the following example.</p>
  
  <pre><code>public partial class SimpleUserInfo
  {
      public string LogOnName { get;set; }
      public string HashedPassword { get;set; }
  }
  
  public partial class UserInfo : SimpleUserInfo
  {
      pubic string Address { get;set; }
  }
  </code></pre>
  
  <p>After I create some data models. I create some generic class that use UserInfo class as parameter.</p>
  
  <pre><code>public class SimpleUserInfoValidator : IValidator&lt;SimpleUserInfo&gt;
  {
      // logic to validate simple user info instance
  }
  </code></pre>
  
  <p>And then, I add attribute to SimpleUserInfo class.</p>
  
  <pre><code>[Validator(typeof(SimpleUserInfoValidator))]
  public partial class SimpleUserInfo {}
  </code></pre>
  
  <p>Finally, I create some function for retrieving validator in given class T.</p>
  
  <pre><code>public GetValidator&lt;T&gt; ()
  {
      var attribute = (ValidatorAttribute)Attribute.GetCustomAttribute(type, typeof(ValidatorAttribute));
  
      if (attribute == null || attribute.ValidatorType == null)
          return null;
  
      var  (IValidator&lt;T&gt;)Activator.CreateInstance(attribute.ValidatorType);   
  }
  </code></pre>
  
  <p>This function will works fine when T is SimpleUserInfo but problem will occur when T is UserInfo. How to solve this?</p>
  
  <p>PS. To solve this question does not require to use new feature of C# 4.0. But I just tell you about I will apply this solution in C# 4.0.</p>
  
  <p><strong>Thanks,</strong></p>