Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 497659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:48:10+00:00 2026-05-13T05:48:10+00:00

The main problem of this question is when we pass T into some function

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:48:10+00:00Added an answer on May 13, 2026 at 5:48 am

    I haven’t installed .NET 4.0 yet, so I am not sure about using covariance properly, but you can get covariance support even in .Net 2.0 and 3.5 using a Duck Typing library (e.g. duck typing library by David Meyer or LinFu by Philip Laureano:

    In other words, last line in GetValidator<T> should look like this:

    // http://www.deftflux.net/blog/page/Duck-Typing-Project.aspx
    return DuckTyping.Cast<IValidator<T>>
         Activator.CreateInstance(attribute.ValidatorType);
    

    or (using LinFu)

    // http://www.codeproject.com/KB/cs/LinFuPart2.aspx
    DynamicObject dynamicObj = new DynamicObject
        (Activator.CreateInstance(attribute.ValidatorType));
    return dynamicObj.CreateDuck<IValidator<T>>()
    

    [Edit]

    It is possible that I have not understood your question, but I believe it boils down to this:

    You have a generic interface with a generic parameter of type T:

    IValidator<SimpleUserInfo> simpleUserValidator;
    

    You want to cast it to the same generic interface, but with a generic parameter which is a base class of T:

    IValidator<SimpleUserInfo> ---> IValidator<UserInfo> 
    

    Simple casting will not work, because generic type covariance is not supported (at least not in older versions of .Net):

    // this will throw an invalid cast exception
    IValidator<UserInfo> userValidator = (IValidator<UserInfo>) simpleUserValidator;
    

    But it will work using duck typing:

    IValidator<UserInfo> userValidator = 
        DuckTyping.Cast<IValidator<UserInfo>> (simpleUserValidator);
    

    Once again, .Net 4.0 should handle covariance, but I haven’t tested it. This example will work in any .Net version, so I included it for completeness sake.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As you have observed, this basically means breaking lines with… May 14, 2026 at 9:14 pm
  • Editorial Team
    Editorial Team added an answer Internet Explorer supports this very well. You can use it… May 14, 2026 at 9:14 pm
  • Editorial Team
    Editorial Team added an answer Use the modulus operator for this type of work. The… May 14, 2026 at 9:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.