0

I know the question inconsistent accessibility is frequently asked, and my question is different because I need it to be private.

Let me give the detailed explaination: I have a static class Line, and a struct LingSegment in Line, users can define their own LineSegment in the Line, and I need to make sure there is no conflict between line segments.

The architecture is like below:

public static class Line {
    private struct LineSegment {
        public LineSegment(int start, int end) {
            Start = start;
            End = end;
        }

    public readonly int Start;
    public readonly int End;
    }

   // User created LineSegment
   static public LineSegment Segment1() {
       return new LineSegment(1, 2);
   }

   static public LineSegment Segment2() {
       return new LineSegment(3, 6);
   }
}

And when users want to access their line segments, they can only access like:

int start = Line.Segment1.Start;
int end = Line.Segment1.End;

The reason that I make LineSegment to be private is that: I only want to users create and access LineSegment through static functions in Line, e.g. Segment1, Segment2. So that I can add a unit test using reflection to get all the methods in Line, and get all the line segments' start and end, then I can judge whether there is conflict between line segments.

If LineSegment is public, users can hack in their code just using: new Line.LineSegment(2, 5), and I can't detect it through unit test. I don't want them create LineSegment outside Line.

But making LineSegment is dis-allowed in C# because of inconsistent accessibility. Is their any solution that can meets my requirment? Thanks!

5
  • actually not, other one can still directly construct LineSegment if their code is under this solution.
    – Caesium
    Commented Oct 25, 2018 at 8:03
  • But Segment1 and Segment2 can't construct the LineSegment too?
    – Caesium
    Commented Oct 25, 2018 at 8:07
  • 1
    if they have access to the solution, they can simply remove the private modifier ... i'd think: if the programmer does dumb things, then let him do dumb things... on the other hand ... why don't you make the LineSegment constructor take a Line parameter, or have a factory build LineSegments and have your checks done at runtime? Commented Oct 25, 2018 at 8:10
  • Yes, there are. But the compiler says the constructor is inaccessible due to its protection level. I think its because the private constructor can't been callled?
    – Caesium
    Commented Oct 25, 2018 at 8:10
  • Yes, you comment makes sense. If they have access to the solution, they can simply remove the private modifier... But I think don't give them the access of LineSegment will give them a promt to use the function like Segment1... (If they construct LineSegment, I can not even find it!
    – Caesium
    Commented Oct 25, 2018 at 8:15

3 Answers 3

2

what about: give them access but warn them...

    public static class Line {
        public struct LineSegment {
            [Obsolete("DO NOT USE THIS DIRECTLY...")]
            public LineSegment(int start, int end) {
                Start = start;
                End = end;
            }

        public readonly int Start;
        public readonly int End;
        }

       // User created LineSegment
       static public LineSegment Segment1() {
           return _CreateSegment(1, 2);
       }

       static public LineSegment Segment2() {
           return _CreateSegment(3, 6);
       }

       static private LineSegment _CreateSegment(int start,int end) {
//we don't want to trigger the warning ...
#pragma warning disable 618
           return new LineSegment(3, 6);
#pragma warning restore 618
       }
    }
3
  • The function _CreateSegment will also trigger a warning for its return type is LineSegment...
    – Caesium
    Commented Oct 25, 2018 at 12:44
  • does it help to disable 618 for the whole method? Commented Oct 26, 2018 at 7:29
  • Yes, it helps. But not seems beatiful.
    – Caesium
    Commented Oct 26, 2018 at 7:36
2

A well tried pattern in these cases is to define a public interface and keep the inner class private, with a public (or internal) constructor. In these way, you prevent other developers (and even yourself in the future) who work on the same project to call the constructor directly, while still giving them access to the relevant properties of the class. So, first define the interface:

public interface ILineSegment
{
    int Start { get; }
    int End { get; }
}

and then your class:

public static class Line
{
    private struct LineSegment : ILineSegment
    {
        public int Start { get; }
        public int End { get; }

        public LineSegment(int start, int end)
        {
            Start = start;
            End = end;
        }
    }

    // User created LineSegment
    static public ILineSegment Segment1()
    {
        return new LineSegment(1, 2);
    }

    static public ILineSegment Segment2()
    {
        return new LineSegment(3, 6);
    }

Note that the return values of LineSegment1 and LineSegment2 have been changed from LineSegment (which is private, and cannot be returned by a public method) to ILineSegment, which is public, and can be returned.

1
  • Yes it's a good idea, but I think DarkSquirrel42's idea can avoid adding a interface and less change of code.
    – Caesium
    Commented Oct 25, 2018 at 12:37
0

After comparasion and ask for some opinions, finally I choosed to use Environment.StackTrace.Contains to meet my requirement.

public static class Line {
    public struct LineSegment {
        public LineSegment(int start, int end) {
            if (!Environment.StackTrace.Contains("MyNamespace.Line") || !Environment.StackTrace.Contains("UnitTest")) {
                throw new InvalidOperationException("Outside code is not allowed to call its constructor. Please construct your property in this file refering to the example.");
            }

            Start = start;
            End = end;
        }

        public readonly int Start;
        public readonly int End;
    }

   // User created LineSegment
   static public LineSegment Segment1() {
       return new LineSegment(1, 2);
   }

   static public LineSegment Segment2() {
       return new LineSegment(3, 6);
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.