我为3个不同的密码类实现了一个工厂模式。工厂将决定创建哪一个,然后从数据库中获取正确类的序列化实例并将其返回给请求者。 现在,我正在对这些类进行序列化,以将它们存储在数据库中。我正在为一个名为BouncyCastle
的PGP密码类编写一个。我可以从文件创建类和关键字,但是当我尝试序列化它时,它说,类PgpPublicKey
和PgpPrivateKey
的对象的两个成员变量无法序列化,因为它们没有无参数构造函数。C#序列化一个没有无参数构造函数的类
public void createdBouncyFromFiles()
{
var bc = new BouncyCastle("C:\\TestFiles\\BouncyPublicKey.txt", "C:\\TestFiles\\BouncyPrivateKey.txt", "Password1");
var xmlSerializer = new XmlSerializer(bc.GetType());
var textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, bc);
var theSerializedClass = textWriter.ToString();
}
该类有两个成员变量是问题。
public class BouncyCastle : ICryptographyProvider
{
public PgpPublicKey m_publicKey;
public PgpPrivateKey m_privateKey;
public string m_passPhrase;
// cut out the irelevant parts
这是公钥类。没有无参数的构造函数。
public class PgpPublicKey
{
public PgpPublicKey(PublicKeyAlgorithmTag algorithm, AsymmetricKeyParameter pubKey, DateTime time);
// cut other methods
}
制作您自己的数据类以进行序列化,根据需要复制值 – 2013-03-04 22:09:30
为什么您不想只添加无参数构造函数? – Restuta 2013-03-04 22:09:47