Type.GetProperty Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene una proprietà specifica dell'oggetto corrente Type.
Overload
| Nome | Descrizione |
|---|---|
| GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) |
Cerca la proprietà specificata i cui parametri corrispondono ai tipi di argomento e ai modificatori specificati, utilizzando i vincoli di associazione specificati. |
| GetProperty(String) |
Cerca la proprietà pubblica con il nome specificato. |
| GetProperty(String, BindingFlags) |
Cerca la proprietà specificata utilizzando i vincoli di associazione specificati. |
| GetProperty(String, Type) |
Cerca la proprietà pubblica con il nome e il tipo restituito specificati. |
| GetProperty(String, Type[]) |
Cerca la proprietà pubblica specificata i cui parametri corrispondono ai tipi di argomento specificati. |
| GetProperty(String, Type, Type[]) |
Cerca la proprietà pubblica specificata i cui parametri corrispondono ai tipi di argomento specificati. |
| GetProperty(String, Type, Type[], ParameterModifier[]) |
Cerca la proprietà pubblica specificata i cui parametri corrispondono ai tipi di argomento e ai modificatori specificati. |
GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
Cerca la proprietà specificata i cui parametri corrispondono ai tipi di argomento e ai modificatori specificati, utilizzando i vincoli di associazione specificati.
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.PropertyInfo GetProperty(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
abstract member GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags, binder As Binder, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo
Parametri
- name
- String
Stringa contenente il nome della proprietà da ottenere.
- bindingAttr
- BindingFlags
Combinazione bit per bit dei valori di enumerazione che specificano come viene eseguita la ricerca.
oppure
Default per restituire null.
- binder
- Binder
Oggetto che definisce un set di proprietà e abilita l'associazione, che può comportare la selezione di un metodo di overload, la coercizione dei tipi di argomento e la chiamata di un membro tramite reflection.
oppure
Riferimento Null (Nothing in Visual Basic), per usare il DefaultBinder.
- returnType
- Type
Tipo restituito della proprietà.
- types
- Type[]
Matrice di Type oggetti che rappresentano il numero, l'ordine e il tipo dei parametri per la proprietà indicizzata da ottenere.
oppure
Matrice vuota del tipo Type ,ovvero Type[] types = new Type[0]) per ottenere una proprietà non indicizzata.
- modifiers
- ParameterModifier[]
Matrice di ParameterModifier oggetti che rappresentano gli attributi associati all'elemento corrispondente nella types matrice. Il gestore di associazione predefinito non elabora questo parametro.
Valori restituiti
Oggetto che rappresenta la proprietà che soddisfa i requisiti specificati, se trovato; in caso contrario, null.
Implementazioni
- Attributi
Eccezioni
Vengono trovate più proprietà con il nome specificato e corrispondenti ai vincoli di associazione specificati.
types è multidimensionale.
oppure
modifiers è multidimensionale.
oppure
types e modifiers non hanno la stessa lunghezza.
Un elemento di types è null.
Commenti
Per altre informazioni su questa API, vedere Osservazioni supplementari sull'API per Type.GetProperty.
Vedi anche
- PropertyInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
Si applica a
GetProperty(String)
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
Cerca la proprietà pubblica con il nome specificato.
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name);
public System.Reflection.PropertyInfo GetProperty(string name);
public System.Reflection.PropertyInfo? GetProperty(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string -> System.Reflection.PropertyInfo
member this.GetProperty : string -> System.Reflection.PropertyInfo
abstract member GetProperty : string -> System.Reflection.PropertyInfo
override this.GetProperty : string -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String) As PropertyInfo
Parametri
- name
- String
Stringa contenente il nome della proprietà pubblica da ottenere.
Valori restituiti
Oggetto che rappresenta la proprietà pubblica con il nome specificato, se trovato; in caso contrario, null.
Implementazioni
- Attributi
Eccezioni
Vengono trovate più proprietà con il nome specificato.
name è null.
Esempio
Nell'esempio seguente viene recuperato l'oggetto Type di una classe definita dall'utente, viene recuperata una proprietà di tale classe e viene visualizzato il nome della proprietà.
using System;
using System.Reflection;
class MyClass1
{
// Declare MyProperty.
public int MyProperty { get; set; }
}
public class MyTypeClass1
{
public static void Main(string[] args)
{
try
{
// Get the Type object corresponding to MyClass1.
Type myType = typeof(MyClass1);
// Get the PropertyInfo object by passing the property name.
PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
// Display the property name.
Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.Name);
}
catch (NullReferenceException e)
{
Console.WriteLine("The property does not exist in MyClass1." + e.Message);
}
}
}
open System
type MyClass() =
let mutable myProperty = 0
// Declare MyProperty.
member _.MyProperty
with get () =
myProperty
and set (value) =
myProperty <- value
try
// Get the Type object corresponding to MyClass.
let myType = typeof<MyClass>
// Get the PropertyInfo object by passing the property name.
let myPropInfo = myType.GetProperty "MyProperty"
// Display the property name.
printfn $"The {myPropInfo.Name} property exists in MyClass."
with :? NullReferenceException as e ->
printfn $"The property does not exist in MyClass.{e.Message}"
Imports System.Reflection
Class MyClass1
Private myProperty1 As Integer
' Declare MyProperty.
Public Property MyProperty() As Integer
Get
Return MyProperty1
End Get
Set(ByVal Value As Integer)
MyProperty1 = Value
End Set
End Property
End Class
Public Class MyTypeClass
Public Shared Sub Run(ByVal args() As String)
Try
' Get Type Object corresponding to MyClass.
Dim myType As Type = GetType(MyClass1)
' Get PropertyInfo object by passing property name.
Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty")
' Display Name property to console.
Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name)
Catch e As NullReferenceException
Console.WriteLine("The property does not exist in MyClass.", e.Message.ToString())
End Try
End Sub
End Class
Commenti
Per altre informazioni su questa API, vedere Osservazioni supplementari sull'API per Type.GetProperty.
Vedi anche
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
Si applica a
GetProperty(String, BindingFlags)
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
Cerca la proprietà specificata utilizzando i vincoli di associazione specificati.
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.PropertyInfo GetProperty(string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
abstract member GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
abstract member GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags) As PropertyInfo
Parametri
- name
- String
Stringa contenente il nome della proprietà da ottenere.
- bindingAttr
- BindingFlags
Combinazione bit per bit dei valori di enumerazione che specificano come viene eseguita la ricerca.
oppure
Default per restituire null.
Valori restituiti
Oggetto che rappresenta la proprietà che soddisfa i requisiti specificati, se trovato; in caso contrario, null.
Implementazioni
- Attributi
Eccezioni
Vengono trovate più proprietà con il nome specificato e corrispondenti ai vincoli di associazione specificati.
name è null.
Esempio
Nell'esempio seguente viene recuperato il tipo di una classe definita dall'utente, viene recuperata una proprietà di tale classe e viene visualizzato il nome della proprietà in base ai vincoli di associazione specificati.
using System;
using System.Reflection;
class MyClass2
{
// Declare MyProperty.
public int MyProperty { get; set; }
}
public class MyTypeClass2
{
public static void Main(string[] args)
{
try
{
// Get Type object of MyClass2.
Type myType = typeof(MyClass2);
// Get the PropertyInfo by passing the property name and specifying the BindingFlags.
PropertyInfo myPropInfo = myType.GetProperty(
"MyProperty",
BindingFlags.Public | BindingFlags.Instance
);
// Display Name property to console.
Console.WriteLine("{0} is a property of MyClass2.", myPropInfo.Name);
}
catch (NullReferenceException e)
{
Console.WriteLine("MyProperty does not exist in MyClass2." + e.Message);
}
}
}
open System
open System.Reflection
type MyClass() =
let mutable myProperty = 0
// Declare MyProperty.
member _.MyProperty
with get () =
myProperty
and set (value) =
myProperty <- value
try
// Get Type object of MyClass.
let myType = typeof<MyClass>
// Get the PropertyInfo by passing the property name and specifying the BindingFlags.
let myPropInfo = myType.GetProperty("MyProperty", BindingFlags.Public ||| BindingFlags.Instance)
// Display Name property to console.
printfn $"{myPropInfo.Name} is a property of MyClass."
with :? NullReferenceException as e ->
printfn $"MyProperty does not exist in MyClass.{e.Message}"
Imports System.Reflection
Module Module1
Public Class MyClass1
Private myProperty1 As Integer
Public Property MyProperty() As Integer
Get
Return myProperty1
End Get
Set(ByVal Value As Integer)
myProperty1 = Value
End Set
End Property
Public Shared Sub Run()
Try
' Get a Type object corresponding to MyClass.
Dim myType As Type = GetType(MyClass1)
' Get a PropertyInfo object by passing property name and specifying BindingFlags.
Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty", BindingFlags.Public Or BindingFlags.Instance)
' Display the Name property.
Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name)
Catch e As NullReferenceException
Console.WriteLine("MyProperty does not exist in MyClass.", e.Message.ToString())
End Try
End Sub
End Class
End Module 'Module1
Vedi anche
- PropertyInfo
- String
- BindingFlags
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
Si applica a
GetProperty(String, Type)
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
Cerca la proprietà pubblica con il nome e il tipo restituito specificati.
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType);
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType);
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string * Type -> System.Reflection.PropertyInfo
member this.GetProperty : string * Type -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type) As PropertyInfo
Parametri
- name
- String
Stringa contenente il nome della proprietà pubblica da ottenere.
- returnType
- Type
Tipo restituito della proprietà.
Valori restituiti
Oggetto che rappresenta la proprietà pubblica con il nome specificato, se trovato; in caso contrario, null.
Implementazioni
- Attributi
Eccezioni
Vengono trovate più proprietà con il nome specificato.
name è nullo returnType è null.
Esempio
L'esempio seguente definisce una classe con una proprietà e recupera il nome e il tipo della proprietà.
using System;
using System.Reflection;
class MyPropertyTypeClass
{
public string MyProperty1 { get; set; } = "Hello World.";
}
class TestClass
{
static void Main()
{
try
{
Type myType = typeof(MyPropertyTypeClass);
// Get the PropertyInfo object representing MyProperty1.
PropertyInfo myStringProperties1 = myType.GetProperty("MyProperty1", typeof(string));
Console.WriteLine("The name of the first property of MyPropertyTypeClass is {0}.",
myStringProperties1.Name);
Console.WriteLine("The type of the first property of MyPropertyTypeClass is {0}.",
myStringProperties1.PropertyType);
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException :" + e.Message);
}
catch (AmbiguousMatchException e)
{
Console.WriteLine("AmbiguousMatchException :" + e.Message);
}
catch (NullReferenceException e)
{
Console.WriteLine("Source : {0}", e.Source);
Console.WriteLine("Message : {0}", e.Message);
}
//Output:
//The name of the first property of MyPropertyTypeClass is MyProperty1.
//The type of the first property of MyPropertyTypeClass is System.String.
}
}
open System
open System.Reflection
type MyClass1() =
let mutable myMessage = "Hello World."
member _.MyProperty1
with get () =
myMessage
and set (value) =
myMessage <- value
try
let myType = typeof<MyClass1>
// Get the PropertyInfo object representing MyProperty1.
let myStringProperties1 = myType.GetProperty("MyProperty1", typeof<string>)
printfn $"The name of the first property of MyClass1 is {myStringProperties1.Name}."
printfn $"The type of the first property of MyClass1 is {myStringProperties1.PropertyType}."
with
| :? ArgumentNullException as e ->
printfn $"ArgumentNullException :{e.Message}"
| :? AmbiguousMatchException as e ->
printfn $"AmbiguousMatchException :{e.Message}"
| :? NullReferenceException as e ->
printfn $"Source : {e.Source}"
printfn $"Message : {e.Message}"
// Output:
// The name of the first property of MyClass1 is MyProperty1.
// The type of the first property of MyClass1 is System.String.
Imports System.Reflection
Class MyTypesClass
Private myMessage As [String] = "Hello World."
Public Property MyProperty1() As String
Get
Return myMessage
End Get
Set(ByVal Value As String)
myMessage = Value
End Set
End Property
End Class
Class TestClass
Shared Sub Run()
Try
Dim myType As Type = GetType(MyTypesClass)
' Get the PropertyInfo object representing MyProperty1.
Dim myStringProperties1 As PropertyInfo = myType.GetProperty("MyProperty1", GetType(String))
Console.WriteLine("The name of the first property of MyTypesClass is {0}.", myStringProperties1.Name)
Console.WriteLine("The type of the first property of MyTypesClass is {0}.", myStringProperties1.PropertyType.ToString())
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException :" + e.Message.ToString())
Catch e As AmbiguousMatchException
Console.WriteLine("AmbiguousMatchException :" + e.Message.ToString())
Catch e As NullReferenceException
Console.WriteLine("Source : {0}", e.Source.ToString())
Console.WriteLine("Message : {0}", e.Message.ToString())
End Try
'Output:
'The name of the first property of MyTypesClass is MyProperty1.
'The type of the first property of MyTypesClass is System.String.
End Sub
End Class
Commenti
Una proprietà viene considerata pubblica ai fini della riflessione se ha almeno un accessore pubblico. In caso contrario, la proprietà è considerata privata ed è necessario usare BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic combinare i valori usando Or) per ottenerla.
La ricerca name fa distinzione tra maiuscole e minuscole. La ricerca include le proprietà pubbliche statiche e di istanza.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce PropertyInfo con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo generico o di un metodo generico, questo metodo cerca le proprietà del vincolo di classe.
Vedi anche
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
Si applica a
GetProperty(String, Type[])
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
Cerca la proprietà pubblica specificata i cui parametri corrispondono ai tipi di argomento specificati.
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, Type[] types);
public System.Reflection.PropertyInfo GetProperty(string name, Type[] types);
public System.Reflection.PropertyInfo? GetProperty(string name, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
member this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, types As Type()) As PropertyInfo
Parametri
- name
- String
Stringa contenente il nome della proprietà pubblica da ottenere.
- types
- Type[]
Matrice di Type oggetti che rappresentano il numero, l'ordine e il tipo dei parametri per la proprietà indicizzata da ottenere.
oppure
Matrice vuota del tipo Type ,ovvero Type[] types = new Type[0]) per ottenere una proprietà non indicizzata.
Valori restituiti
Oggetto che rappresenta la proprietà pubblica i cui parametri corrispondono ai tipi di argomento specificati, se trovato; in caso contrario, null.
Implementazioni
- Attributi
Eccezioni
Vengono trovate più proprietà con il nome specificato e corrispondenti ai tipi di argomento specificati.
types è multidimensionale.
Un elemento di types è null.
Esempio
Nell'esempio seguente viene recuperato l'oggetto Type di una classe definita dall'utente, viene recuperata la proprietà della classe e viene visualizzato il nome della proprietà e il tipo della proprietà come specificato dagli argomenti passati a GetProperty.
using System;
using System.Reflection;
class MyClass3
{
private readonly int[,] _myArray = { { 1, 2 }, { 3, 4 } };
// Declare an indexer.
public int this[int i, int j]
{
get
{
return _myArray[i, j];
}
set
{
_myArray[i, j] = value;
}
}
}
public class MyTypeClass3
{
public static void Main(string[] args)
{
try
{
// Get the Type object.
Type myType = typeof(MyClass3);
Type[] myTypeArr = new Type[2];
// Create an instance of a Type array.
myTypeArr.SetValue(typeof(int), 0);
myTypeArr.SetValue(typeof(int), 1);
// Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
PropertyInfo myPropInfo = myType.GetProperty("Item", myTypeArr);
// Display the property.
Console.WriteLine("The {0} property exists in MyClass3.",
myPropInfo.ToString());
}
catch (NullReferenceException e)
{
Console.WriteLine("An exception occurred.");
Console.WriteLine("Source : {0}", e.Source);
Console.WriteLine("Message : {0}", e.Message);
}
}
}
open System
type MyClass1() =
let myArray = array2D [[1; 2]; [3; 4]]
// Declare an indexed property.
member _.Item
with get (i, j) =
myArray[i, j]
and set (i, j) value =
myArray[i, j] <- value
try
// Get the Type object.
let myType = typeof<MyClass1>
let myTypeArr = Array.zeroCreate<Type> 2
// Create an instance of a Type array.
myTypeArr.SetValue(typeof<int>, 0)
myTypeArr.SetValue(typeof<int>, 1)
// Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
let myPropInfo = myType.GetProperty("Item", myTypeArr)
// Display the property.
printfn $"The {myPropInfo} property exists in MyClass1."
with :? NullReferenceException as e ->
printfn "An exception occurred."
printfn $"Source : {e.Source}"
printfn $"Message : {e.Message}"
Imports System.Reflection
Module Module3
Class MyClass3
Private myArray As Integer(,) = {{1, 2}, {3, 4}}
' Declare an indexer.
Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
Get
Return myArray(i, j)
End Get
Set(ByVal Value As Integer)
myArray(i, j) = Value
End Set
End Property
End Class
Public Class MyTypeClass3
Public Shared Sub Run()
Try
' Get the Type Object.
Dim myType As Type = GetType(MyClass3)
Dim myTypeArr(1) As Type
' Create an instance of a Type array.
myTypeArr.SetValue(GetType(Integer), 0)
myTypeArr.SetValue(GetType(Integer), 1)
' Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
Dim myPropInfo As PropertyInfo = myType.GetProperty("Item", myTypeArr)
' Display the property.
Console.WriteLine("The {0} property exists in MyClass3.", myPropInfo.ToString())
Catch e As NullReferenceException
Console.WriteLine("An exception occurred.")
Console.WriteLine("Source : {0}", e.Source.ToString())
Console.WriteLine("Message : {0}", e.Message.ToString())
End Try
End Sub
End Class
End Module 'Module3
Commenti
Una proprietà viene considerata pubblica ai fini della riflessione se ha almeno un accessore pubblico. In caso contrario, la proprietà è considerata privata ed è necessario usare BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic combinare i valori usando Or) per ottenerla.
La ricerca name fa distinzione tra maiuscole e minuscole. La ricerca include le proprietà pubbliche statiche e di istanza.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce PropertyInfo con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo generico o di un metodo generico, questo metodo cerca le proprietà del vincolo di classe.
Vedi anche
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
Si applica a
GetProperty(String, Type, Type[])
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
Cerca la proprietà pubblica specificata i cui parametri corrispondono ai tipi di argomento specificati.
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types);
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType, Type[] types);
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
member this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type()) As PropertyInfo
Parametri
- name
- String
Stringa contenente il nome della proprietà pubblica da ottenere.
- returnType
- Type
Tipo restituito della proprietà.
- types
- Type[]
Matrice di Type oggetti che rappresentano il numero, l'ordine e il tipo dei parametri per la proprietà indicizzata da ottenere.
oppure
Matrice vuota del tipo Type ,ovvero Type[] types = new Type[0]) per ottenere una proprietà non indicizzata.
Valori restituiti
Oggetto che rappresenta la proprietà pubblica i cui parametri corrispondono ai tipi di argomento specificati, se trovato; in caso contrario, null.
Implementazioni
- Attributi
Eccezioni
Vengono trovate più proprietà con il nome specificato e corrispondenti ai tipi di argomento specificati.
types è multidimensionale.
Un elemento di types è null.
Commenti
Una proprietà viene considerata pubblica ai fini della riflessione se ha almeno un accessore pubblico. In caso contrario, la proprietà è considerata privata ed è necessario usare BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic combinare i valori usando Or) per ottenerla.
La ricerca name fa distinzione tra maiuscole e minuscole. La ricerca include le proprietà pubbliche statiche e di istanza.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce PropertyInfo con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo generico o di un metodo generico, questo metodo cerca le proprietà del vincolo di classe.
Vedi anche
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
Si applica a
GetProperty(String, Type, Type[], ParameterModifier[])
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
- Origine:
- Type.cs
Cerca la proprietà pubblica specificata i cui parametri corrispondono ai tipi di argomento e ai modificatori specificati.
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
member this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo
Parametri
- name
- String
Stringa contenente il nome della proprietà pubblica da ottenere.
- returnType
- Type
Tipo restituito della proprietà.
- types
- Type[]
Matrice di Type oggetti che rappresentano il numero, l'ordine e il tipo dei parametri per la proprietà indicizzata da ottenere.
oppure
Matrice vuota del tipo Type ,ovvero Type[] types = new Type[0]) per ottenere una proprietà non indicizzata.
- modifiers
- ParameterModifier[]
Matrice di ParameterModifier oggetti che rappresentano gli attributi associati all'elemento corrispondente nella types matrice. Il gestore di associazione predefinito non elabora questo parametro.
Valori restituiti
Oggetto che rappresenta la proprietà pubblica che soddisfa i requisiti specificati, se trovato; in caso contrario, null.
Implementazioni
- Attributi
Eccezioni
Vengono trovate più proprietà con il nome specificato e corrispondenti ai tipi di argomento e ai modificatori specificati.
types è multidimensionale.
oppure
modifiers è multidimensionale.
oppure
types e modifiers non hanno la stessa lunghezza.
Un elemento di types è null.
Esempio
Nell'esempio seguente viene ottenuto un Type oggetto corrispondente a MyPropertyClasse la proprietà indicizzata di questa classe viene recuperata utilizzando gli argomenti passati al GetProperty metodo .
using System;
using System.Reflection;
public class MyPropertyClass
{
private readonly int [,] _myPropertyArray = new int[10,10];
// Declare an indexer.
public int this [int i,int j]
{
get
{
return _myPropertyArray[i,j];
}
set
{
_myPropertyArray[i,j] = value;
}
}
}
public class MyTypeClass
{
public static void Main()
{
try
{
Type myType=typeof(MyPropertyClass);
Type[] myTypeArray = new Type[2];
// Create an instance of the Type array representing the number, order
// and type of the parameters for the property.
myTypeArray.SetValue(typeof(int),0);
myTypeArray.SetValue(typeof(int),1);
// Search for the indexed property whose parameters match the
// specified argument types and modifiers.
PropertyInfo myPropertyInfo = myType.GetProperty("Item",
typeof(int),myTypeArray,null);
Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name +
" has a property type of " + myPropertyInfo.PropertyType);
}
catch(Exception ex)
{
Console.WriteLine("An exception occurred " + ex.Message);
}
}
}
open System
type MyPropertyClass() =
let myPropertyArray = Array2D.zeroCreate<int> 10 10
// Declare an indexed property.
member _.Item
with get (i, j) =
myPropertyArray[i, j]
and set (i, j) value =
myPropertyArray[i, j] <- value
try
let myType = typeof<MyPropertyClass>
let myTypeArray = Array.zeroCreate<Type> 2
// Create an instance of the Type array representing the number, order
// and type of the parameters for the property.
myTypeArray.SetValue(typeof<int>, 0)
myTypeArray.SetValue(typeof<int>, 1)
// Search for the indexed property whose parameters match the
// specified argument types and modifiers.
let myPropertyInfo = myType.GetProperty("Item", typeof<int>, myTypeArray, null)
printfn $"{myType.FullName}.{myPropertyInfo.Name} has a property type of {myPropertyInfo.PropertyType}"
with ex ->
printfn $"An exception occurred {ex.Message}"
Imports System.Reflection
Public Class MyPropertyClass
Private myPropertyArray(9, 9) As Integer
' Declare an indexer.
Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
Get
Return myPropertyArray(i, j)
End Get
Set(ByVal Value As Integer)
myPropertyArray(i, j) = Value
End Set
End Property
End Class
Public Class MyTypeClass21
Public Shared Sub Run()
Try
Dim myType As Type = GetType(MyPropertyClass)
Dim myTypeArray(1) As Type
' Create an instance of a Type array representing the number, order
' and type of the parameters for the property.
myTypeArray.SetValue(GetType(Integer), 0)
myTypeArray.SetValue(GetType(Integer), 1)
' Search for the indexed property whose parameters match the
' specified argument types and modifiers.
Dim myPropertyInfo As PropertyInfo = myType.GetProperty("Item",
GetType(Integer), myTypeArray, Nothing)
Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name +
" has a property type of " + myPropertyInfo.PropertyType.ToString())
Catch ex As Exception
Console.WriteLine("An exception occurred " + ex.Message.ToString())
End Try
End Sub
End Class
Commenti
Una proprietà viene considerata pubblica ai fini della riflessione se ha almeno un accessore pubblico. In caso contrario, la proprietà è considerata privata ed è necessario usare BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic combinare i valori usando Or) per ottenerla.
Anche se il binder predefinito non elabora ParameterModifier (il parametro modifiers), è possibile usare la classe astratta System.Reflection.Binder per scrivere un binder personalizzato che esegue l'elaborazione di modifiers.
ParameterModifier viene utilizzato solo quando si esegue la chiamata tramite interoperabilità COM e vengono gestiti solo i parametri passati per riferimento.
La ricerca name fa distinzione tra maiuscole e minuscole. La ricerca include le proprietà pubbliche statiche e di istanza.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce PropertyInfo con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo generico o di un metodo generico, questo metodo cerca le proprietà del vincolo di classe.
Vedi anche
- PropertyInfo
- String
- DefaultBinder
- ParameterModifier
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)