当两个程序集具有相同的命名空间时'Cannot implicitly convert'如何解决错误?



我正在编写证书生成,但我正在经历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:XXXpackagesBouncyCastle.1.8.4libBouncyCastle.Crypto.dll]' to 'Org.BouncyCastle.X509.X509Certificate [C:XXXpackagesiTextSharp-LGPL.4.1.6libiTextSharp.dll]' Project C:xxxsrcMyCode.cs

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

编辑1:

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

我有这部分代码:

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

然后我有这部分代码:

return cert.Generate(pair.Private);

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

编辑2:

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

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
}
  }
}
好的,事情

是我很确定我使用正确的类型作为GeneratePKI方法的返回类型,这很Org.BouncyCastle.X509.X509Certificate,但实际上Org.BouncyCastle.X509.X509Certificate来自iTextSharp库,因此编译器认为它必须隐式地包含它。当我在方法返回类型BouncyCastleCrypto::Org.BouncyCastle.X509.X509Certificate之前添加别名时,一切都神奇地再次开始编译。谢谢@devNull,没有抛弃我。

相关内容

  • 没有找到相关文章

最新更新