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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:53:13+00:00 2026-06-07T06:53:13+00:00

I have an internal class which contains implementation details, including properties, which should not

  • 0

I have an internal class which contains implementation details, including properties, which should not be publicly accessible. I have to pass this class to public callers, and allow them to pass it around, store it, serialize it, or do basically whatever they want with it. The class can be accessed publicly in two different ways. These ways are exposed as two public interfaces. I would like to prevent the user from casting between the two public interfaces. Here is some code to demonstrate what I am talking about.

void Main()
{
    var container = new ContainsDetails(5);

    Console.WriteLine(container.LessDetail.TheDetails);
    var more = (ContainsDetails.IFooPlus)(container.LessDetail);
    more.TheDetails = 10;
    Console.WriteLine(container.LessDetail.TheDetails);
}

public class ContainsDetails {
    public interface IFooPlus {
        int TheDetails {
            get;
            set;
        }
    }

    public interface IFoo {
        int TheDetails {
            get;
        }
    }

    private readonly Detail _myDetail;

    public ContainsDetails(int x) {
        _myDetail = new Detail(x);
    }

    public IFoo LessDetail {
        get {
            return _myDetail;
        }
    }

    public IFooPlus MoreDetail {
        get {
            return _myDetail;
        }
    }

    public bool ProcessFoo(IFoo foo) {
        var theDeets = foo as Detail;
        if (theDeets != null) {
            return theDeets.TheDetails++%2 == 0;
        } else {
            throw new ArgumentException("foo argument must have originated from ContainsDetails.");
        }
    }

    private class Detail : IFooPlus, IFoo {
        private int _myX;

        private Detail() {}

        internal Detail(int x) {
            _myX = x;
        }

        public int TheDetails {
            get {return  _myX; }
            set { _myX = value; }
        }
    }
}

Output is:

5
10

I would like for the output above to be:

5
System.InvalidCastException thrown.....

The class ContainsDetails is just present for demonstration. In my actual code the assembly fills the role of ContainsDetails. Similarly, Detail is an internal class in my actual code.

Here the user knows about both IFooPlus and IFoo. The user does not know that Detail exists. The user also does not know that the object returned by LessDetail is convertible to an IFooPlus. My code returns the same Detail object to the user at different times, and will return it as IFoo or IFooPlus depending on some complicated state of Detail and other internal objects. Also note the ProcessFoo method in ContainsDetails. My code needs to be able to accept an IFoo from the user, and process it as a Detail.

I would like for the cast var more = (ContainsDetails.IFooPlus)(container.LessDetail) to generate an InvalidCastException. It is prohibitively expensive to do runtime checking on all IFooPlus operations to determine if the Detail object is in the correct state for those operations. Instead I would like the user never to be able to treat an IFoo as an IFooPlus.

The only option I can think of is to embed some IFoo member subclass inside of Detail and have the Detail and the subclass hold references to each other. Yuck.

Is there a best practice for preventing this sort of casting, or should I just enclose the IFoo functionality in a member subclass of Detail?

  • 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-07T06:53:15+00:00Added an answer on June 7, 2026 at 6:53 am

    The simplest approach is to have a read-only wrapper which has a reference to the writable one. So LessDetail would always return the same wrapper reference, but MoreDetail would return a reference to the “real” type. You can then make both of them implement an internal interface which allows you to do whatever you need to, with the wrapper probably proxying those calls to the underlying object.

    Alternatively, you could try to revisit your design – it doesn’t sound ideal at the moment, either in terms of not being able to cast between the types or that ProcessFoo accepts any IFoo at compile-time, but is really only able to handle one particular implementation. This feels like an abuse of inheritance to me.

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

Sidebar

Related Questions

All my persistent objects have a property which should not be persisted. At this
I have simplified my code to this internal class Program { private static void
I have a class which contains a static member, a map of strings to
I have the following class public class classB : classA which contains function dostuff
I have an asp web service that contains a class which has a function
in one of my project I have a Garage class. This class contains many
I have a class Type which cannot be copied nor it contains default constructor.
I have a IEnumerable. I have a custom Interval class which just has two
I have an internal class A that does a lot of stuff and I
I have the following class, internal class PageInformation { public string Name { get;

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.