反序列化到自我 [英] Deserialize to self
问题描述
好吧,我可能只是有一个史诗般的失败在这里,但我心里想说这应该工作。
Ok, I'm probably just having an epic fail here, but my mind wants to say this should work.
假设DataProtect.DecryptData需要一个加密的字符串作为输入和一个解密的字符串作为输出。假设deserializeXML进行适当的对象,从新近解密的字符串返回。
Assume DataProtect.DecryptData takes an encrypted string as input and a decrypted string as output. Assume deserializeXML makes the appropriate object and returns it from the newly decrypted string.
所以。为什么不这项工作?
So. Why wouldn't this work?
class ArrivedDetails
{
///...
internal ArrivedDetails(string encrypted)
{
this = DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
}
///...
给我的误差
Gives me an error of
Cannot assign to '<this>' because it's read only
更具体地说,,我怎么能得到这个工作?我基本上是要解密对象的序列化XML版本,然后在构造函数中反序列化。
More specifically,, how can I get this working? I essentially want to decrypt an XML serialized version of the object and then deserialize it within the constructor.
我愿意你不能(有一个解释),我可以把它放在其他地方,只是赋值,心里说,这样的事情应该是可能的。
I'm open to "you can't" (with an explanation) as I can put it elsewhere and just assign values, but my mind says something like this should be possible.
推荐答案
没有,这不是不可能的构造函数,则无法重新分配这个
No, this is not impossible with a constructor, you can't reassign this
.
使用一个静态方法来代替:
Use a static method instead:
public static ArrivedDetails CreateFromString(string encrypted)
{
return DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
}
调用它:
Call it:
ArrivedDetails details = ArrivedDetails.CreateFromString(encrypted);
这篇关于反序列化到自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!