ProgIdAttribute 类

允许用户指定类的 ProgID。

**命名空间:**System.Runtime.InteropServices
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.Class, Inherited:=False)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class ProgIdAttribute
    Inherits Attribute
用法
Dim instance As ProgIdAttribute
[AttributeUsageAttribute(AttributeTargets.Class, Inherited=false)] 
[ComVisibleAttribute(true)] 
public sealed class ProgIdAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class, Inherited=false)] 
[ComVisibleAttribute(true)] 
public ref class ProgIdAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class, Inherited=false) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class ProgIdAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Class, Inherited=false) 
ComVisibleAttribute(true) 
public final class ProgIdAttribute extends Attribute

备注

可将该属性应用于类。

将命名空间与类型名组合在一起可以为类自动生成 ProgID。但这可能产生无效的 ProgID,因为 ProgID 被限定为 39 个字符,并且除句点之外不能包含任何标点。在这种情况下,可使用 ProgIdAttribute 将 ProgID 手动分配给类。

示例

下面的示例阐释了如何对类应用 ProgIdAttribute。应用程序然后获取 MyClass 的所有属性 (Attribute),并输出 ProgIdAttributeValue 属性 (Property)。

   <ClassInterface(ClassInterfaceType.AutoDispatch), ProgId("InteropSample.MyClass")>  _
   Public Class [MyClass]
      
      Public Sub New()
      End Sub 'New
   End Class '[MyClass]

   Class TestApplication
      
      Public Shared Sub Main()
         Try
            Dim attributes As AttributeCollection
            attributes = TypeDescriptor.GetAttributes(GetType([MyClass]))
            Dim progIdAttributeObj As ProgIdAttribute = CType(attributes(GetType(ProgIdAttribute)), ProgIdAttribute)
            Console.WriteLine(("ProgIdAttribute's value is set to : " + progIdAttributeObj.Value))
         Catch e As Exception
            Console.WriteLine(("Exception : " + e.Message.ToString()))
         End Try
      End Sub 'Main
   End Class 'TestApplication
End Namespace 'InteropSample 
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ProgId("InteropSample.MyClass")]
public class MyClass
{
    public MyClass() {}
}

class TestApplication
{      
   public static void Main()
   {
      try
      {
         AttributeCollection attributes;
         attributes = TypeDescriptor.GetAttributes(typeof(MyClass));
         ProgIdAttribute progIdAttributeObj = (ProgIdAttribute)attributes[typeof(ProgIdAttribute)];
         Console.WriteLine("ProgIdAttribute's value is set to : " + progIdAttributeObj.Value);
      }         
      catch(Exception e)
      {
         Console.WriteLine("Exception : " + e.Message);
      }
   }
}
[ClassInterface(ClassInterfaceType::AutoDispatch)]
[ProgId("InteropSample.MyClass")]
public ref class MyClass
{
public:
   MyClass(){}

};

int main()
{
   try
   {
      AttributeCollection^ attributes;
      attributes = TypeDescriptor::GetAttributes( MyClass::typeid );
      ProgIdAttribute^ progIdAttributeObj = dynamic_cast<ProgIdAttribute^>(attributes[ ProgIdAttribute::typeid ]);
      Console::WriteLine( "ProgIdAttribute's value is set to : {0}", progIdAttributeObj->Value );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0}", e->Message );
   }
}
/** @attribute ClassInterface(ClassInterfaceType.AutoDispatch)
 */
/** @attribute ProgId("InteropSample.MyClass")
 */
public class MyClass
{
    public MyClass()
    {
    } //MyClass
} //MyClass

class TestApplication
{
    public static void main(String[] args)
    {
        try {
            AttributeCollection attributes;
            attributes = TypeDescriptor.GetAttributes(MyClass.class.ToType());
            ProgIdAttribute progIdAttributeObj = (ProgIdAttribute)
                (attributes.get_Item(ProgIdAttribute.class.ToType()));
            Console.WriteLine("ProgIdAttribute's value is set to : " 
                + progIdAttributeObj.get_Value());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : " + e.get_Message());
        }
    } //main
} //TestApplication

继承层次结构

System.Object
   System.Attribute
    System.Runtime.InteropServices.ProgIdAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ProgIdAttribute 成员
System.Runtime.InteropServices 命名空间