PropertyAttributes 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义可以与属性相关联的属性。 这些属性值在 Corhdr.h 中进行定义。
此枚举支持其成员值的按位组合。
public enum class PropertyAttributes
[System.Flags]
public enum PropertyAttributes
[System.Flags]
[System.Serializable]
public enum PropertyAttributes
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum PropertyAttributes
[<System.Flags>]
type PropertyAttributes =
[<System.Flags>]
[<System.Serializable>]
type PropertyAttributes =
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type PropertyAttributes =
Public Enum PropertyAttributes
- 继承
- 属性
字段
HasDefault | 4096 | 指定属性具有默认值。 |
None | 0 | 指定没有与属性关联的特性。 |
Reserved2 | 8192 | 保留。 |
Reserved3 | 16384 | 保留。 |
Reserved4 | 32768 | 保留。 |
ReservedMask | 62464 | 指定一个仅供运行时使用的保留标志。 |
RTSpecialName | 1024 | 指定由元数据内部 API 检查名称编码。 |
SpecialName | 512 | 指定属性是特殊的,并用名称描述属性的特殊性。 |
示例
以下示例生成三个属性并显示 PropertyAttributes 枚举值。 请注意,只读属性没有 setter,因此不能由Caption = statement
更改。
using namespace System;
using namespace System::Reflection;
// Define three properties: one read-write, one default,
// and one read only.
// Define a read-write property.
public ref class Aproperty
{
private:
String^ caption;
public:
Aproperty()
: caption( "A Default caption" )
{}
property String^ Caption
{
String^ get()
{
return caption;
}
void set( String^ value )
{
if ( caption != value )
{
caption = value;
}
}
}
};
// Define a default property.
public ref class Bproperty
{
private:
String^ caption;
public:
Bproperty()
: caption( "B Default caption" )
{}
public:
property String^ Item
{
String^ get()
{
return "1";
}
}
property String^ Caption
{
String^ get()
{
return caption;
}
void set( String^ value )
{
if ( caption != value )
{
caption = value;
}
}
}
};
// Define a read-only property.
public ref class Cproperty
{
private:
String^ caption;
public:
Cproperty()
: caption( "C Default caption" )
{}
property String^ Caption
{
String^ get()
{
return caption;
}
}
};
int main()
{
Console::WriteLine( "\nReflection.PropertyAttributes" );
// Determine whether a property exists, and change its value.
Aproperty^ Mypropertya = gcnew Aproperty;
Bproperty^ Mypropertyb = gcnew Bproperty;
Cproperty^ Mypropertyc = gcnew Cproperty;
Console::Write( "\n1. Mypropertya->Caption = {0}", Mypropertya->Caption );
Console::Write( "\n1. Mypropertyb->Caption = {0}", Mypropertyb->Caption );
Console::Write( "\n1. Mypropertyc->Caption = {0}", Mypropertyc->Caption );
// Only Mypropertya can be changed, as Mypropertyb is read-only.
Mypropertya->Caption = "A- This is changed.";
Mypropertyb->Caption = "B- This is changed.";
// Note that Mypropertyc is not changed because it is read only
Console::Write( "\n\n2. Mypropertya->Caption = {0}", Mypropertya->Caption );
Console::Write( "\n2. Mypropertyb->Caption = {0}", Mypropertyb->Caption );
Console::Write( "\n2. Mypropertyc->Caption = {0}", Mypropertyc->Caption );
// Get the PropertyAttributes enumeration of the property.
// Get the type.
Type^ MyTypea = Type::GetType( "Aproperty" );
Type^ MyTypeb = Type::GetType( "Bproperty" );
Type^ MyTypec = Type::GetType( "Cproperty" );
// Get the property attributes.
PropertyInfo^ Mypropertyinfoa = MyTypea->GetProperty( "Caption" );
PropertyAttributes Myattributesa = Mypropertyinfoa->Attributes;
PropertyInfo^ Mypropertyinfob = MyTypeb->GetProperty( "Item" );
PropertyAttributes Myattributesb = Mypropertyinfob->Attributes;
PropertyInfo^ Mypropertyinfoc = MyTypec->GetProperty( "Caption" );
PropertyAttributes Myattributesc = Mypropertyinfoc->Attributes;
// Display the property attributes value.
Console::Write( "\n\na- {0}", Myattributesa );
Console::Write( "\nb- {0}", Myattributesb );
Console::Write( "\nc- {0}", Myattributesc );
return 0;
}
// This example displays the following output to the console
//
// Reflection.PropertyAttributes
//
// 1. Mypropertya.Caption = A Default caption
// 1. Mypropertyb.Caption = B Default caption
// 1. Mypropertyc.Caption = C Default caption
//
// 2. Mypropertya.Caption = A- This is changed.
// 2. Mypropertyb.Caption = B- This is changed.
// 2. Mypropertyc.Caption = C Default caption
//
// a- None
// b- None
// c- None
using System;
using System.Reflection;
// Define three properties: one read-write, one default,
// and one read only.
public class Aproperty
// Define a read-write property.
{
private string caption = "A Default caption";
public string Caption
{
get{return caption;}
set
{
if (caption != value){caption = value;}
}
}
}
public class Bproperty
// Define a default property.
{
private string caption = "B Default caption";
public string this [int index]
{
get {return "1";}
}
public string Caption
{
get{return caption;}
set
{
if (caption != value){caption = value;}
}
}
}
public class Cproperty
// Define a read-only property.
{
private string caption = "C Default caption";
public string Caption
{
get{return caption;}
// No setting is allowed, because this is a read-only property.
}
}
class propertyattributesenum
{
public static int Main(string[] args)
{
Console.WriteLine("\nReflection.PropertyAttributes");
// Determine whether a property exists, and change its value.
Aproperty Mypropertya = new Aproperty();
Bproperty Mypropertyb = new Bproperty();
Cproperty Mypropertyc = new Cproperty();
Console.Write("\n1. Mypropertya.Caption = " + Mypropertya.Caption );
Console.Write("\n1. Mypropertyb.Caption = " + Mypropertyb.Caption );
Console.Write("\n1. Mypropertyc.Caption = " + Mypropertyc.Caption );
// Only Mypropertya can be changed, as Mypropertyb is read-only.
Mypropertya.Caption = "A- This is changed.";
Mypropertyb.Caption = "B- This is changed.";
// Note that Mypropertyc is not changed because it is read only
Console.Write("\n\n2. Mypropertya.Caption = " + Mypropertya.Caption );
Console.Write("\n2. Mypropertyb.Caption = " + Mypropertyb.Caption );
Console.Write("\n2. Mypropertyc.Caption = " + Mypropertyc.Caption );
// Get the PropertyAttributes enumeration of the property.
// Get the type.
Type MyTypea = Type.GetType("Aproperty");
Type MyTypeb = Type.GetType("Bproperty");
Type MyTypec = Type.GetType("Cproperty");
// Get the property attributes.
PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption");
PropertyAttributes Myattributesa = Mypropertyinfoa.Attributes;
PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Item");
PropertyAttributes Myattributesb = Mypropertyinfob.Attributes;
PropertyInfo Mypropertyinfoc = MyTypec.GetProperty("Caption");
PropertyAttributes Myattributesc = Mypropertyinfoc.Attributes;
// Display the property attributes value.
Console.Write("\n\na- " + Myattributesa.ToString());
Console.Write("\nb- " + Myattributesb.ToString());
Console.Write("\nc- " + Myattributesc.ToString());
return 0;
}
}
// This example displays the following output to the console
//
// Reflection.PropertyAttributes
//
// 1. Mypropertya.Caption = A Default caption
// 1. Mypropertyb.Caption = B Default caption
// 1. Mypropertyc.Caption = C Default caption
//
// 2. Mypropertya.Caption = A- This is changed.
// 2. Mypropertyb.Caption = B- This is changed.
// 2. Mypropertyc.Caption = C Default caption
//
// a- None
// b- None
// c- None
Imports System.Reflection
' Make three properties, one read-write, one default,
' and one read-only.
Public Class Aproperty
' Define a read-write property.
Private myCaption As String = "A Default caption"
Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> value Then
myCaption = value
End If
End Set
End Property
End Class
Public Class Bproperty
' Define a default property.
Private myCaption As String = "B Default caption"
Default Public ReadOnly Property Item(ByVal index As Integer) As String
Get
Return "1"
End Get
End Property
Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> value Then
myCaption = value
End If
End Set
End Property
End Class
Public Class Cproperty
' Define a read-only property.
Private myCaption As String = "C Default caption"
Public ReadOnly Property Caption() As String
Get
Return myCaption
End Get
'No setting is allowed because this property is read-only.
End Property
End Class
Class propertyattributesenum
Public Shared Function Main() As Integer
Console.WriteLine(ControlChars.CrLf & "Reflection.PropertyAttributes")
' Determine whether a property exists, and change its value.
Dim Mypropertya As New Aproperty()
Dim Mypropertyb As New Bproperty()
Dim Mypropertyc As New Cproperty()
Console.Write(ControlChars.CrLf & "1. Mypropertya.Caption = " & _
Mypropertya.Caption)
Console.Write(ControlChars.CrLf & "1. Mypropertyb.Caption = " & _
Mypropertyb.Caption)
Console.Write(ControlChars.CrLf & "1. Mypropertyc.Caption = " & _
Mypropertyc.Caption)
' Only Mypropertya can be changed because Mypropertyb is read-only.
Mypropertya.Caption = "A- This is changed."
Mypropertyb.Caption = "B- This is changed."
' Note that Mypropertyc is not changed, because it is read-only.
Console.Write(ControlChars.CrLf & ControlChars.CrLf & _
"2. Mypropertya.Caption = " & Mypropertya.Caption)
Console.Write(ControlChars.CrLf & "2. Mypropertyb.Caption = " & _
Mypropertyb.Caption)
Console.Write(ControlChars.CrLf + "2. Mypropertyc.Caption = " & _
Mypropertyc.Caption)
' Get the PropertyAttributes Enumeration of the property.
' Get the type.
Dim MyTypea As Type = Type.GetType("Aproperty")
Dim MyTypeb As Type = Type.GetType("Bproperty")
Dim MyTypec As Type = Type.GetType("Cproperty")
' Get the property attributes.
Dim Mypropertyinfoa As PropertyInfo = MyTypea.GetProperty("Caption")
Dim Myattributesa As PropertyAttributes = Mypropertyinfoa.Attributes
Dim Mypropertyinfob As PropertyInfo = MyTypeb.GetProperty("Item")
Dim Myattributesb As PropertyAttributes = Mypropertyinfob.Attributes
Dim Mypropertyinfoc As PropertyInfo = MyTypec.GetProperty("Caption")
Dim Myattributesc As PropertyAttributes = Mypropertyinfoc.Attributes
' Display the property attributes value.
Console.Write(ControlChars.CrLf & ControlChars.CrLf & "a- " & _
Myattributesa.ToString())
Console.Write(ControlChars.CrLf & "b- " & Myattributesb.ToString())
Console.Write(ControlChars.CrLf & "c- " & Myattributesc.ToString())
Return 0
End Function' End Function
End Class
' This example displays the following output to the console
'
' Reflection.PropertyAttributes
'
' 1. Mypropertya.Caption = A Default caption
' 1. Mypropertyb.Caption = B Default caption
' 1. Mypropertyc.Caption = C Default caption
'
' 2. Mypropertya.Caption = A- This is changed.
' 2. Mypropertyb.Caption = B- This is changed.
' 2. Mypropertyc.Caption = C Default caption
'
' a- None
' b- None
' c- None
注解
若要获取 , PropertyAttributes
请先获取 类 Type
。
Type
从 中获取 PropertyInfo
。
PropertyInfo
从 中获取 Attributes
。
枚举值是一个数字,表示在 方法上实现的属性的按位 OR。