|
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
|
Übersetzung
Original
|
PropertyBuilder-Klasse
System.Reflection.MemberInfo
System.Reflection.PropertyInfo
System.Reflection.Emit.PropertyBuilder
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Der PropertyBuilder-Typ macht die folgenden Member verfügbar.
| Name | Beschreibung | |
|---|---|---|
![]() | Attributes | |
![]() | CanRead | |
![]() | CanWrite | |
![]() | CustomAttributes | |
![]() | DeclaringType | |
![]() | GetMethod | |
![]() | IsSpecialName | |
![]() | MemberType | |
![]() | MetadataToken | |
![]() | Module | |
![]() | Name | |
![]() | PropertyToken | |
![]() | PropertyType | |
![]() | ReflectedType | |
![]() | SetMethod |
| Name | Beschreibung | |
|---|---|---|
![]() | AddOtherMethod | |
![]() | Equals | |
![]() | GetAccessors() | |
![]() | GetAccessors(Boolean) | |
![]() | GetConstantValue | |
![]() | GetCustomAttributes(Boolean) | |
![]() | GetCustomAttributes(Type, Boolean) | |
![]() | GetCustomAttributesData | |
![]() | GetGetMethod() | |
![]() | GetGetMethod(Boolean) | |
![]() | GetHashCode | |
![]() | GetIndexParameters | |
![]() | GetOptionalCustomModifiers | |
![]() | GetRawConstantValue | |
![]() | GetRequiredCustomModifiers | |
![]() | GetSetMethod() | |
![]() | GetSetMethod(Boolean) | |
![]() | GetType | |
![]() | GetValue(Object) | |
![]() | GetValue(Object, Object[]) | |
![]() | GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) | |
![]() | IsDefined | |
![]() | SetConstant | |
![]() | SetCustomAttribute(CustomAttributeBuilder) | |
![]() | SetCustomAttribute(ConstructorInfo, Byte[]) | |
![]() | SetGetMethod | |
![]() | SetSetMethod | |
![]() | SetValue(Object, Object) | |
![]() | SetValue(Object, Object, Object[]) | |
![]() | SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo) | |
![]() | ToString |
| Name | Beschreibung | |
|---|---|---|
![]() | GetCustomAttribute(Type) | Überladen. |
![]() | GetCustomAttribute(Type, Boolean) | Überladen. |
![]() | GetCustomAttribute<T>() | Überladen. |
![]() | GetCustomAttribute<T>(Boolean) | Überladen. |
![]() | GetCustomAttributes() | Überladen. |
![]() | GetCustomAttributes(Type) | Überladen. |
![]() | GetCustomAttributes<T>() | Überladen. |
![]() | GetCustomAttributes<T>(Boolean) | Überladen. |
![]() | IsDefined |
| Name | Beschreibung | |
|---|---|---|
![]() ![]() | _MemberInfo.GetIDsOfNames | |
![]() ![]() | _MemberInfo.GetType | |
![]() ![]() | _MemberInfo.GetTypeInfo | |
![]() ![]() | _MemberInfo.GetTypeInfoCount | |
![]() ![]() | _MemberInfo.Invoke | |
![]() ![]() | _PropertyBuilder.GetIDsOfNames | |
![]() ![]() | _PropertyBuilder.GetTypeInfo | |
![]() ![]() | _PropertyBuilder.GetTypeInfoCount | |
![]() ![]() | _PropertyBuilder.Invoke | |
![]() ![]() | _PropertyInfo.GetIDsOfNames | |
![]() ![]() | _PropertyInfo.GetType | |
![]() ![]() | _PropertyInfo.GetTypeInfo | |
![]() ![]() | _PropertyInfo.GetTypeInfoCount | |
![]() ![]() | _PropertyInfo.Invoke |
Hinweis |
|---|
Das auf diesen Typ oder Member angewendete HostProtectionAttribute-Attribut besitzt den folgenden Resources-Eigenschaftswert: MayLeakOnAbort. Das HostProtectionAttribute hat keine Auswirkungen auf Desktopanwendungen (die normalerweise durch Doppelklicken auf ein Symbol, Eingeben eines Befehls oder einer URL in einem Browser gestartet werden). Weitere Informationen finden Sie unter der HostProtectionAttribute-Klasse oder unter SQL Server-Programmierung und Hostschutzattribute. |
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 [System.String]' created! // --- // The customerName field of instance custData has been set to 'Joe User'. // -------------------
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core-Rolle wird nicht unterstützt), Windows Server 2008 R2 (Server Core-Rolle wird mit SP1 oder höher unterstützt; Itanium wird nicht unterstützt)
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.
