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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:54:57+00:00 2026-06-04T04:54:57+00:00

Okay, here is what i would like to do. Class Container<T> { T contained;

  • 0

Okay, here is what i would like to do.

Class Container<T>
{
    T contained;
    public void ContainObject(T obj)
    {
        contained = obj;
        if(/*Magical Code That Detects If T Implemtns IContainableObject*/)
        {
            IContainableObect c = (IContainableObject)obj;
            c.NotifyContained(self);
        }
    }
}

interface IContainableObject
{
    public void NotifyContained(Container<REPLACE_THIS>);//This line is important, see below after reading code.
}



Class ImplementingType : IContaiableObject
{
    public Container<ImplementingType> MyContainer;
    public void NotifyContained(Container<ImplmentingType> c)
    {
        MyContainer = c;
    }
}




Class Main
{
    public static void Main(args)
    {
        ImplementingType iObj = new ImplementingType();
        Container<ImplementingType> container = new Container();
        container.ContainObject(iObj);
        //iObj.MyContainer should now be pointing to container.
    }
}

Basically, to sum up the above example, I have a generic wrapper type of type T.
I would like that wrapper type to notify whatever it contains that it is being contained (with a copy of its self!) IF the contained object implements a specific interface (this bit I know how to do)

But it gets tricky! Why? Well because the container generic needs to have a type.

Remember that important line?

If REPLACE_THIS is IContainableObject, then all implementers of the interface must use IContainerObject, not the name of the implementing class in their NotifyContained method.

Using ImplementingType as the type of the container within the interface is even worse, for obvious reasons!

So my question is, what do I do to make REPLACE_THIS represent the class of the object implementing the interface?

  • 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-04T04:54:58+00:00Added an answer on June 4, 2026 at 4:54 am
    class Container<T>
    {
        T contained;
        public void ContainObject(T obj)
        {
            contained = obj;
            var containable = obj as IContainableObject<T>;
            if(containable != null)
            {
                containable.NotifyContained(this);
            }
        }
    }
    
    interface IContainableObject<T>
    {
        void NotifyContained(Container<T> c);
    }
    
    class ImplementingType : IContainableObject<ImplementingType>
    {
        public Container<ImplementingType> MyContainer;
        public void NotifyContained(Container<ImplementingType> c)
        {
            MyContainer = c;
        }
    }
    

    EDIT: add version with generic constraint

    interface IContainer<T>
    {
        void ContainObject(T obj);
    }
    
    class Container<T> : IContainer<T> where T : IContainableObject<T>
    {
        T contained;
    
        public void ContainObject(T obj)
        {
            contained = obj;
            contained.NotifyContained(this);
        }
    }
    
    interface IContainableObject<T>
    {
        void NotifyContained(IContainer<T> c);
    }
    
    class ImplementingType : IContainableObject<ImplementingType>
    {
        public IContainer<ImplementingType> MyContainer;
    
        public void NotifyContained(IContainer<ImplementingType> c)
        {
            Debug.WriteLine("notify contained");
            MyContainer = c;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am generating some Dynamic SQL and would like to ensure that my code
Okay here is what I'm trying to do: I have a model object that
Okay, so we have a utility here in-house that generates business-model classes from our
Okay, I'm not doing anything fancy here, but it's driving me crazy that I
Is it okay to initialize a 2D vector like this (here all values in
I have a library with an API much like this: public class Library :
Here is my search function : class Ads extends Controller { public function search($format='velos',
Okay here's my situation. I have the following branches development development-kirby (270 commits ahead
Okay so here's what I'm doing. I'm making a request to a server to
Okay so here is the deal. As the question states, I'm trying to POST

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.