Type.IsInterface 属性

定义

获取一个值,通过该值指示 Type 是否是一个接口;即,不是类或值类型。

public:
 property bool IsInterface { bool get(); };
public bool IsInterface { get; }
member this.IsInterface : bool
Public ReadOnly Property IsInterface As Boolean

属性值

Boolean

如果 true 是接口,则为 Type;否则为 false

实现

示例

以下示例创建接口,检查接口类型,并指示类是否设置了 IsInterface 属性。

using namespace System;

// Declare an interface.
interface class myIFace{};
public ref class MyIsInterface{};

void main()
{
   try
   {
      // Get the IsInterface attribute for myIFace.
      bool myBool1 = myIFace::typeid->IsInterface;
      
      //Display the IsInterface attribute for myIFace.
      Console::WriteLine( "Is the specified type an interface? {0}.", myBool1 );
      
      // Get the attribute IsInterface for MyIsInterface.
      bool myBool2 = MyIsInterface::typeid->IsInterface;
      
      //Display the IsInterface attribute for MyIsInterface.
      Console::WriteLine( "Is the specified type an interface? {0}.", myBool2 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "\nAn exception occurred: {0}.", e->Message );
   }
}
/* The example produces the following output:

Is the specified type an interface? True.
Is the specified type an interface? False.
*/
using System;
// Declare an interface.
interface myIFace
{
}
class MyIsInterface
{
    public static void Main(string []args)
    {
        try
        {
            // Get the IsInterface attribute for myIFace.
            bool myBool1 = typeof(myIFace).IsInterface;
            //Display the IsInterface attribute for myIFace.
            Console.WriteLine("Is the specified type an interface? {0}.", myBool1);
            // Get the attribute IsInterface for MyIsInterface.
            bool myBool2 = typeof(MyIsInterface).IsInterface;
            //Display the IsInterface attribute for MyIsInterface.
            Console.WriteLine("Is the specified type an interface? {0}.", myBool2);
        }
        catch(Exception e)
        {
            Console.WriteLine("\nAn exception occurred: {0}.", e.Message);
        }
    }
}
/* The example produces the following output:

Is the specified type an interface? True.
Is the specified type an interface? False.
*/
' Declare an interface.
Interface myIFace
End Interface

Class MyIsInterface
    Public Shared Sub Main()
        ' Get the IsInterface attribute for myIFace.
        Dim myBool1 As Boolean = GetType(myIFace).IsInterface
        Console.WriteLine("Is the specified type an interface? {0}.", myBool1)

        ' Determine whether Example is an interface.
        Dim myBool2 As Boolean = GetType(MyIsInterface).IsInterface
        Console.WriteLine("Is the specified type an interface? {0}.", myBool2)
        Console.ReadLine()

    End Sub
End Class
' The example displays the following output:
'       Is the specified type an interface? True.
'       Is the specified type an interface? False.

注解

ClassSemanticsMask将类型声明区分为类、接口或值类型。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此属性始终返回 false

此属性为只读。

适用于

另请参阅