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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:20:39+00:00 2026-06-17T09:20:39+00:00

Suppose I have following class: public class Xyz { } public class Abc: Xyz

  • 0

Suppose I have following class:

public class Xyz {
}

public class Abc: Xyz {
}

public class Pqr: Xyz {
}

public class Wtf: Abc {
}

with a method to find the common base type of type1 and type2:

public static Type FindBaseClassWith(this Type type1, Type type2);

Then, I call the method with:

typeof(Wtf).FindBaseClassWith(variousType).FindBaseClassWith(typeof(Xyz));

Where variousType is a variable of Type, it possibly sometimes be null.

And FindBaseClassWith is meant to be chained call like:

t1.FindBaseClassWith(t2).FindBaseClassWith(t3).FindBaseClassWith(t4);

to find the only base type between them.

What should the method FindBaseClassWith return?

A most acceptable answer will be mark whether it will be the solution.

  • 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-06-17T09:20:40+00:00Added an answer on June 17, 2026 at 9:20 am

    Since it’s an extension method, it will work fine even if one of the members of the chain is null. You can call extension methods on a null reference because it’s syntactic sugar for a method call where the object is passed as a parameter. However, you will get a NullReferenceException if you try to access a property like .Name at the end when the result is null.

    If you want to use a chain call to collect a series of parameters, and then only “process” them at the end, you want a similar pattern to LINQ. The intermediate types should be some sort of a wrapper that simply collects the values in the chain. Then there should be a final call that actually initiates the process of matching the types, i.e., something like .Resolve(). Here’s an example implementation called TypeChain:

    public class TypeChain
    {
        private List<Type> types;
    
        public TypeChain(Type t)
        {
            types = new List<Type>();
            types.Add(t);
        }
    
        // searching for common base class (either concrete or abstract)
        public TypeChain FindBaseClassWith(Type typeRight)
        {
            this.types.Add(typeRight);
            return this;
        }
    
        public Type Resolve()
        {
            // do something to analyze all of the types
            if (types.All (t => t == null))
                return null;
            else
                types = types.Where (t => t != null).ToList();
    
            var temp = types.First();
            foreach (var type in types.Skip(1))
            {
                temp = temp.FindBaseClassWithHelper(type);
            }
            return temp;
        }
    }
    

    There would be a few changes to the FindBaseClassWith static implementations:

    // searching for common base class (either concrete or abstract)
    public static Type FindBaseClassWithHelper(this Type typeLeft, Type typeRight)
    {
        if(typeLeft == null || typeRight == null) return null;
    
        return typeLeft
                .GetClassHierarchy()
                .Intersect(typeRight.GetClassHierarchy())
                .FirstOrDefault(type => !type.IsInterface);
    }
    
    // searching for common base class (either concrete or abstract)
    public static TypeChain FindBaseClassWith(this Type typeLeft, Type typeRight)
    {
        return new TypeChain(typeLeft).FindBaseClassWith(typeRight);
    }
    

    Using the chaining logic above allows for a logic scenario that isn’t possible with the original code. It’s now possible to check if all entries are null, and then return null, or if any are not null, then purge all the null entries first before processing so they don’t pollute the result. This can be extended of course to any desired logic.

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

Sidebar

Related Questions

Suppose I have the following (trivially simple) base class: public class Simple { public
Suppose I have the following class: public class GenericClass<T> { public T Find() {
Suppose I have the following class: class Camera { public Camera( double exposure, double
Suppose I have the following: public class MyObject { public string Name {get; set;}
Suppose you have the following class: class Test : ISerializable { public static Test
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following 2 entities: @Entity public class Person implements Serializable {
Suppose I have the following service object public class UserService { @Autowired private UserDao
Say suppose I have the following Java code. public class Example { public static
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();

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.