给出这样的结构:
public struct SomeStruct
{
public SomeStruct(String stringProperty, Int32 intProperty)
{
this.StringProperty = stringProperty;
this.IntProperty = intProperty;
}
public String StringProperty { get; set; }
public Int32 IntProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当然,会生成一个编译器错误,其中包含"this"对象在分配所有字段之前无法使用.
有没有办法为支持字段或属性本身分配值,或者我是否必须使用我自己的显式支持字段以老式方式实现属性?
我知道在使用自动属性时,编译器会在屏幕后面创建自己的支持字段.但是,在我读过的许多程序中,我看到人们明确地写了
private int _backingField;
public int Property { get { return _backingField; } }
Run Code Online (Sandbox Code Playgroud)
上面和下面有什么区别?
public int Property { get; private set; }
Run Code Online (Sandbox Code Playgroud)
我明白当你在getter或setter中实际存在副作用时使用该属性是显而易见的,但通常情况并非如此.此外,我知道您必须在结构的情况下明确使用支持字段,您不能通过属性访问其成员.
我能找到的唯一区别是调用值的方式在它定义的类中是不同的.那么它是简单的首选项,还是通过其属性调用值或直接访问领域?简单约定?
我想要遵循TypeScript最常用的命名约定.我注意到官方网站显示的代码示例包含用于类型和模块的Pascal-case以及几乎所有其他内容的驼峰式示例.
我目前正在实现一个封装支持值的属性:
class SomeClass {
get status() {
return statusBackingField;
}
set status(newValue: Status) {
statusBackingField = newValue;
//Do other work here
}
statusBackingField: Status;
}
Run Code Online (Sandbox Code Playgroud)
该物业的名称是status
.在C#中,我通常会命名属性Status
和支持值status
.由于惯例是对属性使用camel-case,这不起作用.我不确定我应该使用哪种约定来与其他TypeScript代码保持一致.
其他语言,如C#和Java,似乎有官方或事实上的标准约定.在TypeScript中命名支持字段是否有任何此类权威或事实上的标准约定?
对于亲密的选民:请注意我不是在寻找意见.我正在寻找上述总结问题中所要求的客观信息.
在C#中,如果使用Type.GetFields()
表示派生类的类型,它将返回a)派生类中所有显式声明的字段,b)派生类中自动属性的所有支持字段和c)基础中所有显式声明的字段类.
为什么缺少基类中自动属性的d)支持字段?
例:
public class Base {
public int Foo { get; set; }
}
public class Derived : Base {
public int Bar { get; set; }
}
class Program {
static void Main(string[] args) {
FieldInfo[] fieldInfos = typeof(Derived).GetFields(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.FlattenHierarchy
);
foreach(FieldInfo fieldInfo in fieldInfos) {
Console.WriteLine(fieldInfo.Name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将只显示Bar的支持字段,而不是Foo.
编辑(再次):如果有人有兴趣,您可以在跟踪器上关注此问题.
编辑:我知道支持属性,他们将涵盖大多数用例.我不是在找工作,我特别想知道是否有办法命名支持字段.
您可以轻松地重命名属性的getter和setter
@get:JvmName("getFancy")
@set:JvmName("setFancy")
var fancyProperty = ...
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何更改支持字段的名称,即使使用@field
目标.
@field:JvmName("fancy")
var fancyProperty = ...
Run Code Online (Sandbox Code Playgroud)
上面给出了一个错误:
此注释不适用于具有支持字段的目标'成员属性'并使用站点目标'@field'
最终我想要的是与JavaFX互操作.在定义JavaFX属性时,通常遵循以下标准(使用一些额外的代码使其变得懒惰):
private ObjectProperty<Color> color = new SimpleObjectProperty<>(this, "color", DEFAULT_COLOR);
public ObjectProperty<Color> colorProperty() {
return color;
}
public Color getColor() {
return colorProperty.get();
}
public void setColor(Color color) {
colorProperty().set(color);
}
Run Code Online (Sandbox Code Playgroud)
所以我想要的是这样的事情(虽然@field
显然不起作用):
@field:JvmName("color")
@get:JvmName("colorProperty")
val colorProperty: ObjectProperty<Color> =
SimpleObjectProperty(this, "color", DEFAULT_COLOR)
var color
get() = colorProperty.get()
set(value) = colorProperty.set(value)
Run Code Online (Sandbox Code Playgroud)
这将允许FXML(使用反射)的绑定功能,遵循Java中的标准约定,并且仍然很好并且易于使用Kotlin.
在这种情况下,我不能使用支持属性,因为我需要color
字段为a ObjectProperty<Color>
,但getter和setter color …
我喜欢组织这样的简单属性:
private int foo;
public int Foo
{
get { return foo; }
set
{
// validate value
foo = value;
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在玩StyleCop,它对我在构造者之后放置字段大吼大叫.这种风格是否被普遍接受,只要该字段永远不会在该属性之外引用?注意:我意识到涉及个人偏好,但我想知道是否就此问题达成了普遍共识.
我有一个简单的POCO类,例如
class C {
[MyAtrib]
public int i {get; set;}
[MyAtrib]
public int i2;
}
Run Code Online (Sandbox Code Playgroud)
我打电话的时候:
GetType().GetFields(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Run Code Online (Sandbox Code Playgroud)
在那个类(实例)上我无法得到FieldInfo
那些自动生成getter/setter(即int i
上面)的成员.
实际上,我正在尝试读取那些自定义属性(MyAtrib
),而不能为那些具有的属性执行此操作{get; set;}
.
这是为什么?我希望得到这两个i
和它的(私人)支持领域,因为i
是公开的.
我能到i
的MyAtrib
某种方式通过反射?
在C#中,自动实现的属性非常方便.但是,尽管它们除了封装它们的支持字段之外什么都不做,但它们仍然不能作为ref或out参数传递.例如:
public int[] arr { get; private set; } /* Our auto-implemented property */
/* ... */
public void method(int N) { /* A non-static method, can write to this.arr */
System.Array.Resize<int>(ref this.arr, N); /* Doesn't work! */
}
Run Code Online (Sandbox Code Playgroud)
在这个特定的情况下,我们可以解决这个问题:
public void method(int N) { /* A non-static method, can write to this.arr */
int[] temp = this.arr;
System.Array.Resize<int>(ref temp, N);
this.arr = temp;
}
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方法来使用C#中自动实现属性的支持字段的引用?
我使用BinaryFormatter将以下类序列化到一个文件中:
[Serializable]
public class TestClass
{
public String ItemTwo { get; set; }
public String ItemOne { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用此代码:
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, new TestClass{ItemOne = "ItemOne", ItemTwo = "ItemTwo"});
fs.Close();
Run Code Online (Sandbox Code Playgroud)
使用此代码反序列化时:
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
TestClass addresses = (TestClass)formatter.Deserialize(fs);
fs.Close();
Run Code Online (Sandbox Code Playgroud)
我得到一切正常.但是,现在我需要这个类有一些支持字段,如下所示:
[Serializable]
public class TestClass
{
private string _itemTwo;
private string _itemOne;
public String ItemTwo
{
get { return _itemTwo; } …
Run Code Online (Sandbox Code Playgroud) 可以说,我有一个变量:
var myObject : MyObject? = null
它应该在某个地方清除:
myObject?.clear
myObject = null
Run Code Online (Sandbox Code Playgroud)
并且在使用地点应该绝对不可为空.在Java中,我可以这样做:
private MyObject getMyObject(){
if(myObject == null) {
myObject = new MyObject()
}
return myObject
}
Run Code Online (Sandbox Code Playgroud)
问题:如何在Kotlin实现这一目标?
我找到了使用elvis-operator的建议:
private fun getMyObject() = myObject ?: MyObject()
Run Code Online (Sandbox Code Playgroud)
但是这并没有MyObject
为myObject
变量分配结果(如果要创建新实例).请帮我解决和解释.谢谢你
我们有一个自定义面板类,它通过内部 DoubleAnimation 对象为其子项设置动画。但是,我们希望将动画的 Duration 依赖属性公开为我们面板的公共属性,以便用户在使用我们的面板时可以在他们的 XAML 中更改它。但是我们不想暴露动画对象的任何其他部分,只想暴露持续时间。
不断向我建议的第一件事是使用 PropertyChanged 通知,但这仅适用于 setter,而不适用于 getter。我们也不能简单地创建 .NET 属性,因为 XAML 完全绕过了 .NET 属性。
我的一个同事有一个聪明的想法......在外部属性和内部对象的属性之间使用双向数据绑定,这实际上看起来是一个非常巧妙的解决方案。但是,除了数据绑定之外,还有另一种/更好的方法来做到这一点……通过它包含对象的公共接口来公开内部对象的依赖属性吗?
更新:
看起来双向数据绑定是要走的路。(感谢@Jeff!)为此,我发现这是设置外部 DP 的最佳方式,因此它是内部对象的 DP 的完美匹配——元数据、默认值和所有内容!然后使用杰夫的绑定技巧,你就完成了!
public Duration Duration {
get { return (Duration)GetValue(DurationProperty); }
set { SetValue(DurationProperty, value); }
}
public static readonly DependencyProperty DurationProperty = DoubleAnimation.DurationProperty.AddOwner(
typeof(SlideContentPanel));
Run Code Online (Sandbox Code Playgroud) 我在第 3 方图书馆有一门课,只有一个get;
财产。
public class Person
{
public string Name {get;}
}
Run Code Online (Sandbox Code Playgroud)
我想Name
使用反射或任何其他合适的方法设置属性,但我不知道 Name 从何处获取其值。我的意思是我不知道它是否有这样的支持字段?
private string m_name;
Run Code Online (Sandbox Code Playgroud)
或者是这样的:
public string Name {get; private set;}
Run Code Online (Sandbox Code Playgroud)
我该如何设置?
backing-field ×12
c# ×8
properties ×7
reflection ×3
kotlin ×2
.net ×1
binding ×1
coding-style ×1
constructor ×1
field ×1
getter ×1
java ×1
nullable ×1
reference ×1
struct ×1
typescript ×1
wpf ×1