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 4039530
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:37:32+00:00 2026-05-20T12:37:32+00:00

This is a pretty general question, but the specific thing I’m doing is simple

  • 0

This is a pretty general question, but the specific thing I’m doing is simple so I’m including the code. How do I check for type compatibility between two objects when I don’t know the type of either at compile time?

That is, I can do if (object is SomeType) when SomeType is a type name known at compile time. GetType() is not sufficient because it will not work with derived types. Basically I want to be able to say, if (object.IsTypeOfOrIsDerivedFrom(someType)) where sig of this magical method is IsTypeOfOrIsDerivedFrom(Type type)

Here is the context.

// Return all controls that are (or are derived from) any of a list of Types
public static IEnumerable<Control> FindControls(this Control control, IEnumerable<Type> types, bool recurse) 
{
    foreach (Control ctl in control.Controls)
    {
        /// How can I compare the base types of item & ctl?
        if (types.Any(item=>  .... ))
        {
            yield return (ctl);
        }
        if (recurse && ctl.Controls.Count > 0)
        {
            IEnumerable<Control> subCtl = ctl.FindControls(types,true);
            if (subCtl != null)
            {
                yield return (subCtl);
            }
        }
    }
    yield break;
}
  • 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-20T12:37:33+00:00Added an answer on May 20, 2026 at 12:37 pm

    You can use, Type.IsAssignableFrom e.g.

    public class Foo { }
    
    public class Bar : Foo { }
    
    ...
    
    bool compatible = typeof(Foo).IsAssignableFrom(typeof(Bar));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, the context of this question is pretty specific, but I think there is
This is a pretty-much theoretical question, but.. How much of an operating system could
Hey. This example is pretty specific but I think it could apply to a
This is pretty simple, I come from a swing/awt background. I'm just wondering what
This is pretty trivial, but I noticed on SO that instead of an offset
So this seems pretty basic but I can't get it to work. I have
I thought this was pretty straight forward but I don't get the same results
I have a pretty general question here. What is the best + reliable way
This pretty much has me defeated. On XP and earlier versions of Windows you
This is pretty weird. I have my Profiler open and it obviously shows that

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.