From 9aa68da7d3dcd24761d03c4f41720194ab3638b8 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 4 Feb 2019 11:36:06 -0800 Subject: [PATCH] More compact implementation of Marshal.GenerateProgIdForType (#22395) --- .../System/Runtime/InteropServices/Marshal.cs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs index 06a3a6bf382d..a1c2f172c5cd 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs @@ -847,21 +847,10 @@ public static string GenerateProgIdForType(Type type) throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type)); } - foreach (CustomAttributeData cad in type.GetCustomAttributesData()) + ProgIdAttribute progIdAttribute = type.GetCustomAttribute(); + if (progIdAttribute != null) { - if (cad.Constructor.DeclaringType == typeof(ProgIdAttribute)) - { - // Retrieve the PROGID string from the ProgIdAttribute. - IList caConstructorArgs = cad.ConstructorArguments; - Debug.Assert(caConstructorArgs.Count == 1, "caConstructorArgs.Count == 1"); - - CustomAttributeTypedArgument progIdConstructorArg = caConstructorArgs[0]; - Debug.Assert(progIdConstructorArg.ArgumentType == typeof(string), "progIdConstructorArg.ArgumentType == typeof(string)"); - - string strProgId = (string)progIdConstructorArg.Value; - - return strProgId ?? string.Empty; - } + return progIdAttribute.Value ?? string.Empty; } // If there is no prog ID attribute then use the full name of the type as the prog id.