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 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

Given a model structure like this: class Book(models.Model): user = models.ForeignKey(User) class Readingdate(models.Model): book
Given a class: class Control { public Control Parent { get; set; } public
I've got a tiny model (let's call it Node) that represents a tree-like structure.
I can't figure out what is up with this. I have a Scene class
I'm looking at reducing the memory consumption of a table like collection object. Given
I am implementing a tree think of it as a folder structure so I
Related to my earlier question (thanks for your response Cryo), I have a further
I have a ListView binded to a GenericList and for each Item it is
I'm new to iPhone development, and multiple views (xib or nib) are really confusing
I'm attempting my first google app engine project – a simple player stats database

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.