Col*_*nee 3 c# reference pointer-to-pointer
要清楚,我想要一个指向指针的行为,这个问题的目的是生成干净,可读的代码.
我有一些代码包含检查多个Dictionary.TryGetValue
调用结果的条件.如果它可以通过一次调用检索所有必需的对象,那将会更清晰,所以我想编写一个扩展,允许我执行以下操作:
Dictionary<string, string> myDictionary; // Initialized somewhere
string x, y, z;
bool foundAllEntries = myDictionary.TryGetValues({"xvalue", out x}, {"yvalue", out y},
{"zvalue", out z});
if (foundAllEntries)
; // Do something with x, y, and z
Run Code Online (Sandbox Code Playgroud)
但是,我无法找到一种方法将扩展方法引用传递给将保存输出的对象.这似乎应该是非常基本的东西.
如何在对象中存储对本地引用的引用?
请注意,这个问题并不是要求实现TryGetValues函数的替代方法.我可以通过很多方式实现"工作",但没有一种方法可以像我想要的那样生成干净的代码.
这似乎应该是非常基本的东西.
它不仅不是基本的,它是完全不可能的:没有办法用ref
或装饰数据类型out
- 这些修饰符仅适用于形式方法参数.换句话说,没有"参考变量"或"输出变量"这样的东西; 语言中只有"参考参数"和"输出参数".
此外,您不能将输出或引用参数作为可变长度参数列表(即params
部分)的一部分传递,因此该方法也不起作用.
我可以通过很多方式实现"工作",但没有一种方法可以像我想要的那样生成干净的代码.
奇怪的是,上述并不意味着您无法实现您尝试实现的方案,如果应用代理设计模式,则代码几乎与原始代码一样干净.诀窍是链式方法调用,并为结果提供隐式转换运算符,如下所示:
class MyMap {
internal IDictionary<string,string> dict = ...
public ItemGetterResult TryGetValues {
get {
return new ItemGetterResult(this, true);
}
}
}
class ItemGetterResult {
private readonly MyMap map;
private bool IsSuccessful {get;set;}
internal ItemGetterResult(MyMap theMap, bool successFlag) {
map = theMap;
IsSuccessful = successFlag;
}
public static implicit operator bool(ItemGetterResult r) {
return r.IsSuccessful;
}
public ItemGetterResult Get(string key, out string val) {
return new ItemGetterResult(
map
, this.IsSuccessful && map.dict.TryGetValue(key, out val)
);
}
}
Run Code Online (Sandbox Code Playgroud)
现在调用看起来像这样:
bool foundAllEntries = myDictionary.TryGetValues
.Get("xvalue", out x)
.Get("yvalue", out y)
.Get("zvalue", out z);
Run Code Online (Sandbox Code Playgroud)