当两个程序集具有相同的名称空间时,如何解决“无法隐式转换”错误?

时间:2019-01-31 12:44:33

标签: c# .net-assembly conflicting-libraries

我正在编写证书生成程序,但是遇到了BouncyCastle和ITextSharp-LGPL-4.1.6之间的程序集名称空间冲突之类的事情。

因此,我尝试将别名添加到我正在使用的BouncyCastle库中,并尝试进行显式转换,但是没有任何效果。

return (BouncyCastleCrypto::Org.BouncyCastle.X509.X509Certificate)cert.Generate(pair.Private);

结果是我得到一个错误:

CS0029 Cannot implicitly convert type 'Org.BouncyCastle.X509.X509Certificate [C:\XXX\packages\BouncyCastle.1.8.4\lib\BouncyCastle.Crypto.dll]' to 'Org.BouncyCastle.X509.X509Certificate [C:\XXX\packages\iTextSharp-LGPL.4.1.6\lib\iTextSharp.dll]' Project C:\xxx\src\MyCode.cs

我不知道现在该怎么办,我没有选择。

EDIT1:

问题不是关于访问类型本身,而是关于调用类型的方法。

我有这部分代码:

BouncyCastleCrypto::Org.BouncyCastle.X509.X509V3CertificateGenerator cert = new BouncyCastleCrypto::Org.BouncyCastle.X509.X509V3CertificateGenerator();

然后我有这部分代码:

return cert.Generate(pair.Private);

哪个给我错误。当这只是方法调用时,我应该如何阐明我正在使用哪个程序集?

EDIT2:

好的,所以我在这里粘贴整个文件:

extern alias BouncyCastleCrypto;
using BouncyCastleCrypto.Org.BouncyCastle.Crypto.Operators;
using BouncyCastleCrypto.Org.BouncyCastle.Asn1.Pkcs;
using BouncyCastleCrypto.Org.BouncyCastle.Asn1.X509;
using BouncyCastleCrypto.Org.BouncyCastle.Crypto;
using BouncyCastleCrypto.Org.BouncyCastle.Crypto.Generators;
using BouncyCastleCrypto.Org.BouncyCastle.Crypto.Prng;
using BouncyCastleCrypto.Org.BouncyCastle.OpenSsl;
using BouncyCastleCrypto.Org.BouncyCastle.Pkcs;
using BouncyCastleCrypto.Org.BouncyCastle.Security;
using XXXX.ViewModels;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using BouncyCastleCrypto.Org.BouncyCastle.X509;
using BouncyCastleCrypto.Org.BouncyCastle.Math;

namespace XXXX.Helpers
{
  public static class PKIHelper
  {


public static Org.BouncyCastle.X509.X509Certificate GenCert(CertInfo info)
{
  RsaKeyPairGenerator _rsa = new RsaKeyPairGenerator();
  SecureRandom _random = new SecureRandom();

  _rsa.Init(new KeyGenerationParameters(_random, info.rsa_strength));
  AsymmetricCipherKeyPair _pair = _rsa.GenerateKeyPair();

  X509Name _cert_name = new X509Name("CN=" + info.name);
  BigInteger _serialnumber = BigInteger.ProbablePrime(120, new Random());

  BouncyCastleCrypto::Org.BouncyCastle.X509.X509V3CertificateGenerator _cert = new BouncyCastleCrypto::Org.BouncyCastle.X509.X509V3CertificateGenerator();
  _cert.SetSerialNumber(_serialnumber);
  _cert.SetSubjectDN(_cert_name);
  _cert.SetIssuerDN(_cert_name);
  _cert.SetNotBefore(info.begin_date);
  _cert.SetNotAfter(info.expire_date);
  _cert.SetSignatureAlgorithm("SHA1withRSA");
  _cert.SetPublicKey(_pair.Public);

  _cert.AddExtension(X509Extensions.ExtendedKeyUsage.Id, false,
      new AuthorityKeyIdentifier(
          SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(_pair.Public),
          new GeneralNames(new GeneralName(_cert_name)), _serialnumber));
  _cert.AddExtension(X509Extensions.ExtendedKeyUsage.Id, false,
      new ExtendedKeyUsage(new[] { KeyPurposeID.IdKPServerAuth }));


  return _cert.Generate(_pair.Private); // here's error
}

  }
}

1 个答案:

答案 0 :(得分:0)

好的,那是我很确定自己使用了正确的类型作为GeneratePKI方法的Org.BouncyCastle.X509.X509Certificate方法的返回类型,但实际上Org.BouncyCastle.X509.X509Certificate来自iTextSharp库,因此编译器认为它必须隐式转换它。当我在方法返回类型BouncyCastleCrypto::Org.BouncyCastle.X509.X509Certificate之前添加别名时,它们都神奇地再次开始编译。感谢@devNull,不要遗弃我。