小编Rog*_*con的帖子

C#序列化一个没有无参数构造函数的类

我正在为3种不同的加密类实现工厂模式.工厂将确定要创建哪一个,然后从数据库中获取正确类的序列化实例并将其返回给请求者.现在我正在编写类以将它们存储在数据库中.我正在写一个名为PGP加密类的BouncyCastle.我可以从文件创建类和键,但是当我尝试序列化它时,它表示两个成员变量,它们是类的对象PgpPublicKey,并且PgpPrivateKey由于它们没有无参数构造函数而无法序列化.

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();
}
Run Code Online (Sandbox Code Playgroud)

该类有两个成员变量是问题.

public class BouncyCastle : ICryptographyProvider
{

    public PgpPublicKey m_publicKey;
    public PgpPrivateKey m_privateKey;
    public string m_passPhrase;
    // cut out the irelevant parts
Run Code Online (Sandbox Code Playgroud)

这是公钥类.没有无参数的构造函数.

public class PgpPublicKey
{
    public PgpPublicKey(PublicKeyAlgorithmTag algorithm, AsymmetricKeyParameter pubKey, DateTime time);
    // cut other methods
}
Run Code Online (Sandbox Code Playgroud)

c# serialization bouncycastle

12
推荐指数
2
解决办法
3万
查看次数

标签 统计

bouncycastle ×1

c# ×1

serialization ×1