PropertyBuilder 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
형식의 속성을 정의합니다.
public ref class PropertyBuilder abstract : System::Reflection::PropertyInfo
public ref class PropertyBuilder sealed : System::Reflection::PropertyInfo
public ref class PropertyBuilder sealed : System::Reflection::PropertyInfo, System::Runtime::InteropServices::_PropertyBuilder
public abstract class PropertyBuilder : System.Reflection.PropertyInfo
public sealed class PropertyBuilder : System.Reflection.PropertyInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class PropertyBuilder : System.Reflection.PropertyInfo, System.Runtime.InteropServices._PropertyBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class PropertyBuilder : System.Reflection.PropertyInfo, System.Runtime.InteropServices._PropertyBuilder
type PropertyBuilder = class
inherit PropertyInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type PropertyBuilder = class
inherit PropertyInfo
interface _PropertyBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type PropertyBuilder = class
inherit PropertyInfo
interface _PropertyBuilder
Public MustInherit Class PropertyBuilder
Inherits PropertyInfo
Public NotInheritable Class PropertyBuilder
Inherits PropertyInfo
Public NotInheritable Class PropertyBuilder
Inherits PropertyInfo
Implements _PropertyBuilder
- 상속
- 특성
- 구현
예제
다음 코드 샘플에서는 속성 프레임워크를 만들기 위해 가져온 PropertyBuilder 속성을 사용하여 TypeBuilder.DefineProperty 동적 형식의 속성을 구현하고 속성 내에서 IL 논리를 구현하는 데 연결된 MethodBuilder 속성을 구현하는 방법을 보여 줍니다.
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
class PropertyBuilderDemo
{
public static Type BuildDynamicTypeWithProperties()
{
AppDomain myDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "MyDynamicAssembly";
// To generate a persistable assembly, specify AssemblyBuilderAccess.RunAndSave.
AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(myAsmName,
AssemblyBuilderAccess.RunAndSave);
// Generate a persistable single-module assembly.
ModuleBuilder myModBuilder =
myAsmBuilder.DefineDynamicModule(myAsmName.Name, myAsmName.Name + ".dll");
TypeBuilder myTypeBuilder = myModBuilder.DefineType("CustomerData",
TypeAttributes.Public);
FieldBuilder customerNameBldr = myTypeBuilder.DefineField("customerName",
typeof(string),
FieldAttributes.Private);
// The last argument of DefineProperty is null, because the
// property has no parameters. (If you don't specify null, you must
// specify an array of Type objects. For a parameterless property,
// use an array with no elements: new Type[] {})
PropertyBuilder custNamePropBldr = myTypeBuilder.DefineProperty("CustomerName",
PropertyAttributes.HasDefault,
typeof(string),
null);
// The property set and property get methods require a special
// set of attributes.
MethodAttributes getSetAttr =
MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig;
// Define the "get" accessor method for CustomerName.
MethodBuilder custNameGetPropMthdBldr =
myTypeBuilder.DefineMethod("get_CustomerName",
getSetAttr,
typeof(string),
Type.EmptyTypes);
ILGenerator custNameGetIL = custNameGetPropMthdBldr.GetILGenerator();
custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr);
custNameGetIL.Emit(OpCodes.Ret);
// Define the "set" accessor method for CustomerName.
MethodBuilder custNameSetPropMthdBldr =
myTypeBuilder.DefineMethod("set_CustomerName",
getSetAttr,
null,
new Type[] { typeof(string) });
ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator();
custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr);
custNameSetIL.Emit(OpCodes.Ret);
// Last, we must map the two methods created above to our PropertyBuilder to
// their corresponding behaviors, "get" and "set" respectively.
custNamePropBldr.SetGetMethod(custNameGetPropMthdBldr);
custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr);
Type retval = myTypeBuilder.CreateType();
// Save the assembly so it can be examined with Ildasm.exe,
// or referenced by a test program.
myAsmBuilder.Save(myAsmName.Name + ".dll");
return retval;
}
public static void Main()
{
Type custDataType = BuildDynamicTypeWithProperties();
PropertyInfo[] custDataPropInfo = custDataType.GetProperties();
foreach (PropertyInfo pInfo in custDataPropInfo) {
Console.WriteLine("Property '{0}' created!", pInfo.ToString());
}
Console.WriteLine("---");
// Note that when invoking a property, you need to use the proper BindingFlags -
// BindingFlags.SetProperty when you invoke the "set" behavior, and
// BindingFlags.GetProperty when you invoke the "get" behavior. Also note that
// we invoke them based on the name we gave the property, as expected, and not
// the name of the methods we bound to the specific property behaviors.
object custData = Activator.CreateInstance(custDataType);
custDataType.InvokeMember("CustomerName", BindingFlags.SetProperty,
null, custData, new object[]{ "Joe User" });
Console.WriteLine("The customerName field of instance custData has been set to '{0}'.",
custDataType.InvokeMember("CustomerName", BindingFlags.GetProperty,
null, custData, new object[]{ }));
}
}
// --- O U T P U T ---
// The output should be as follows:
// -------------------
// Property 'System.String CustomerName' created!
// ---
// The customerName field of instance custData has been set to 'Joe User'.
// -------------------
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Class PropertyBuilderDemo
Public Shared Function BuildDynamicTypeWithProperties() As Type
Dim myDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyDynamicAssembly"
' To generate a persistable assembly, specify AssemblyBuilderAccess.RunAndSave.
Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _
AssemblyBuilderAccess.RunAndSave)
' Generate a persistable, single-module assembly.
Dim myModBuilder As ModuleBuilder = _
myAsmBuilder.DefineDynamicModule(myAsmName.Name, myAsmName.Name & ".dll")
Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("CustomerData", TypeAttributes.Public)
' Define a private field to hold the property value.
Dim customerNameBldr As FieldBuilder = myTypeBuilder.DefineField("customerName", _
GetType(String), FieldAttributes.Private)
' The last argument of DefineProperty is Nothing, because the
' property has no parameters. (If you don't specify Nothing, you must
' specify an array of Type objects. For a parameterless property,
' use an array with no elements: New Type() {})
Dim custNamePropBldr As PropertyBuilder = _
myTypeBuilder.DefineProperty("CustomerName", _
PropertyAttributes.HasDefault, _
GetType(String), _
Nothing)
' The property set and property get methods require a special
' set of attributes.
Dim getSetAttr As MethodAttributes = _
MethodAttributes.Public Or MethodAttributes.SpecialName _
Or MethodAttributes.HideBySig
' Define the "get" accessor method for CustomerName.
Dim custNameGetPropMthdBldr As MethodBuilder = _
myTypeBuilder.DefineMethod("GetCustomerName", _
getSetAttr, _
GetType(String), _
Type.EmptyTypes)
Dim custNameGetIL As ILGenerator = custNameGetPropMthdBldr.GetILGenerator()
custNameGetIL.Emit(OpCodes.Ldarg_0)
custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr)
custNameGetIL.Emit(OpCodes.Ret)
' Define the "set" accessor method for CustomerName.
Dim custNameSetPropMthdBldr As MethodBuilder = _
myTypeBuilder.DefineMethod("get_CustomerName", _
getSetAttr, _
Nothing, _
New Type() {GetType(String)})
Dim custNameSetIL As ILGenerator = custNameSetPropMthdBldr.GetILGenerator()
custNameSetIL.Emit(OpCodes.Ldarg_0)
custNameSetIL.Emit(OpCodes.Ldarg_1)
custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr)
custNameSetIL.Emit(OpCodes.Ret)
' Last, we must map the two methods created above to our PropertyBuilder to
' their corresponding behaviors, "get" and "set" respectively.
custNamePropBldr.SetGetMethod(custNameGetPropMthdBldr)
custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr)
Dim retval As Type = myTypeBuilder.CreateType()
' Save the assembly so it can be examined with Ildasm.exe,
' or referenced by a test program.
myAsmBuilder.Save(myAsmName.Name & ".dll")
return retval
End Function 'BuildDynamicTypeWithProperties
Public Shared Sub Main()
Dim custDataType As Type = BuildDynamicTypeWithProperties()
Dim custDataPropInfo As PropertyInfo() = custDataType.GetProperties()
Dim pInfo As PropertyInfo
For Each pInfo In custDataPropInfo
Console.WriteLine("Property '{0}' created!", pInfo.ToString())
Next pInfo
Console.WriteLine("---")
' Note that when invoking a property, you need to use the proper BindingFlags -
' BindingFlags.SetProperty when you invoke the "set" behavior, and
' BindingFlags.GetProperty when you invoke the "get" behavior. Also note that
' we invoke them based on the name we gave the property, as expected, and not
' the name of the methods we bound to the specific property behaviors.
Dim custData As Object = Activator.CreateInstance(custDataType)
custDataType.InvokeMember("CustomerName", BindingFlags.SetProperty, Nothing, _
custData, New Object() {"Joe User"})
Console.WriteLine("The customerName field of instance custData has been set to '{0}'.", _
custDataType.InvokeMember("CustomerName", BindingFlags.GetProperty, _
Nothing, custData, New Object() {}))
End Sub
End Class
' --- O U T P U T ---
' The output should be as follows:
' -------------------
' Property 'System.String CustomerName' created!
' ---
' The customerName field of instance custData has been set to 'Joe User'.
' -------------------
설명
A PropertyBuilder 는 항상 .와 TypeBuilder연결됩니다.
TypeBuilder.
DefineProperty 메서드는 클라이언트에 새 PropertyBuilder 메서드를 반환합니다.
생성자
| Name | Description |
|---|---|
| PropertyBuilder() |
PropertyBuilder 클래스의 새 인스턴스를 초기화합니다. |
속성
| Name | Description |
|---|---|
| Attributes |
이 속성의 특성을 가져옵니다. |
| CanRead |
속성을 읽을 수 있는지 여부를 나타내는 값을 가져옵니다. |
| CanWrite |
속성을 쓸 수 있는지 여부를 나타내는 값을 가져옵니다. |
| CustomAttributes |
이 멤버의 사용자 지정 특성을 포함하는 컬렉션을 가져옵니다. (다음에서 상속됨 MemberInfo) |
| DeclaringType |
이 멤버를 선언하는 클래스를 가져옵니다. |
| GetMethod |
이 속성의 접근자를 |
| IsCollectible |
이 MemberInfo 개체가 수집 가능한 AssemblyLoadContext어셈블리에 저장된 하나 이상의 어셈블리를 참조하는지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MemberInfo) |
| IsSpecialName |
속성이 특수 이름인지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 PropertyInfo) |
| MemberType |
이 멤버가 MemberTypes 속성임을 나타내는 값을 가져옵니다. (다음에서 상속됨 PropertyInfo) |
| MetadataToken |
메타데이터 요소를 식별하는 값을 가져옵니다. (다음에서 상속됨 MemberInfo) |
| Module |
현재 속성을 선언하는 형식이 정의되는 모듈을 가져옵니다. |
| Module |
현재 MemberInfo 가 나타내는 멤버를 선언하는 형식이 정의된 모듈을 가져옵니다. (다음에서 상속됨 MemberInfo) |
| Name |
이 멤버의 이름을 가져옵니다. |
| PropertyToken |
이 속성에 대한 토큰을 검색합니다. |
| PropertyType |
이 속성의 필드 형식을 가져옵니다. |
| ReflectedType |
이 인스턴스를 가져오는 데 사용된 클래스 개체를 |
| SetMethod |
이 속성의 접근자를 |
메서드
| Name | Description |
|---|---|
| AddOtherMethod(MethodBuilder) |
이 속성과 연결된 다른 메서드 중 하나를 추가합니다. |
| AddOtherMethodCore(MethodBuilder) |
파생 클래스에서 재정의되는 경우 이 속성과 연결된 다른 메서드 중 하나를 추가합니다. |
| Equals(Object) |
이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다. (다음에서 상속됨 PropertyInfo) |
| GetAccessors() |
요소가 현재 인스턴스에 의해 반영된 속성의 public |
| GetAccessors(Boolean) |
이 속성에 대한 public 및 non-public |
| GetAccessors(Boolean) |
요소가 public을 반영하고 지정된 경우 현재 인스턴스에 의해 반영된 속성의 public이 아닌 |
| GetConstantValue() |
컴파일러에서 속성과 연결된 리터럴 값을 반환합니다. (다음에서 상속됨 PropertyInfo) |
| GetCustomAttributes(Boolean) |
이 속성에 대한 모든 사용자 지정 특성의 배열을 반환합니다. |
| GetCustomAttributes(Type, Boolean) |
로 식별되는 사용자 지정 특성의 배열을 Type반환합니다. |
| GetCustomAttributesData() |
대상 멤버에 적용된 특성에 대한 데이터를 나타내는 개체 목록을 CustomAttributeData 반환합니다. (다음에서 상속됨 MemberInfo) |
| GetGetMethod() |
이 속성의 public |
| GetGetMethod(Boolean) |
이 속성에 대한 public 및 non-public get 접근자를 반환합니다. |
| GetGetMethod(Boolean) |
파생 클래스에서 재정의되는 경우 이 속성에 대한 public 또는 non-public |
| GetHashCode() |
이 인스턴스의 해시 코드를 반환합니다. (다음에서 상속됨 PropertyInfo) |
| GetIndexParameters() |
속성에 대한 모든 인덱스 매개 변수의 배열을 반환합니다. |
| GetModifiedPropertyType() |
이 속성 개체의 수정된 형식을 가져옵니다. (다음에서 상속됨 PropertyInfo) |
| GetOptionalCustomModifiers() |
속성의 선택적 사용자 지정 한정자를 나타내는 형식 배열을 반환합니다. (다음에서 상속됨 PropertyInfo) |
| GetRawConstantValue() |
컴파일러에서 속성과 연결된 리터럴 값을 반환합니다. (다음에서 상속됨 PropertyInfo) |
| GetRequiredCustomModifiers() |
속성의 필수 사용자 지정 한정자를 나타내는 형식의 배열을 반환합니다. (다음에서 상속됨 PropertyInfo) |
| GetSetMethod() |
이 속성의 public |
| GetSetMethod(Boolean) |
이 속성의 set 접근자를 반환합니다. |
| GetSetMethod(Boolean) |
파생 클래스에서 재정의되는 경우 이 속성의 접근자를 |
| GetType() |
속성의 특성을 검색하고 속성 메타데이터에 대한 액세스를 제공합니다. (다음에서 상속됨 PropertyInfo) |
| GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) |
지정된 바인딩, 인덱스 및 |
| GetValue(Object, Object[]) |
속성의 getter 메서드를 호출하여 인덱싱된 속성의 값을 가져옵니다. |
| GetValue(Object) |
지정된 개체의 속성 값을 반환합니다. (다음에서 상속됨 PropertyInfo) |
| HasSameMetadataDefinitionAs(MemberInfo) |
형식의 속성을 정의합니다. (다음에서 상속됨 MemberInfo) |
| IsDefined(Type, Boolean) |
이 속성에 하나 이상의 인스턴스 |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| SetConstant(Object) |
이 속성의 기본값을 설정합니다. |
| SetConstantCore(Object) |
파생 클래스에서 재정의되는 경우 이 속성의 기본값을 설정합니다. |
| SetCustomAttribute(ConstructorInfo, Byte[]) |
지정된 사용자 지정 특성 Blob을 사용하여 사용자 지정 특성을 설정합니다. |
| SetCustomAttribute(CustomAttributeBuilder) |
사용자 지정 특성 작성기를 사용하여 사용자 지정 특성을 설정합니다. |
| SetCustomAttributeCore(ConstructorInfo, ReadOnlySpan<Byte>) |
파생 클래스에서 재정의되는 경우 이 어셈블리에서 사용자 지정 특성을 설정합니다. |
| SetGetMethod(MethodBuilder) |
속성 값을 가져오는 메서드를 설정합니다. |
| SetGetMethodCore(MethodBuilder) |
파생 클래스에서 재정의되는 경우 속성 값을 가져오는 메서드를 설정합니다. |
| SetSetMethod(MethodBuilder) |
속성 값을 설정하는 메서드를 설정합니다. |
| SetSetMethodCore(MethodBuilder) |
파생 클래스에서 재정의되는 경우 속성 값을 설정하는 메서드를 설정합니다. |
| SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo) |
지정된 개체의 속성 값을 지정된 값으로 설정합니다. |
| SetValue(Object, Object, Object[]) |
인덱스 속성에 대한 선택적 인덱스 값을 사용하여 속성 값을 설정합니다. |
| SetValue(Object, Object) |
지정된 개체의 속성 값을 설정합니다. (다음에서 상속됨 PropertyInfo) |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
명시적 인터페이스 구현
확장명 메서드
| Name | Description |
|---|---|
| GetAccessors(PropertyInfo, Boolean) |
형식의 속성을 정의합니다. |
| GetAccessors(PropertyInfo) |
형식의 속성을 정의합니다. |
| GetCustomAttribute(MemberInfo, Type, Boolean) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttribute(MemberInfo, Type) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성을 검색합니다. |
| GetCustomAttribute<T>(MemberInfo, Boolean) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttribute<T>(MemberInfo) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성을 검색합니다. |
| GetCustomAttributes(MemberInfo, Boolean) |
지정된 멤버에 적용되는 사용자 지정 특성의 컬렉션을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttributes(MemberInfo, Type, Boolean) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성 컬렉션을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttributes(MemberInfo, Type) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성 컬렉션을 검색합니다. |
| GetCustomAttributes(MemberInfo) |
지정된 멤버에 적용되는 사용자 지정 특성의 컬렉션을 검색합니다. |
| GetCustomAttributes<T>(MemberInfo, Boolean) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성 컬렉션을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttributes<T>(MemberInfo) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성 컬렉션을 검색합니다. |
| GetGetMethod(PropertyInfo, Boolean) |
형식의 속성을 정의합니다. |
| GetGetMethod(PropertyInfo) |
형식의 속성을 정의합니다. |
| GetMetadataToken(MemberInfo) |
사용 가능한 경우 지정된 멤버에 대한 메타데이터 토큰을 가져옵니다. |
| GetSetMethod(PropertyInfo, Boolean) |
형식의 속성을 정의합니다. |
| GetSetMethod(PropertyInfo) |
형식의 속성을 정의합니다. |
| HasMetadataToken(MemberInfo) |
지정된 멤버에 메타데이터 토큰을 사용할 수 있는지 여부를 나타내는 값을 반환합니다. |
| IsDefined(MemberInfo, Type, Boolean) |
지정된 형식의 사용자 지정 특성이 지정된 멤버에 적용되는지 여부와 필요에 따라 상위 항목에 적용되는지 여부를 나타냅니다. |
| IsDefined(MemberInfo, Type) |
지정된 형식의 사용자 지정 특성이 지정된 멤버에 적용되는지 여부를 나타냅니다. |