C#对象初始化程序-对新实例的引用 [英] C# Object Initialiser - Reference to the new instance

查看:165
本文介绍了C#对象初始化程序-对新实例的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能以某种方式获得对使用对象初始化程序创建的实例的引用

Can I somehow get a reference to the instance I am creating using object initialiser

var x = new TestClass
           {
                 Id = 1,
                 SomeProperty = SomeMethod(this)
           }

此"应指向我正在创建的新TestClass实例.但是显然,它引用了此代码所在的类的实例.

"this" should point to the new TestClass instance I'm creating. But it obviously refers the the instance of the class in which this code resides.

我不是问这是否是个好方法. 我知道我可以这样做:

I'm not asking if this is a good way to do this. I'm aware that I can do this like this:

var x = new TestClass {Id= x};
x.SomeProperty = SomeMethod(this);

我有一个复杂的场景,在这种情况下,对象初始化程序中对新实例的引用将使生活更轻松.

I have a complicated scenario, in which a reference to the new instance in the object initialiser would make life easier.

这有可能吗?

推荐答案

无法解决,C#规范明确指出

There's no way around it, the C# specification explicitly says that "It is not possible for an object or collection initializer to refer to the object instance being initialized."

至于为什么不可能,我怀疑没有很好的方法来实现它.我们想要一些等价的语法糖

As for why it's impossible, I suspect that there's just no nice way to implement it. We want some syntactic sugar equivalent to

var temp = new TestClass();
temp.Id = 1;
temp.SomeProperty = SomeMethod(temp);
x = temp;

我们只需要一个关键字来引用初始化器中的temp,但是没有一个关键字很容易获得.我们不能使用this,因为它已经意味着初始化程序之外的内容. SomeProperty = this.SomeMethod(this)是否应该等同于temp.SomeProperty = this.SomeMethod(temp)temp.SomeProperty = temp.SomeMethod(temp)?第二个是一致的,但是如果我们需要第一个会发生什么?

We just need a keyword to refer to temp within the initializer, but none is easily available. We can't use this because it already means something outside the initializer. Should SomeProperty = this.SomeMethod(this) be equivalent to temp.SomeProperty = this.SomeMethod(temp) or temp.SomeProperty = temp.SomeMethod(temp)? The second is consistent, but then what happens if we need the first?

我们可以尝试使用x,尽管只有在新对象立即分配给变量的情况下才能选择名称.但是,我们现在无法在初始化程序内引用x的旧值,而只能执行temp.SomeProperty = SomeMethod(x)的操作.

We could try to use x, though we can only pick a name if the new object is immediately assigned to a variable. However, we now can't refer to the old value of x inside the initializer, doing the equivalent of temp.SomeProperty = SomeMethod(x).

我们可以重用属性设置器中的value关键字.这听起来不错,因为如果您将属性获取器视为set_SomeProperty(value)方法的语法糖,则value已经代表缺少的参数.用它来引用对象初始化程序中缺少的变量看起来很有希望.但是,我们可以在属性设置器中创建此对象,在这种情况下,已经使用了value了,我们需要能够执行temp.SomeProperty = SomeMethod(value).

We could reuse the value keyword from property setters. This sounds good since value already stands in for the missing parameter if you consider a property getter to be syntactic sugar for a set_SomeProperty(value) method. Using it to also refer to the missing variable in the object initializer looks promising. However, we could be creating this object inside a property setter, in which case value is already being used, and we need to be able to do temp.SomeProperty = SomeMethod(value).

看来我们必须为此创建一个新的关键字,也许是newthis.但是,这是对语言的重大更改,因为任何具有名为newthis的变量的代码都不再起作用.微软通常需要一个很好的理由来引入重大更改,因此最好禁止访问正在初始化的对象.

It looks like we'll have to create a new keyword just for this purpose, maybe newthis. However, this is a breaking change to the language because any code that has a variable called newthis doesn't work any more. Microsoft generally needs a really good reason to introduce breaking changes, so it's better to forbid access to the object being initialized.

这篇关于C#对象初始化程序-对新实例的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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