|
Il presente articolo è stato tradotto automaticamente. Passare il puntatore sulle frasi nell'articolo per visualizzare il testo originale. Ulteriori informazioni.
|
Traduzione
Originale
|
Classe PropertyBuilder
System.Reflection.MemberInfo
System.Reflection.PropertyInfo
System.Reflection.Emit.PropertyBuilder
Spazio dei nomi: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Il tipo PropertyBuilder espone i seguenti membri.
| Nome | Descrizione | |
|---|---|---|
![]() | Attributes | |
![]() | CanRead | |
![]() | CanWrite | |
![]() | CustomAttributes | |
![]() | DeclaringType | |
![]() | GetMethod | |
![]() | IsSpecialName | |
![]() | MemberType | |
![]() | MetadataToken | |
![]() | Module | |
![]() | Name | |
![]() | PropertyToken | |
![]() | PropertyType | |
![]() | ReflectedType | |
![]() | SetMethod |
| Nome | Descrizione | |
|---|---|---|
![]() | 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 |
| Nome | Descrizione | |
|---|---|---|
![]() | GetCustomAttribute(Type) | Sottoposto a overload. |
![]() | GetCustomAttribute(Type, Boolean) | Sottoposto a overload. |
![]() | GetCustomAttribute<T>() | Sottoposto a overload. |
![]() | GetCustomAttribute<T>(Boolean) | Sottoposto a overload. |
![]() | GetCustomAttributes() | Sottoposto a overload. |
![]() | GetCustomAttributes(Type) | Sottoposto a overload. |
![]() | GetCustomAttributes<T>() | Sottoposto a overload. |
![]() | GetCustomAttributes<T>(Boolean) | Sottoposto a overload. |
![]() | IsDefined |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() | _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 |
Nota |
|---|
L'attributo HostProtectionAttribute applicato a questo tipo di membro dispone del seguente valore per la proprietà Resources: MayLeakOnAbort. L'oggetto HostProtectionAttribute non influisce sulle applicazioni desktop, che in genere vengono avviate facendo doppio clic sull'icona, digitando un comando oppure immettendo un URL in un browser. Per ulteriori informazioni, vedere la classe HostProtectionAttribute o programmazione per SQL Server e attributi di protezione host. |
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 (ruoli di base del server non supportati), Windows Server 2008 R2 (ruoli di base del server supportati con SP1 o versione successiva, Itanium non supportato)
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.
