More compact implementation of Marshal.GenerateProgIdForType (#22395)
authorJan Kotas <jkotas@microsoft.com>
Mon, 4 Feb 2019 19:36:06 +0000 (11:36 -0800)
committerGitHub <noreply@github.com>
Mon, 4 Feb 2019 19:36:06 +0000 (11:36 -0800)
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs

index 06a3a6b..a1c2f17 100644 (file)
@@ -847,21 +847,10 @@ namespace System.Runtime.InteropServices
                 throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));
             }
 
-            foreach (CustomAttributeData cad in type.GetCustomAttributesData())
+            ProgIdAttribute progIdAttribute = type.GetCustomAttribute<ProgIdAttribute>();
+            if (progIdAttribute != null)
             {
-                if (cad.Constructor.DeclaringType == typeof(ProgIdAttribute))
-                {
-                    // Retrieve the PROGID string from the ProgIdAttribute.
-                    IList<CustomAttributeTypedArgument> 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.