好吧,我可能只是遇到了一个史诗般的失败,但我想说这应该行得通。
假设 DataProtect.DecryptData 将加密的字符串作为输入,并将解密的字符串作为输出。假设 deserializeXML 生成适当的对象并从新解密的字符串中返回它。
所以。为什么这行不通?
class ArrivedDetails
{
///...
internal ArrivedDetails(string encrypted)
{
this = DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
}
///...
给我一个错误
Cannot assign to '<this>' because it's read only
更具体地说,我怎样才能让它发挥作用?我基本上想解密对象的 XML 序列化版本,然后在构造函数中反序列化它。
我对“你不能”(有解释)持开放态度,因为我可以把它放在其他地方并只是赋值,但我的想法是这样的事情应该是可能的。
最佳答案
不,这不可能使用构造函数,您不能重新分配 this
。
改用静态方法:
public static ArrivedDetails CreateFromString(string encrypted)
{
return DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
}
调用它:
ArrivedDetails details = ArrivedDetails.CreateFromString(encrypted);
关于c# - 反序列化为自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5398904/