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

  • SEARCH
  • Home
  • 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 1090981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:27:15+00:00 2026-05-16T23:27:15+00:00

Given a structure like this: class Parent { } class Child : Parent {

  • 0

Given a structure like this:

class Parent { }

class Child : Parent { }

I have a method that takes a generic type with a constraint that the object is of type Child

static void doSomething<T>() where T : Child
{
    if (typeof(T) == typeof(Parent))
    {
        /* ... */
    }
    else if (typeof(T) == typeof(Child))
    {
        /* ... */
    }
}

the only problem is that, if I have:

class implementsParent : Parent { }

class implementsChild : Child { }

calling the generic method with a type of implementsParent won’t work:

doSomething<implementsParent>(); // compile error
doSomething<implementsChild>(); // works fine

I’m trying to work around the fact that overloading a generic method doesn’t take into account constraints.

What can I do here?

@Sander Rijken: Additional information.

I need to use the generic type to call an ORM framework method like so:

static void doSomething<T>() where T : Child
{
    if (typeof(T) == typeof(Parent))
    {
        Parent obj = orm.GetObject<T>(criteria);
    }
    else if (typeof(T) == typeof(Child))
    {
        Child obj = orm.GetObject<T>(criteria);
    }
}

having the constraint be where T : Parent causes the Child obj = orm.GetObject() to break because T cannot be converted to Type ‘Child’

@Richard Hein:

Originally, I had 2 methods, each with a constraint to one of child/parent (in this case: XPObject and XPCustomObject from the DevExpress ORM – XPObject inherits from XPCustomObject).

The methods look like so:

static void removeBlank<T>(UnitOfWork uow) where T : XPObject
{
    T blank = uow.GetObjectByKey<T>(0):
    if (blank != null)
    {
        blank.Delete();
        uow.CommitChanges();
    }
}

XPCustomObject is used (in this case) to have PKs of type short (instead of the default int on XPObjects). So the only change is in the call to get the object:

static void removeBlankCustomObject<T>(UnitOfWork uow) where T : XPCustomObject
{
    T blank = uow.GetObjectByKey<T>((short)0);    
    if (blank != null)
    {
        blank.Delete();
        uow.CommitChanges();
    }
}

The difference is minimal, so I want to merge the two methods together.

  • 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-16T23:27:15+00:00Added an answer on May 16, 2026 at 11:27 pm
    doSomething<implementsParent>();
    

    This fails because it doesn’t meet the type constraint. T isn’t derived from Child

    Did you mean to declare it:

    static void doSomething<T>() where T : Parent
    

    Edit: This will work, given the requirement you added.

    static void doSomething<T>() where T : Parent
    {
        if (typeof(T) == typeof(Parent))
        {
            T obj = orm.GetObject<T>(criteria);
        }
        else if (typeof(T) == typeof(Child))
        {
            T obj = orm.GetObject<T>(criteria);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that looks like this: public class Node { public
Given this structure: <root> <user> <userName>user1</userName> <userImageLocation>/user1.png</userImageLocation> </user> <user> <userName>user2</userName> </user> </root> public class
Assume a legacy class and method structure like below public class Foo { public
EDIT: It looks like their is no simpler way to do this. Given the
I have the following problem: Given a Guice type literal TypeLiteral<T> template and a
got a question regarding serializing classes that I've defined. I have some classes like
Guys I have the following structure in my project. application/ Bootstrap.php configs/ application.ini modules/
I have a design where I have a std::list of base pointers that I'd
The class project has us reading in the title, artist and lyrics of 10,514
I'm sure there's a better way to do this. I'm trying to create a

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.