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!
LineSegment
if their code is under this solution.Segment1
andSegment2
can't construct theLineSegment
too?LineSegment
will give them a promt to use the function likeSegment1
... (If they constructLineSegment
, I can not even find it!