cod*_*der 0 c# properties reference
继我之前的问题:c-sharp-convert-existing-class-to-use-properties-correct
我有一个这样的课:
public class TestCaseInfo
{
public string text { get; set; } = "";
public string requirement_ref { get; set; } = "";
public string given { get; set; } = "";
public string when { get; set; } = "";
public string then { get; set; } = "";
public UInt32 timeLimit { get; set; } = 0;
}
Run Code Online (Sandbox Code Playgroud)
我以前填充这样的结构:
if (!getNodeValue(testcase_node.SelectSingleNode("text"), ref testcaseInfo.text))
errStr += nodeError(testcase_node, "Missing 'text' node");
Run Code Online (Sandbox Code Playgroud)
注意:我试图通过引用传递它.我已经阅读了大量的排队,基本上都说你不能这样做.很公平...
所以我想传递"真实"值(我认为它称为支持值?).就像是:
if (!getNodeValue(testcase_node.SelectSingleNode("text"), ref testcaseInfo._text)) // where '_text' should be the 'backing' value.
errStr += nodeError(testcase_node, "Missing 'text' node");
Run Code Online (Sandbox Code Playgroud)
但我错过了两件事(可能更多!):
该属性的支持字段没有有效的标识符.您无法使用自动属性,而是显式定义属性的方法get和set方法,以及您自己的支持字段,从而为您提供支持字段的有效标识符,尽管从外部公开此支持字段的设计非常糟糕.
您应该做的是重新设计代码,这样您就不需要首先通过引用传递值.您应该只是按值传递字符串,如果此函数的结果是字符串的计算,则返回它.然后调用者可以将该字符串设置回属性,如果这是他们想要的.那将是更惯用的设计.(因为你也有一个布尔值,你需要传递字符串和布尔值.)
| 归档时间: |
|
| 查看次数: |
177 次 |
| 最近记录: |