我如何组织这些结构?
我有许多不同的结构,如下所示(显然更复杂一些),并且希望从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
SStruct
被声明为嵌套在MyMainClass
中,因此暗示(至少每当我看到类似的内容时)是SStruct
旨在支持MyMainClass
,并且不被外部类使用。如果是这种情况,解决问题的最简单方法是将struct
声明为private
,然后将private
成员设为公共
或内部
。现在,其他类无法访问成员(因为它们根本无法访问该结构),而 MyMainClass 可以。如果您实际上在其他地方使用 SStruct,我建议您在任何其他类之外声明它,以便清楚地表明它应该以这种方式使用。
最后,一般来说,您应该避免可变值类型。创建设置所需状态的构造函数,然后让 struct 以此方式度过其生命。如果您需要“更改”它,那么执行此操作的方法应返回具有所需状态的新创建的实例。
Since
SStruct
is declared nested withinMyMainClass
, the implication (at least whenever I see something like that) is thatSStruct
is intended to supportMyMainClass
, and not be used by outside classes. If that's the case, the easiest work-around to your problem is to declared thestruct
asprivate
, and then make theprivate
memberspublic
orinternal
. Now, other classes can't access the members (since they can't access the struct at all,) whileMyMainClass
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.您可以将私有字段标记为内部字段,以便字段在它们所在的程序集外部不可见。
You could mark private fields as internal, so that fields won't be visible outside the assembly they reside.
如果嵌套类型需要是
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 aninternal
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.