Different level of accessors for class and enum

87 Views Asked by At

I need to keep my enumeration internal and I need to keep my class style public. How to handle in this situation?

public class Style : VisionItem, BaseItem
{
    public string TargetType { get; set; }
    public string FontFamily { get; set; }
    public int FontSize { get; set; }
    public StyleType Type { get; set; }
}

internal enum StyleType
{
    TextCss,
    TextJs
}

I received such error: Inconsistent accessibility: property type 'StyleType' is less accessible than property 'Style.Type'

2

There are 2 best solutions below

0
David Browne - Microsoft On BEST ANSWER

The type Style can be public, but external code can't see the Type property. eg

public class Style : VisionItem, BaseItem
{
    public string TargetType { get; set; }
    public string FontFamily { get; set; }
    public int FontSize { get; set; }
    internal StyleType Type { get; set; }
}

internal enum StyleType
{
    TextCss,
    TextJs
}
0
Adam Silenko On

You can declare enum like this:

internal enum StyleType : int {
    TextCss,
    TextJs
}

and Style with Type as int propety that set StyleType local variable

private StyleType type;
public int Type {
    get => (int) type;
    /*internal*/ set {
        if(Enum.IsDefined(typeof(StyleType), value))
            type = (StyleType) value;
        else
            throw new ArgumentOutOfRangeException();
    }
}