0

我想使用 HashTable 映射对对象的引用而不是对象值

configMapping.Add("HEADERS_PATH", Me.headers_path)

这样,当我要检索“HEADERS_PATH”的值时,我就可以为 Me.headers_path 分配一个值

类似于 C 中的“&”运算符

4

4 回答 4

3

我假设Me.headers_path是 System.String。因为System.String是不可变的,所以您想要的无法实现。但是您可以添加额外的间接级别来实现类似的行为。

计算机科学中的所有问题都可以通过另一个层次的间接来解决。 巴特勒兰普森

C# 中的示例(请善意编辑到 VB 并稍后删除此评论):

public class Holder<T> {
    public T Value { get; set; }
}

...

Holder<String> headerPath = new Holder<String>() { Value = "this is a test" };
configMapping.Add("HEADERS_PATH", headerPath);

...

((Holder<String>)configMapping["HEADERS_PATH"]).Value = "this is a new test";

// headerPath.Value == "this is a new test"
于 2008-10-02T23:45:14.183 回答
1

使 headers_path 成为一个礼节(带有集合)

于 2008-10-02T23:44:05.013 回答
1

这似乎是一个字典,在 .Net 2.0 中,如果要更新的引用始终是字符串,则可以将其定义为 Dictionary,如果要获取任意引用,则可以定义为 Dictionary(不推荐)。

如果您需要替换字典中的值,您可以定义自己的类并提供一些辅助方法来简化此操作。

于 2008-10-02T23:49:24.020 回答
1

我不完全确定你想做什么。假设 smink 是正确的,那么这里是他的代码的 VB 翻译。抱歉,我无法编辑它,我认为我还没有足够的代表。

public class Holder(Of T)
    public Value as T 
end class
...
Dim headerPath as new Holder(Of String)
headerPath.Value = "this is a test"
configMapping.Add("HEADERS_PATH", headerPath)
...
Directcast(configMapping["HEADERS_PATH"]),Holder(Of String)).Value = "this is a new test"

'headerPath.Value now equals "this is a new test"

@marcj - 你需要在你的答案中转义尖括号,所以使用 < 对于 < 和 > 对于 >。再次抱歉,我不能只为您编辑您的帖子。

于 2008-11-13T19:42:18.457 回答