简体   繁体   English

在结构中设置属性的属性?

[英]Setting a property of a property in a struct?

I created a struct, that has another struct as a property. 我创建了一个结构,该结构具有另一个结构作为属性。 I want to be able to do something like this: 我希望能够做这样的事情:

MyColor.RGBColor.R = 255;

MyColor and RGBColor being the structs I created. MyColor和RGBColor是我创建的结构。 This doesn't work, I set a whole new value to RGBColor, but that's not straightforward and there has to be an easier way. 这是行不通的,我为RGBColor设置了一个新的值,但这并不简单,必须有一种更简单的方法。 Currently, I have to do this: 目前,我必须这样做:

MyColor.RGBColor = new RGBColor(255, MyColor.RGColor.G ...)

I'm pretty sure if I stopped encapsualting the private properties in public properties, and just make them public in the first place, I won't have this problem... but I always read that this is a bad thing to do, so how can I go about this? 我敢肯定,如果我停止将私有财产封装在公共财产中,而只是首先将其公开,我不会遇到这个问题...但是我总是读到这是一件坏事,所以我该怎么办? Thanks 谢谢

EDIT: 编辑:

This is how I implement the properties currently: 这是我当前实现属性的方式:

private rgbcolor _RGBColor;
public rgbcolor RGBColor {
  get {
    return _RGBColor;
  }
  set {
    _RGBColor = value;
  }
}

This is expected behavior. 这是预期的行为。 By accessing the property RGBColor , you invoke its getter method, which returns the struct's instance by value . 通过访问属性RGBColor ,可以调用其getter方法,该方法按value返回结构的实例。 Even though you would then assign (which, in fact, even doesn't compile) a value into R , the struct itself is not stored back into the RGBColor property — even if it had a setter. 即使您随后将一个值(实际上甚至不会编译)赋值给R ,该结构本身也不会存储RGBColor属性中-即使它具有setter。 This is just how value types behave. 这就是值类型的行为。

If you can, just avoid using structs. 如果可以,请避免使用结构。 In the world of classes with automatic references and properties with getter/setter methods they tend to be very counter-intuitive. 在具有自动引用的类和具有getter / setter方法的属性的世界中,它们往往非常违反直觉。 Trying to represent small classes (in terms of data size) as structs is premature optimization . 尝试将小类(就数据大小而言)表示为结构是过早的优化

Note: What you call a “private property” is not a property. 注意:您所谓的“私有财产” 不是财产。 It's a member field. 这是一个成员字段。


You should look at conventions for class names vs. variable names. 您应该查看类名与变量名的约定。 You seem to have it somewhat backwards in your implementation. 您的实现似乎有些倒退。


Here's what I think you're trying to do: 这是我认为您要尝试执行的操作:

private rgbcolor _RGBColor = new rgbcolor();
public rgbcolor RGBColor {
  get {
    return _RGBColor;
  }
}

Once that's in place, you should be able to do something like (assuming there's a property R on rgbcolor) 一旦到位,您应该可以执行类似的操作(假设rgbcolor上有属性R)

MyColor.RGBColor.R = 255;

This will work because the instance for MyColor.RGBColor will exist when the RGBColor property is accessed. 这将起作用,因为访问RGBColor属性时将存在MyColor.RGBColor的实例。

When you use value types (structs) you should keep in mind that passing them in methods as parameters or returning from methods (also in some other cases) always cause making a copy of that object. 当使用值类型(结构)时,请记住,将它们作为参数传递给方法或从方法返回(在某些情况下也是如此)总是会导致该对象的复制。 Properties are just the methods after compilation. 属性只是编译后的方法。 So your code in fact equal: 因此,您的代码实际上是相等的:

var copy = MyColor.RGBColor; // you get a copy now
copy.R = 255; // and now you change a copy, not the original object's value

C# compiller understand that this is an error and don't compile your desired variant. C#编译器知道这是一个错误,请不要编译所需的变体。 Because of this it's strongly recomended to create imutable structs. 因此,强烈建议您创建不可更改的结构。 In your case it's better to use classes. 在您的情况下,最好使用类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM