My answer is that you should simply declare as internal all the constructors that you want to hide from external classes.
If you do this, only classes within the same assembly will be able to access them (for example, "class library project").
Other than that, you should follow the recommendations in some other answers that recommend a different architecture.
However, you can make the constructor private and call it through reflection.
Instead of directly accessing the constructor, you can have a private static method in SubGroup that returns the factory method and calls this through reflection. This makes calling the constructor somewhat more difficult (and avoids boxing operations during construction).
I do not recommend doing this; You should find a cleaner way, for example, using the internal constructor or another architecture , but so that you can see how you could do this:
using System; using System.Reflection; namespace ConsoleApplication1 { class Program { static void Main() { Group group = new Group(); SubGroup subGroup = group.MakeSubgroup(42); Console.WriteLine(subGroup.Value); } } public sealed class Group { public SubGroup MakeSubgroup(int value) { return _subgroupFactory(value); } static Func<int, SubGroup> getSubgroupFactory() { var method = (typeof(SubGroup)).GetMethod("getSubgroupFactory", BindingFlags.NonPublic | BindingFlags.Static); return (Func<int, SubGroup>) method.Invoke(null, null); } static readonly Func<int, SubGroup> _subgroupFactory = getSubgroupFactory(); } public sealed class SubGroup { private SubGroup(int value) { this.value = value; } public int Value { get { return value; } } static Func<int, SubGroup> getSubgroupFactory() { return param => new SubGroup(param); } readonly int value; } }
source share