Persk
Persk

Reputation: 669

How can I access properties of private nested class from different assembly?

I have a Class called SomeFile which has nested classes called Header and Body.

I want to access SomeFile.Header.CreateDate and SomeFile.Body.Hash but the code can't be compiled showing Inconsistent Accessibility error since the nested classes are private and properties of classes in SomeFile are public.

I don't want Header and Body to be instanced outside of SomeFile but I want them to be accessed through SomeFile.Header and SomeFile.Body

Are there any pattern or any solution to achieve this?

Thank in advance!

Upvotes: 0

Views: 79

Answers (1)

user585968
user585968

Reputation:

Make your constructors internal like this:

public class SomeFile
{
    public Header Header { get; set; }

    public Body Body { get; set; }

}

public class Header
{
    internal Header()
    {

    }

    public DateTime CreateDate { get; set; }
}

public class Body
{
    internal Body()
    {

    }

    public string Hash { get; private set; }
}

This will allow callers to access types such as Header and Body but they won't be able to instantiate it as requested.

Upvotes: 1

Related Questions