英文:
How to set property as one type and get as another?
问题
我有这段代码:
public class TaskDetails
{
private Dictionary<string, string> _arguments;
public string Arguments
{
get
{
string _tmp = "";
foreach (var _item in _arguments)
{
string _str = _item.Key + ": " + _item.Value + "<br>";
_tmp += string.Join(_str, Environment.NewLine);
}
return _tmp;
}
set { _arguments = value; }
}
}
这个想法非常简单:我在代码中使用我喜欢的方式(即Dictionary)将值放入属性中,但我希望从属性中以字符串的形式获取它以进行表示。
但是IDE关于set访问器说我不能隐式地将类型string转换为Dictionary。愚蠢的IDE,我想要将Dictionary放入Dictionary类型的私有属性中。
那么,我应该如何才能公开将属性设置为Dictionary,并以字符串形式公开获取此属性呢?
英文:
I have this code:
public class TaskDetails
{
private Dictionary<string, string> _arguments;
public string Arguments
{
get
{
string _tmp = "";
foreach (var _item in _arguments)
{
string _str = _item.Key + ": " + _item.Value + "<br>";
_tmp += String.Join(_str, Environment.NewLine);
}
return _tmp;
}
set { _arguments = value; }
}
}
The idea is very simple: I put value into property in the form I'm comfortable with up the code (i.e. Dictionary), but I get it from property in the form for representation down the code - the string.
But IDE says about set accessor that I cannot implicitly convert type string to Dictionary. Silly IDE, I want to put in Dictionary into Dictionary-type private property.
How, then, should I do it to publicly set property as Dictionary and publicly get this property as string?
答案1
得分: 2
将字典表示为以<br>
分隔的纯文本字符串集合的概念有点不寻常,将来可能需要进行一些调整(例如,如果您想将文本放入div
元素以使其成为有效的HTML5,或者如果您想提供转义以防止跨站脚本攻击,或者您可能想使用CRLF而不是Environment.Newline
以匹配HTTP协议)。因此,我建议您将这个想法封装在一个独立的类中。这将提高代码的可维护性。
然后,您可以为自定义类添加一些隐式转换运算符,并编写代码,自动进行字符串或字典之间的转换,从而使您可以按照自己的思路编写代码(尽管在底层执行的操作可能略有不同)。
public class HtmlDictionary //或其他名称
{
private readonly Dictionary<string, string> _arguments;
public HtmlDictionary(Dictionary<string, string> arguments)
{
_arguments = arguments;
}
public override string ToString()
{
string tmp = "";
foreach (var item in arguments)
{
string str = $"{item.Key}: {item.Value}<br>\r\n";
tmp += str;
}
return tmp;
}
public static implicit operator string(HtmlDictionary input) => input.ToString();
public static implicit operator HtmlDictionary(Dictionary<string, string> input) => new HtmlDictionary(input);
}
现在,您可以这样做:
public class TaskDetails
{
public HtmlDictionary Arguments { get; set }
}
var o = new TaskDetails();
o.Arguments = new Dictionary<string, string>();
string s = o.Arguments;
看起来您正在设置不同类型的变量,但在底层,编译器只是在您的代码中进行隐式转换。
P.S. 下划线通常只出现在成员变量前面。如果您在局部变量名前加下划线,将会让很多人感到困惑。
英文:
The notion of representing a dictionary as a <br>
delimited set of plaintext strings is a little unusual and will probably need tweaks at some point in the future (e.g. if you wanted to put the text into div
elements to make them valid HTML5, or if you wanted to provide escaping to prevent XSS, or you might want to use CRLF instead of Environment.Newline
to match the HTTP protocol). So I'd suggest you encapsulate that idea in a class of its own. This will improve your maintainability.
Then you can give you custom class some implicit conversion operators, and you can write code that automatically converts to/from string or dictionary, allowing you to code it the way you were thinking (even though it's doing something slightly different under the covers).
public class HtmlDictionary //Or some other name
{
private readonly Dictionary<string, string> _arguments;
public HtmlDictionary(Dictionary<string, string> arguments)
{
_arguments = arguments;
}
public override string ToString()
{
string tmp = "";
foreach (var item in arguments)
{
string str = $"{item.Key}: {item.Value}<br>\r\n";
tmp += str;
}
return tmp;
}
public static implicit operator string(HtmlDictionary input) => input.ToString();
public static implicit operator HtmlDictionary(Dictionary<string, string> input) => new HtmlDictionary(input);
}
Now you can do this:
public class TaskDetails
{
public HtmlDictionary Arguments { get; set }
}
var o = new TaskDetails();
o.Arguments = new Dictionary<string,string>();
string s = o.Arguments;
It looks like you are setting a different type than you are getting, but under the covers the compiler is just implicitly converting for you.
P.S. The underscore generally precedes member variables only. It will confuse a lot of people if you put an underscore in front of local variable names too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论