当两个程序集具有相同的命名空间时,如何解决 'Cannot implicitly convert' 错误?
How to solve 'Cannot implicitly convert' error when two assemblies have the same namespace?
我正在编写证书生成程序,但我遇到了类似 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
我现在不知道该怎么办,我别无选择。
编辑 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
}
}
}
好吧,我很确定我使用的 return 类型是 GeneratePKI
方法的正确类型,即 Org.BouncyCastle.X509.X509Certificate
,但实际上 Org.BouncyCastle.X509.X509Certificate
来自 iTextSharp
库,因此编译器认为它必须隐式地转换它。当我在方法 return 类型 BouncyCastleCrypto::Org.BouncyCastle.X509.X509Certificate
之前添加别名时,它神奇地再次开始编译。感谢@devNull,没有抛弃我。
我正在编写证书生成程序,但我遇到了类似 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
我现在不知道该怎么办,我别无选择。
编辑 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
}
}
}
好吧,我很确定我使用的 return 类型是 GeneratePKI
方法的正确类型,即 Org.BouncyCastle.X509.X509Certificate
,但实际上 Org.BouncyCastle.X509.X509Certificate
来自 iTextSharp
库,因此编译器认为它必须隐式地转换它。当我在方法 return 类型 BouncyCastleCrypto::Org.BouncyCastle.X509.X509Certificate
之前添加别名时,它神奇地再次开始编译。感谢@devNull,没有抛弃我。