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.
void SomeFunction<T> (object model)
{
(SomeClass<T>)model;
}
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?
Updated # 1
For more understanding, please look at the following example.
public partial class SimpleUserInfo
{
public string LogOnName { get;set; }
public string HashedPassword { get;set; }
}
public partial class UserInfo : SimpleUserInfo
{
pubic string Address { get;set; }
}
After I create some data models. I create some generic class that use UserInfo class as parameter.
public class SimpleUserInfoValidator : IValidator<SimpleUserInfo>
{
// logic to validate simple user info instance
}
And then, I add attribute to SimpleUserInfo class.
[Validator(typeof(SimpleUserInfoValidator))]
public partial class SimpleUserInfo {}
Finally, I create some function for retrieving validator in given class T.
public GetValidator<T> ()
{
var attribute = (ValidatorAttribute)Attribute.GetCustomAttribute(type, typeof(ValidatorAttribute));
if (attribute == null || attribute.ValidatorType == null)
return null;
var (IValidator<T>)Activator.CreateInstance(attribute.ValidatorType);
}
This function will works fine when T is SimpleUserInfo but problem will occur when T is UserInfo. How to solve this?
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.
Thanks,