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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:24:54+00:00 2026-05-14T07:24:54+00:00

I am trying to provide an interface to an abstract generic base class. I

  • 0

I am trying to provide an interface to an abstract generic base class. I want to have a method exposed on the interface that consumes the generic type, but whose implementation is ultimately handled by the classes that inherit from my abstract generic base.

However I don’t want the subclasses to have to downcast to work with the generic type (as they already know what the type should be).

Here is a simple version of the only way I can see to get it to work at the moment.

public interface IFoo
{
    void Process(Bar_base bar);
}

public abstract class FooBase<T> : IFoo 
    where T : Bar_base
{
    abstract void Process(T bar);

    // Explicit IFoo Implementation
    void IFoo.Process(Bar_base bar)
    {
        if (bar == null) throw new ArgumentNullException();

        // Downcast here in base class (less for subclasses to worry about)
        T downcasted_bar = bar as T;
        if (downcasted_bar == null)
        {
            throw new InvalidOperationException(
                          string.Format("Expected type '{0}', not type '{1}'",
                                        T.ToString(), bar.GetType().ToString());
        }

        //Process downcasted object.
        Process(downcasted_bar);            
    }

}

Then subclasses of FooBase would look like this…

public class Foo_impl1 : FooBase<Bar_impl1>
{
     void override Process(Bar_impl1 bar)
     {
         //No need to downcast here!
     } 
}

Obviously this won’t provide me compile time Type Checking, but I think it will get the job done…

Questions:
1. Will this function as I think it will?
2. Is this the best way to do this?
3. What are the issues with doing it this way?
4. Can you suggest a different approach?

Thanks!


Edit: In Response to many answers, It is a requirement that IFoo is not Generic. I need to be able to manipulate a collection of IFoo objects regardless of the generic Types they use.


Edit: In an effort to clarify the reasoning for this…

Bar_base contains a reference to type IFoo. And must call the process method to validate the data it contains. Think of IFoo as an object that contains validation logic for the Bar_base derived objects. When the validity of Bar_base object is questioned, it calls Process on its IFoo reference to validate itself.

The reason IFoo can’t be generic is that I need to be able to reference a collection of IFoo independent of the Bar_base classes.

I am going to try the approach of having two interfaces a Generic one that contains the Process method, and non-generic one that doesn’t.

  • 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-14T07:24:54+00:00Added an answer on May 14, 2026 at 7:24 am

    In the case where IFoo cannot be generic, it’s very typical in a case like this to have two interfaces, IFoo<T> and IFoo, where IFoo uses the most base class supported. Think IEnumerable<T> and IEnumerable. The non-generic version is usually hidden as an interface overload, so it only comes into play when accessing the class via the non-generic interface.

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

Sidebar

Related Questions

I have two Java class hierarchies that share a common ancestor and implement a
I am trying to register a generic type in a config file for Unity
I am trying to use Google's Sketchup C++ SDK (latest version) to export a
I'm trying to embed a browser control in my application (IWebBrowser2). I need to
In C#, I'm trying to serialize ClassA into XML: [Serializable] public ClassA { [XmlElement]
I'm trying to interact with a webcam from Java on Windows (without going through
Is it possible to set the return type of a selector in objective-c to
I am trying to develop an application which will detect the color of the
I'm trying to do file uploads with ruby 1.8 and it seems like there
tl:dr How would a hosted django app correctly transform resource paths to match any

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.