Implement Marshal.GenerateProgIdForType
authorAlistair Leslie-Hughes <leslie_alistair@hotmail.com>
Wed, 24 Oct 2012 05:36:32 +0000 (16:36 +1100)
committerAlistair Leslie-Hughes <leslie_alistair@hotmail.com>
Wed, 31 Oct 2012 02:48:32 +0000 (13:48 +1100)
mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs
mcs/class/corlib/Test/System.Runtime.InteropServices/MarshalTest.cs

index 43cf9bfe2ea147e350cbaaa314d07a366256f953..1e9673cb3576aafb659dfdaeae965aa02980b135 100644 (file)
@@ -31,6 +31,7 @@
 //
 
 using System.Collections;
+using System.Collections.Generic;
 using System.Runtime.CompilerServices;
 using System;
 using System.Security;
@@ -272,10 +273,27 @@ namespace System.Runtime.InteropServices
                        return type.GUID;
                }
 
-               [MonoTODO]
                public static string GenerateProgIdForType (Type type)
                {
-                       throw new NotImplementedException ();
+                       IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (type);
+
+                       foreach (var a in attrs)
+                       {
+                               var dt = a.Constructor.DeclaringType;
+                               string name = dt.Name;
+                               if (name == "ProgIdAttribute")
+                               {
+                                       var args = a.ConstructorArguments;
+                                       string text = a.ConstructorArguments[0].Value as string;
+                                       if (text == null)
+                                       {
+                                               text = string.Empty;
+                                       }
+                                       return text;
+                               }
+                       }
+
+                       return type.FullName;
                }
 
                [MonoTODO]
index 21a8a573c47e1ddc2d9b82add36c7f9994ec8ee7..334fc1c7fdc9741a367f9ec742644fa787863ccf 100644 (file)
@@ -660,6 +660,18 @@ namespace MonoTests.System.Runtime.InteropServices
                        }
                }
 
+               [Test]
+               public void TestGenerateProgIdForType()
+               {
+                       string output;
+                       
+                       output = Marshal.GenerateProgIdForType(typeof(TestCoClass));
+                       Assert.AreEqual ("MonoTests.System.Runtime.InteropServices.TestCoClass", output, "#1");
+                       
+                       output = Marshal.GenerateProgIdForType(typeof(TestCoClassWithProgId));
+                       Assert.AreEqual ("CoClassWithProgId", output, "#2");
+               }
+
                [Test]
                public void TestGlobalAlloc ()
                {
@@ -754,5 +766,13 @@ namespace MonoTests.System.Runtime.InteropServices
                {
                }
        }
+
+       [ProgId("CoClassWithProgId")]
+       public class TestCoClassWithProgId : ITestDispatch
+       {
+               public void DoNothing ()
+               {
+               }
+       }
 }
 #endif