外管局指针的指针(以及引用参考)在C# [英] SAFE Pointer to a pointer (well reference to a reference) in C#

查看:150
本文介绍了外管局指针的指针(以及引用参考)在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#应用程序我的工作我有一个很长的标识如下: -

In a C# application I am working on I have a very long identifier as follows:-

foo.bar.bwah.blah.whatever.very.very.huge

每当我引用这个对象这是一个绝对的噩梦,不幸的是我确实需要引用了很多: -

Whenever I to reference this object it's an absolute nightmare, and unfortunately I do need to reference it a lot:-

var something = foo.bar.bwah.blah.whatever.very.very.huge.a;
var somethingElse = foo.bar.bwah.blah.whatever.very.very.huge.b;
foo.bar.bwah.blah.whatever.very.very.huge.c = 12;

等。等等。

我想用某种形式的小得多别名更新此code,问题然而,我想改变底层参考,并具有别名更新还的没有明确更新别名

I want to update this code using a far smaller alias of some kind, the problem is however that I want to change the underlying reference, and have the alias update also without explicitly updating the alias.

目前,如果我做到以下几点: -

Currently if I do the following:-

foo.bar.bwah.blah.whatever.very.very.huge.a = "hello";
string shorter = foo.bar.bwah.blah.whatever.very.very.huge.a;
foo.bar.bwah.blah.whatever.very.very.huge.a = "world";
Console.WriteLine(shorter);

这将输出你好。我想实现的是类似以下内容: -

It will output "hello". What I want to achieve is something like the following:-

foo.bar.bwah.blah.whatever.very.very.huge.a = "hello";
string** shorterPointer = &foo.bar.bwah.blah.whatever.very.very.huge.a;
foo.bar.bwah.blah.whatever.very.very.huge.a = "world";
Console.WriteLine(**shorter);

这将输出世界的要求。

Which would output "world" as required.

我相信你可以做到这样的事情使用不安全的code在C#中,但我的不能的做到这一点,我要使用安全的code而已。

I believe you can achieve something like this using unsafe code in C#, however I cannot do that, I have to use safe code only.

没有任何人有任何想法我会如何实现这一目标?

Does anybody have any ideas how I might achieve this?

请注意:这个问题是不是字符串是不可变的,我知道他们是 - 事实上,我认为他们是对这个问题的目的。这也许可以更简单,如果我用一些其他类型的......所以当我将你好到随后的世界到,我的实例化不同的对象的每一次,所以我存储的参考经过重新分配到一个无效。

Please Note: This question is not about strings being immutable, I know they are - in fact I assumed they are for the purposes of the question. It might perhaps be simpler if I used some other type... so when I assign "hello" to a then "world" to a, I am instantiating different objects on each occasion, hence my stored reference to a becomes invalid after re-assignment.

推荐答案

出的一种方式是使用一个或一对lambda表达式

One way out is to use one or a pair of lambdas.

例如:

Func<string> getter = () => blah_de_blah;
Action<string> setter = x => blah_de_blah = x;

现在,您可以使用getter和setter来读取和写入的长标识符。​​

Now, you can use getter and setter to read and write the long identifier.

然而,由于你的点是成员访问中,绕了最简单的方法是采取引用直接父,像这样:

However, since your dots are member accessors, the easiest way of going about it is to take a reference to the immediate parent, like so:

var myHuge = foo.bar.bwah.blah.whatever.very.very.huge;
// now access myHuge.a everywhere

这是在任何情况下很好的做法,作为一项长期的虚线EX pression这样是违反的德米特,该抑制的codeBase的灵活性法 - 它需要太多的地方太多信息

This is good practice in any case, as a long dotted expression like this is a violation of the Law of Demeter, which inhibits the flexibility of the codebase - it requires too much information in too many places.

这篇关于外管局指针的指针(以及引用参考)在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆