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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:46:10+00:00 2026-06-16T02:46:10+00:00

I am having a bit of difficulty in understanding how the Container/Component model interacts

  • 0

I am having a bit of difficulty in understanding how the Container/Component model interacts with each other in C#. I get how the Component contains a Site object which has information about Container and Component. But, suppose I had the following code:

using System;
using System.ComponentModel;

public class Entity : Container {
    public string Foo = "Bar";
}

public class Position : Component {
    public int X, Y, Z;    
    public Position(int X, int Y, int Z){
        this.X = X;
        this.Y = Y;
        this.Z = Z;
    }
}

public class Program {

    public static void Main(string[] args) {

        Entity e = new Entity();
        Position p = new Position(10, 20, 30);

        e.Add(p, "Position");            

    }    

}

This works without issue, it defines a Container (Entity) and a Component (Position) that is contained inside it.

However, if I invoke p.Site.Container, it will return Entity, but as IContainer. That is, I would have to explicitly do something like (Console.WriteLine(p.Site.Container as Entity).Foo); if I wanted to access Foo. This seems quite cumbersome.

Am I missing something, or is there a better way to do what I want?

  • 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-16T02:46:11+00:00Added an answer on June 16, 2026 at 2:46 am

    You’re not missing anything. There is no interface contract regarding what container a component can be inside. If you want to restrict what kind of components can be added to the container you can overload the Add method and do a check of the type of component being added:

    public class Entity : Container {
        public string Foo = "Bar";
    
        public virtual void Add(IComponent component) {
            if (!typeof(Position).IsAssignableFrom(component.GetType())) {
                throw new ArgumentException(...);
            }
            base.Add(component);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of difficulty understanding how the python __init__ ( ) function
I am having a bit of difficulty proxying a request on my site. In
I am having a bit of difficulty understanding how t-sql treats null values. As
I'm having a bit of difficulty understanding how to synchronize ArrayList's between threads in
I'm a bit new to web development and am having trouble understanding how site
I'm having a bit of difficulty figuring out how to use this Regular Expression
I am having a bit of difficulty with this current code I have set
I'm having a bit of difficulty passing a reference type between webservices. My set
I'm having a bit of difficulty coming up with the right answer to this,
I'm having a bit of a difficulty using jQuery to bind a function to

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.