简体   繁体   English

当您尝试将值设置为只读属性时会发生什么?

[英]What happens when you try to set value to a read-only property?

Recently I have been working with C# and I ran into this curious problem.最近我一直在使用 C#,我遇到了这个奇怪的问题。 I tried to set a value to the read-only property of the excel chart class, before I read the documentation and discovered that the property is read-only.在阅读文档并发现该属性是只读的之前,我尝试为 excel 图表类的只读属性设置一个值。

var charts= worksheet.ChartObjects() as xcel.ChartObjects;
var chartObj = charts.Add(60,10,200,400) as xcel.ChartObject;
var chart = chartObj.Chart;
chart.Name = "GicsSectorIndustry";   <--

I get this error:我收到此错误:

Insufficient memory to continue the execution of the program

When I remove that line, the code works perfectly, so it led me to wonder what happens behind the scenes when I try to set a value for the read-only property that causes it to "run out of memory".当我删除该行时,代码运行良好,所以当我尝试为只读属性设置一个导致它“内存不足”的值时,它让我想知道幕后会发生什么。

If it was read-only, you could not compile that code.如果它是只读的,则无法编译该代码。 This means it's not read-only but the documentation says to treat it as read-only.这意味着它不是只读的,但文档说将其视为只读。


Rephrased question: What happens when you try to assign to an assignable property that the documentation says to treat as read-only?重新表述的问题:当您尝试分配给文档说视为只读的可分配属性时会发生什么?

Short answer: Does it matter?简短的回答:重要吗?

Long answer: Without the source, it's very hard to tell.长答案:没有来源,很难说。 You could use ILSpy to debug it but the short answer still applies.您可以使用 ILSpy 对其进行调试,但简短的回答仍然适用。

It seems you are reading the wrong documentation.您似乎正在阅读错误的文档。

I had inquired as to what namespace the class you were using came from.我曾询问过您使用的类来自哪个命名空间。 According to the documentation for the Interop assembly, the signature is:根据 Interop 程序集的文档,签名是:

public:
property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };

As you can see, there is a setter.如您所见,有一个 setter。

You can read more about it here .您可以在此处阅读更多相关信息。

There exists another version (a wrapper, I imagine) from Tools for Office. Tools for Office 存在另一个版本(我想是一个包装器)。 The signature for that one does not provide a setter:该签名不提供设置器:

public string Name { get; }

More information on that one can be found here .可以在此处找到有关该方法的更多信息。

Interop assemblies bridge between managed and unmanaged code so my educated guess is that the second version (wrapper) probably exists to manage issues like the one you are experiencing, or it could be another Interop implementation all together.互操作程序集是托管和非托管代码之间的桥梁,所以我有根据的猜测是第二个版本(包装器)可能存在来管理您遇到的问题,或者它可能是另一个互操作实现。

As others have stated, if no setter was provided, your code just wouldn't compile but in this case it does.正如其他人所说,如果没有提供 setter,你的代码就不会编译,但在这种情况下它会编译。 It's just something goes wrong under the covers.只是在幕后出了点问题。

UPDATE:更新:

I found the following concerning the difference between the two implementations:我发现以下关于两种实现之间的区别:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/86a62151-fbf0-4584-a68c-83060fb95c3d/officeinteropexcel?forum=vsto https://social.msdn.microsoft.com/Forums/vstudio/en-US/86a62151-fbf0-4584-a68c-83060fb95c3d/officeinteropexcel?forum=vsto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM