Type.IsPrimitive 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出 Type 是否為其中一個基本類型 (Primitive Type)。
public:
property bool IsPrimitive { bool get(); };
public bool IsPrimitive { get; }
member this.IsPrimitive : bool
Public ReadOnly Property IsPrimitive As Boolean
屬性值
如果 true
是其中一個基本類型,則為 Type,否則為 false
。
實作
範例
下列範例示範 IsContextful 類別的 Type 、 IsMarshalByRef 和 IsPrimitive
屬性。 它會檢查指定的型別是否可以裝載于內容中、是否可以以傳址方式封送處理,以及類型是否為基本資料類型。
using namespace System;
using namespace System::Runtime::Remoting::Contexts;
public ref class ContextBoundClass: public ContextBoundObject
{
public:
String^ Value;
};
public ref class Example
{
public:
void Demo()
{
// Determine whether the types can be hosted in a Context.
Console::WriteLine("The IsContextful property for the {0} type is {1}.",
Example::typeid->Name, Example::typeid->IsContextful);
Console::WriteLine("The IsContextful property for the {0} type is {1}.",
ContextBoundClass::typeid->Name, ContextBoundClass::typeid->IsContextful);
// Determine whether the types are marshalled by reference.
Console::WriteLine("The IsMarshalByRef property of {0} is {1}.",
Example::typeid->Name, Example::typeid->IsMarshalByRef );
Console::WriteLine("The IsMarshalByRef property of {0} is {1}.",
ContextBoundClass::typeid->Name, ContextBoundClass::typeid->IsMarshalByRef );
// Determine whether the types are primitive datatypes.
Console::WriteLine("{0} is a primitive data type: {1}.",
int::typeid->Name, int::typeid->IsPrimitive );
Console::WriteLine("{0} is a primitive data type: {1}.",
String::typeid->Name, String::typeid->IsPrimitive );
}
};
int main()
{
Example^ ex = gcnew Example;
ex->Demo();
}
// The example displays the following output:
// The IsContextful property for the Example type is False.
// The IsContextful property for the ContextBoundClass type is True.
// The IsMarshalByRef property of Example is False.
// The IsMarshalByRef property of ContextBoundClass is True.
// Int32 is a primitive data type: True.
// String is a primitive data type: False.
using System;
using System.Runtime.Remoting.Contexts;
public class ContextBoundClass: ContextBoundObject
{
public string Value = "The Value property.";
}
public class Example
{
public static void Main()
{
// Determine whether the types can be hosted in a Context.
Console.WriteLine("The IsContextful property for the {0} type is {1}.",
typeof(Example).Name, typeof(Example).IsContextful);
Console.WriteLine("The IsContextful property for the {0} type is {1}.",
typeof(ContextBoundClass).Name, typeof(ContextBoundClass).IsContextful);
// Determine whether the types are marshalled by reference.
Console.WriteLine("The IsMarshalByRef property of {0} is {1}.",
typeof(Example).Name, typeof(Example).IsMarshalByRef);
Console.WriteLine("The IsMarshalByRef property of {0} is {1}.",
typeof(ContextBoundClass).Name, typeof(ContextBoundClass).IsMarshalByRef);
// Determine whether the types are primitive datatypes.
Console.WriteLine("{0} is a primitive data type: {1}.",
typeof(int).Name, typeof(int).IsPrimitive);
Console.WriteLine("{0} is a primitive data type: {1}.",
typeof(string).Name, typeof(string).IsPrimitive);
}
}
// The example displays the following output:
// The IsContextful property for the Example type is False.
// The IsContextful property for the ContextBoundClass type is True.
// The IsMarshalByRef property of Example is False.
// The IsMarshalByRef property of ContextBoundClass is True.
// Int32 is a primitive data type: True.
// String is a primitive data type: False.
open System
type ContextBoundClass() as this =
inherit ContextBoundObject()
[<DefaultValue>]
val mutable public Value : string
do
this.Value <- "The Value property."
type Example() = class end
// Determine whether the types can be hosted in a Context.
printfn $"The IsContextful property for the {typeof<Example>.Name} type is {typeof<Example>.IsContextful}."
printfn $"The IsContextful property for the {typeof<ContextBoundClass>.Name} type is {typeof<ContextBoundClass>.IsContextful}."
// Determine whether the types are marshalled by reference.
printfn $"The IsMarshalByRef property of {typeof<Example>.Name} is {typeof<Example>.IsMarshalByRef}."
printfn $"The IsMarshalByRef property of {typeof<ContextBoundClass>.Name} is {typeof<ContextBoundClass>.IsMarshalByRef}."
// Determine whether the types are primitive datatypes.
printfn $"{typeof<int>.Name} is a primitive data type: {typeof<int>.IsPrimitive}."
printfn $"{typeof<string>.Name} is a primitive data type: {typeof<string>.IsPrimitive}."
// The example displays the following output:
// The IsContextful property for the Example type is False.
// The IsContextful property for the ContextBoundClass type is True.
// The IsMarshalByRef property of Example is False.
// The IsMarshalByRef property of ContextBoundClass is True.
// Int32 is a primitive data type: True.
// String is a primitive data type: False.
Imports System.Runtime.Remoting.Contexts
Public Class ContextBoundClass : Inherits ContextBoundObject
Public Value As String = "The Value property."
End Class
Public Class Example
Public Shared Sub Main()
' Determine whether the types can be hosted in a Context.
Console.WriteLine("The IsContextful property for the {0} type is {1}.",
GetType(Example).Name, GetType(Example).IsContextful)
Console.WriteLine("The IsContextful property for the {0} type is {1}.",
GetType(ContextBoundClass).Name, GetType(ContextBoundClass).IsContextful)
' Determine whether the types are marshalled by reference.
Console.WriteLine("The IsMarshalByRef property of {0} is {1}.",
GetType(Example).Name, GetType(Example).IsMarshalByRef)
Console.WriteLine("The IsMarshalByRef property of {0} is {1}.",
GetType(ContextBoundClass).Name, GetType(ContextBoundClass).IsMarshalByRef)
' Determine whether the types are primitive datatypes.
Console.WriteLine("{0} is a primitive data type: {1}.",
GetType(Integer).Name, GetType(Integer).IsPrimitive)
Console.WriteLine("{0} is a primitive data type: {1}.",
GetType(String).Name, GetType(String).IsPrimitive)
End Sub
End Class
' The example displays the following output:
' The IsContextful property for the Example type is False.
' The IsContextful property for the ContextBoundClass type is True.
' The IsMarshalByRef property of Example is False.
' The IsMarshalByRef property of ContextBoundClass is True.
' Int32 is a primitive data type: True.
' String is a primitive data type: False.
備註
基本類型為 Boolean 、、、、 Int16UInt16Int32SByteUInt32UIntPtrDoubleInt64UInt64IntPtrChar 和 。 SingleByte
如果目前 Type 代表泛型型別,或泛型型別或泛型方法定義中的型別參數,則此屬性一律會傳 false
回 。