我如何组织这些结构?

发布于 2024-11-24 12:24:27 字数 437 浏览 3 评论 0原文

我有许多不同的结构,如下所示(显然更复杂一些),并且希望从 ABC 内的方法内访问私有变量和方法,同时使这些相同的变量和方法从类 MyMainClass 的外部不可见;我知道我可以通过反射来解决这个问题,但我不想走这条路。我确信这里有人遇到过类似的问题 - 你是如何解决这个问题的?

public class MyMainClass {

    public struct SStruct {
        private ulong myInternalVar;
        public ulong InternalVar{ get{ return myInternalVar; } }
    }

    public void ABC() {
        SStruct val1=new SStruct();
        val1.gElementID=101;
    }

}

I have a number of different structs like below (obviously a little more involved and complicated), and am wanting to access private variables and methods from within methods within ABC while making those same variables and methods invisible from outside of the class MyMainClass; I know I could work around this with Reflection, but I'd rather not go down that route. I'm sure somebody here has had a similar problem - how did you get around it?

public class MyMainClass {

    public struct SStruct {
        private ulong myInternalVar;
        public ulong InternalVar{ get{ return myInternalVar; } }
    }

    public void ABC() {
        SStruct val1=new SStruct();
        val1.gElementID=101;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

居里长安 2024-12-01 12:24:28

由于 SStruct 被声明为嵌套在 MyMainClass 中,因此暗示(至少每当我看到类似的内容时)是 SStruct 旨在支持 MyMainClass,并且不被外部类使用。如果是这种情况,解决问题的最简单方法是将 struct 声明为 private,然后将 private 成员设为 公共内部。现在,其他类无法访问成员(因为它们根本无法访问该结构),而 MyMainClass 可以。

如果您实际上在其他地方使用 SStruct,我建议您在任何其他类之外声明它,以便清楚地表明它应该以这种方式使用。

最后,一般来说,您应该避免可变值类型。创建设置所需状态的构造函数,然后让 struct 以此方式度过其生命。如果您需要“更改”它,那么执行此操作的方法应返回具有所需状态的新创建的实例。

Since SStruct is declared nested within MyMainClass, the implication (at least whenever I see something like that) is that SStruct is intended to support MyMainClass, and not be used by outside classes. If that's the case, the easiest work-around to your problem is to declared the struct as private, and then make the private members public or internal. Now, other classes can't access the members (since they can't access the struct at all,) while MyMainClass can.

If you're actually using SStruct elsewhere, I would recommend declaring it outside of any other classes, so that it's clear it's meant to be used that way.

Finally, you should just avoid mutable value types in general. Create constructors that set the state you want, and then let the struct live out its life that way. If you need to "alter" it, then the methods that do so should return a newly created instance with the required state.

请恋爱 2024-12-01 12:24:28

您可以将私有字段标记为内部字段,以便字段在它们所在的程序集外部不可见。

You could mark private fields as internal, so that fields won't be visible outside the assembly they reside.

懒猫 2024-12-01 12:24:28

如果嵌套类型需要是public,则无法实现您想要的效果。最接近您所假装的解决方案是创建一个内部设置器,这样它就无法在程序集外部使用。不管怎样,我不确定你想要实现什么以及为什么。

在可用信息很少的情况下,我的建议是考虑将结构实现为不可变类型(可变结构是邪恶的),然后重载构造函数以设置内部状态。这不会解决您面临的问题,这只是对您的总体设计的一条建议。

You can not achieve what you want if the nested types need to be public. The closest solution to what you are pretending is creating an internal setter, this way it would not be available outside the assembly. Anyhow, I am not sure what you are trying to achieve and why.

My advice, with the little information availabe, would be to consider implementing your structs as immutable types (mutable structs are evil) and then overload the constructor to set the internal state. This will not resolve the problem you are facing, it's just a piece of advice on your general design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文