From 58af12ad76e7bb6ac68278d45109da4472a3aae8 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 6 Nov 2022 14:48:05 +0700 Subject: Asn1 GetInstance refactoring --- crypto/src/asn1/Asn1Null.cs | 22 ++++++++++++---------- crypto/src/asn1/Asn1ObjectDescriptor.cs | 22 ++++++++++++---------- crypto/src/asn1/Asn1OctetString.cs | 23 ++++++++++++----------- crypto/src/asn1/Asn1RelativeOid.cs | 22 ++++++++++++---------- crypto/src/asn1/Asn1Sequence.cs | 23 ++++++++++++----------- crypto/src/asn1/Asn1Set.cs | 23 ++++++++++++----------- crypto/src/asn1/Asn1TaggedObject.cs | 21 +++++++++++---------- crypto/src/asn1/DERExternal.cs | 22 ++++++++++++---------- crypto/src/asn1/DerBMPString.cs | 22 ++++++++++++---------- crypto/src/asn1/DerBitString.cs | 23 ++++++++++++----------- crypto/src/asn1/DerBoolean.cs | 22 ++++++++++++---------- crypto/src/asn1/DerEnumerated.cs | 22 ++++++++++++---------- crypto/src/asn1/DerGeneralString.cs | 22 ++++++++++++---------- crypto/src/asn1/DerGraphicString.cs | 22 ++++++++++++---------- crypto/src/asn1/DerIA5String.cs | 22 ++++++++++++---------- crypto/src/asn1/DerInteger.cs | 22 ++++++++++++---------- crypto/src/asn1/DerNumericString.cs | 22 ++++++++++++---------- crypto/src/asn1/DerObjectIdentifier.cs | 22 ++++++++++++---------- crypto/src/asn1/DerPrintableString.cs | 22 ++++++++++++---------- crypto/src/asn1/DerT61String.cs | 22 ++++++++++++---------- crypto/src/asn1/DerUTF8String.cs | 22 ++++++++++++---------- crypto/src/asn1/DerUniversalString.cs | 25 +++++++++++++------------ crypto/src/asn1/DerVideotexString.cs | 22 ++++++++++++---------- crypto/src/asn1/DerVisibleString.cs | 25 +++++++++++++------------ 24 files changed, 289 insertions(+), 248 deletions(-) diff --git a/crypto/src/asn1/Asn1Null.cs b/crypto/src/asn1/Asn1Null.cs index 9ea9b4375..77304c0fb 100644 --- a/crypto/src/asn1/Asn1Null.cs +++ b/crypto/src/asn1/Asn1Null.cs @@ -25,21 +25,23 @@ namespace Org.BouncyCastle.Asn1 public static Asn1Null GetInstance(object obj) { - if (obj == null || obj is Asn1Null) - { - return (Asn1Null)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is Asn1Null asn1Null) + return asn1Null; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is Asn1Null) - return (Asn1Null)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is Asn1Null converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (Asn1Null)Meta.Instance.FromByteArray((byte[])obj); + return (Asn1Null)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/Asn1ObjectDescriptor.cs b/crypto/src/asn1/Asn1ObjectDescriptor.cs index 9c99f441e..13521a744 100644 --- a/crypto/src/asn1/Asn1ObjectDescriptor.cs +++ b/crypto/src/asn1/Asn1ObjectDescriptor.cs @@ -36,21 +36,23 @@ namespace Org.BouncyCastle.Asn1 */ public static Asn1ObjectDescriptor GetInstance(object obj) { - if (obj == null || obj is Asn1ObjectDescriptor) - { - return (Asn1ObjectDescriptor)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is Asn1ObjectDescriptor asn1ObjectDescriptor) + return asn1ObjectDescriptor; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is Asn1ObjectDescriptor) - return (Asn1ObjectDescriptor)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is Asn1ObjectDescriptor converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (Asn1ObjectDescriptor)Meta.Instance.FromByteArray((byte[])obj); + return (Asn1ObjectDescriptor)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/Asn1OctetString.cs b/crypto/src/asn1/Asn1OctetString.cs index d34686134..8f7da8800 100644 --- a/crypto/src/asn1/Asn1OctetString.cs +++ b/crypto/src/asn1/Asn1OctetString.cs @@ -36,22 +36,23 @@ namespace Org.BouncyCastle.Asn1 */ public static Asn1OctetString GetInstance(object obj) { - if (obj == null || obj is Asn1OctetString) - { - return (Asn1OctetString)obj; - } - //else if (obj is Asn1OctetStringParser) - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is Asn1OctetString asn1OctetString) + return asn1OctetString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is Asn1OctetString) - return (Asn1OctetString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is Asn1OctetString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (Asn1OctetString)Meta.Instance.FromByteArray((byte[])obj); + return (Asn1OctetString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/Asn1RelativeOid.cs b/crypto/src/asn1/Asn1RelativeOid.cs index a1997864d..3c4bf237a 100644 --- a/crypto/src/asn1/Asn1RelativeOid.cs +++ b/crypto/src/asn1/Asn1RelativeOid.cs @@ -29,21 +29,23 @@ namespace Org.BouncyCastle.Asn1 public static Asn1RelativeOid GetInstance(object obj) { - if (obj == null || obj is Asn1RelativeOid) - { - return (Asn1RelativeOid)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is Asn1RelativeOid asn1RelativeOid) + return asn1RelativeOid; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is Asn1RelativeOid) - return (Asn1RelativeOid)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is Asn1RelativeOid converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (Asn1RelativeOid)FromByteArray((byte[])obj); + return (Asn1RelativeOid)FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/Asn1Sequence.cs b/crypto/src/asn1/Asn1Sequence.cs index 1a123e26d..a8191de99 100644 --- a/crypto/src/asn1/Asn1Sequence.cs +++ b/crypto/src/asn1/Asn1Sequence.cs @@ -30,22 +30,23 @@ namespace Org.BouncyCastle.Asn1 */ public static Asn1Sequence GetInstance(object obj) { - if (obj == null || obj is Asn1Sequence) - { - return (Asn1Sequence)obj; - } - //else if (obj is Asn1SequenceParser) - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is Asn1Sequence asn1Sequence) + return asn1Sequence; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is Asn1Sequence) - return (Asn1Sequence)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is Asn1Sequence converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (Asn1Sequence)Meta.Instance.FromByteArray((byte[])obj); + return (Asn1Sequence)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/Asn1Set.cs b/crypto/src/asn1/Asn1Set.cs index faec50eb0..2b3810e43 100644 --- a/crypto/src/asn1/Asn1Set.cs +++ b/crypto/src/asn1/Asn1Set.cs @@ -31,22 +31,23 @@ namespace Org.BouncyCastle.Asn1 */ public static Asn1Set GetInstance(object obj) { - if (obj == null || obj is Asn1Set) - { - return (Asn1Set)obj; - } - //else if (obj is Asn1SetParser) - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is Asn1Set asn1Set) + return asn1Set; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is Asn1Set) - return (Asn1Set)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is Asn1Set converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (Asn1Set)Meta.Instance.FromByteArray((byte[])obj); + return (Asn1Set)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/Asn1TaggedObject.cs b/crypto/src/asn1/Asn1TaggedObject.cs index 46aa137a8..63ab6a5d0 100644 --- a/crypto/src/asn1/Asn1TaggedObject.cs +++ b/crypto/src/asn1/Asn1TaggedObject.cs @@ -21,22 +21,23 @@ namespace Org.BouncyCastle.Asn1 public static Asn1TaggedObject GetInstance(object obj) { - if (obj == null || obj is Asn1TaggedObject) - { - return (Asn1TaggedObject)obj; - } - //else if (obj is Asn1TaggedObjectParser) - else if (obj is IAsn1Convertible asn1Convertible) + if (obj == null) + return null; + + if (obj is Asn1TaggedObject asn1TaggedObject) + return asn1TaggedObject; + + if (obj is IAsn1Convertible asn1Convertible) { Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); - if (asn1Object is Asn1TaggedObject taggedObject) - return taggedObject; + if (asn1Object is Asn1TaggedObject converted) + return converted; } - else if (obj is byte[] byteArray) + else if (obj is byte[] bytes) { try { - return CheckedCast(FromByteArray(byteArray)); + return CheckedCast(FromByteArray(bytes)); } catch (IOException e) { diff --git a/crypto/src/asn1/DERExternal.cs b/crypto/src/asn1/DERExternal.cs index 9fba95165..207930062 100644 --- a/crypto/src/asn1/DERExternal.cs +++ b/crypto/src/asn1/DERExternal.cs @@ -25,21 +25,23 @@ namespace Org.BouncyCastle.Asn1 public static DerExternal GetInstance(object obj) { - if (obj == null || obj is DerExternal) - { - return (DerExternal)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerExternal derExternal) + return derExternal; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerExternal) - return (DerExternal)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerExternal converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerExternal)Meta.Instance.FromByteArray((byte[])obj); + return (DerExternal)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerBMPString.cs b/crypto/src/asn1/DerBMPString.cs index a289eed1b..284a4b830 100644 --- a/crypto/src/asn1/DerBMPString.cs +++ b/crypto/src/asn1/DerBMPString.cs @@ -31,21 +31,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerBmpString GetInstance(object obj) { - if (obj == null || obj is DerBmpString) - { - return (DerBmpString)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerBmpString derBmpString) + return derBmpString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerBmpString) - return (DerBmpString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerBmpString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerBmpString)Meta.Instance.FromByteArray((byte[])obj); + return (DerBmpString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerBitString.cs b/crypto/src/asn1/DerBitString.cs index 8aab88353..44b3bb95a 100644 --- a/crypto/src/asn1/DerBitString.cs +++ b/crypto/src/asn1/DerBitString.cs @@ -38,22 +38,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerBitString GetInstance(object obj) { - if (obj == null || obj is DerBitString) - { - return (DerBitString)obj; - } - //else if (obj is Asn1BitStringParser) - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerBitString derBitString) + return derBitString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerBitString) - return (DerBitString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerBitString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return GetInstance(FromByteArray((byte[])obj)); + return GetInstance(FromByteArray(bytes)); } catch (IOException e) { diff --git a/crypto/src/asn1/DerBoolean.cs b/crypto/src/asn1/DerBoolean.cs index ad578ae80..6256db6e0 100644 --- a/crypto/src/asn1/DerBoolean.cs +++ b/crypto/src/asn1/DerBoolean.cs @@ -30,21 +30,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerBoolean GetInstance(object obj) { - if (obj == null || obj is DerBoolean) - { - return (DerBoolean)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerBoolean derBoolean) + return derBoolean; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerBoolean) - return (DerBoolean)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerBoolean converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerBoolean)Meta.Instance.FromByteArray((byte[])obj); + return (DerBoolean)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerEnumerated.cs b/crypto/src/asn1/DerEnumerated.cs index 920b3dc8e..b85c5a43e 100644 --- a/crypto/src/asn1/DerEnumerated.cs +++ b/crypto/src/asn1/DerEnumerated.cs @@ -28,21 +28,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerEnumerated GetInstance(object obj) { - if (obj == null || obj is DerEnumerated) - { - return (DerEnumerated)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerEnumerated derEnumerated) + return derEnumerated; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerEnumerated) - return (DerEnumerated)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerEnumerated converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerEnumerated)Meta.Instance.FromByteArray((byte[])obj); + return (DerEnumerated)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerGeneralString.cs b/crypto/src/asn1/DerGeneralString.cs index e6637732a..6a378307d 100644 --- a/crypto/src/asn1/DerGeneralString.cs +++ b/crypto/src/asn1/DerGeneralString.cs @@ -22,21 +22,23 @@ namespace Org.BouncyCastle.Asn1 public static DerGeneralString GetInstance(object obj) { - if (obj == null || obj is DerGeneralString) - { - return (DerGeneralString) obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerGeneralString derGeneralString) + return derGeneralString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerGeneralString) - return (DerGeneralString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerGeneralString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerGeneralString)Meta.Instance.FromByteArray((byte[])obj); + return (DerGeneralString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerGraphicString.cs b/crypto/src/asn1/DerGraphicString.cs index cb32d14eb..85330eb33 100644 --- a/crypto/src/asn1/DerGraphicString.cs +++ b/crypto/src/asn1/DerGraphicString.cs @@ -29,21 +29,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerGraphicString GetInstance(object obj) { - if (obj == null || obj is DerGraphicString) - { - return (DerGraphicString)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerGraphicString derGraphicString) + return derGraphicString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerGraphicString) - return (DerGraphicString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerGraphicString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerGraphicString)Meta.Instance.FromByteArray((byte[])obj); + return (DerGraphicString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerIA5String.cs b/crypto/src/asn1/DerIA5String.cs index a56879831..de2860130 100644 --- a/crypto/src/asn1/DerIA5String.cs +++ b/crypto/src/asn1/DerIA5String.cs @@ -30,21 +30,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerIA5String GetInstance(object obj) { - if (obj == null || obj is DerIA5String) - { - return (DerIA5String)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerIA5String derIA5String) + return derIA5String; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerIA5String) - return (DerIA5String)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerIA5String converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerIA5String)Meta.Instance.FromByteArray((byte[])obj); + return (DerIA5String)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerInteger.cs b/crypto/src/asn1/DerInteger.cs index c8d4e47df..05a790743 100644 --- a/crypto/src/asn1/DerInteger.cs +++ b/crypto/src/asn1/DerInteger.cs @@ -42,21 +42,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerInteger GetInstance(object obj) { - if (obj == null || obj is DerInteger) - { - return (DerInteger)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerInteger derInteger) + return derInteger; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerInteger) - return (DerInteger)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerInteger converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerInteger)Meta.Instance.FromByteArray((byte[])obj); + return (DerInteger)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerNumericString.cs b/crypto/src/asn1/DerNumericString.cs index 693ff7d6e..819d946b1 100644 --- a/crypto/src/asn1/DerNumericString.cs +++ b/crypto/src/asn1/DerNumericString.cs @@ -30,21 +30,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerNumericString GetInstance(object obj) { - if (obj == null || obj is DerNumericString) - { - return (DerNumericString)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerNumericString derNumericString) + return derNumericString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerNumericString) - return (DerNumericString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerNumericString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerNumericString)Meta.Instance.FromByteArray((byte[])obj); + return (DerNumericString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerObjectIdentifier.cs b/crypto/src/asn1/DerObjectIdentifier.cs index b10f8f8b6..cb5771958 100644 --- a/crypto/src/asn1/DerObjectIdentifier.cs +++ b/crypto/src/asn1/DerObjectIdentifier.cs @@ -34,21 +34,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerObjectIdentifier GetInstance(object obj) { - if (obj == null || obj is DerObjectIdentifier) - { - return (DerObjectIdentifier)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerObjectIdentifier derObjectIdentifier) + return derObjectIdentifier; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerObjectIdentifier) - return (DerObjectIdentifier)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerObjectIdentifier converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerObjectIdentifier)Meta.Instance.FromByteArray((byte[])obj); + return (DerObjectIdentifier)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerPrintableString.cs b/crypto/src/asn1/DerPrintableString.cs index 3c44a2d52..5830afa47 100644 --- a/crypto/src/asn1/DerPrintableString.cs +++ b/crypto/src/asn1/DerPrintableString.cs @@ -30,21 +30,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerPrintableString GetInstance(object obj) { - if (obj == null || obj is DerPrintableString) - { - return (DerPrintableString)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerPrintableString derPrintableString) + return derPrintableString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerPrintableString) - return (DerPrintableString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerPrintableString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerPrintableString)Meta.Instance.FromByteArray((byte[])obj); + return (DerPrintableString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerT61String.cs b/crypto/src/asn1/DerT61String.cs index a0e4f1d22..45f57ae0a 100644 --- a/crypto/src/asn1/DerT61String.cs +++ b/crypto/src/asn1/DerT61String.cs @@ -30,21 +30,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerT61String GetInstance(object obj) { - if (obj == null || obj is DerT61String) - { - return (DerT61String)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerT61String derT61String) + return derT61String; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerT61String) - return (DerT61String)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerT61String converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerT61String)Meta.Instance.FromByteArray((byte[])obj); + return (DerT61String)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerUTF8String.cs b/crypto/src/asn1/DerUTF8String.cs index d15a19d39..9472f5082 100644 --- a/crypto/src/asn1/DerUTF8String.cs +++ b/crypto/src/asn1/DerUTF8String.cs @@ -30,21 +30,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerUtf8String GetInstance(object obj) { - if (obj == null || obj is DerUtf8String) - { - return (DerUtf8String)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerUtf8String derUtf8String) + return derUtf8String; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerUtf8String) - return (DerUtf8String)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerUtf8String converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerUtf8String)Meta.Instance.FromByteArray((byte[])obj); + return (DerUtf8String)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerUniversalString.cs b/crypto/src/asn1/DerUniversalString.cs index e4e93bd7d..1183ed1f3 100644 --- a/crypto/src/asn1/DerUniversalString.cs +++ b/crypto/src/asn1/DerUniversalString.cs @@ -32,24 +32,25 @@ namespace Org.BouncyCastle.Asn1 * * @exception ArgumentException if the object cannot be converted. */ - public static DerUniversalString GetInstance( - object obj) + public static DerUniversalString GetInstance(object obj) { - if (obj == null || obj is DerUniversalString) - { - return (DerUniversalString)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerUniversalString derUniversalString) + return derUniversalString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerUniversalString) - return (DerUniversalString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerUniversalString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerUniversalString)Meta.Instance.FromByteArray((byte[])obj); + return (DerUniversalString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerVideotexString.cs b/crypto/src/asn1/DerVideotexString.cs index a5fbe0602..636af0499 100644 --- a/crypto/src/asn1/DerVideotexString.cs +++ b/crypto/src/asn1/DerVideotexString.cs @@ -29,21 +29,23 @@ namespace Org.BouncyCastle.Asn1 */ public static DerVideotexString GetInstance(object obj) { - if (obj == null || obj is DerVideotexString) - { - return (DerVideotexString)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerVideotexString derVideotexString) + return derVideotexString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerVideotexString) - return (DerVideotexString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerVideotexString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerVideotexString)Meta.Instance.FromByteArray((byte[])obj); + return (DerVideotexString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { diff --git a/crypto/src/asn1/DerVisibleString.cs b/crypto/src/asn1/DerVisibleString.cs index 359370040..77fe54c9a 100644 --- a/crypto/src/asn1/DerVisibleString.cs +++ b/crypto/src/asn1/DerVisibleString.cs @@ -28,24 +28,25 @@ namespace Org.BouncyCastle.Asn1 * * @exception ArgumentException if the object cannot be converted. */ - public static DerVisibleString GetInstance( - object obj) + public static DerVisibleString GetInstance(object obj) { - if (obj == null || obj is DerVisibleString) - { - return (DerVisibleString)obj; - } - else if (obj is IAsn1Convertible) + if (obj == null) + return null; + + if (obj is DerVisibleString derVisibleString) + return derVisibleString; + + if (obj is IAsn1Convertible asn1Convertible) { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerVisibleString) - return (DerVisibleString)asn1Object; + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is DerVisibleString converted) + return converted; } - else if (obj is byte[]) + else if (obj is byte[] bytes) { try { - return (DerVisibleString)Meta.Instance.FromByteArray((byte[])obj); + return (DerVisibleString)Meta.Instance.FromByteArray(bytes); } catch (IOException e) { -- cgit 1.4.1