BReflection - Esempi
Analisi di un tipo applicativo
using B.BReflections;
using B.BStatics;
using System.Collections;
using System.Reflection;
BReflection reflection = new BReflection(
typeof(CustomerEditor).FullName!,
typeof(CustomerEditor).Assembly);
if (reflection.IsNothing)
{
throw new InvalidOperationException("Tipo non trovato nell'assembly indicato.");
}
PropertyInfo? companyName = reflection.GetProperty("companyname");
MethodInfo? saveMethod = reflection.Methods
.FirstOrDefault(method => method.Name == "Save");
GetProperty e GetField confrontano i nomi senza distinzione tra maiuscole e minuscole. Per tipi definiti dall'applicazione, passare esplicitamente l'assembly o un tipo appartenente all'assembly; il costruttore con il solo nome cerca nell'assembly di BFramework.
Lettura e assegnazione sicura di proprietà
CustomerEditor customer = new CustomerEditor();
PropertyInfo property = typeof(CustomerEditor).GetProperty("Age")!;
bool assigned = BReflection.SetValueProperty(customer, property, "42");
BValueProperty? value = BReflection.GetBValueProperty(customer, property);
SetValueProperty converte i tipi primitivi supportati, gestisce i nullable e restituisce false quando l'assegnazione non è valida. GetBValueProperty restituisce valore, tipo e comportamento di lettura.
Lettura degli attributi B-Arts
using static B.BAttributes.BGlobal;
PropertyInfo property = typeof(CustomerEditor).GetProperty("CompanyName")!;
string caption = BConvert.ToString(
BReflection.GetAttribute(property, eBAttributes.BCaptionAttribute),
property.Name);
Usare il catalogo eBAttributes invece di duplicare la reflection degli attributi nei componenti applicativi.
Tipo degli elementi di una lista
IList customers = new List<CustomerEditor>();
Type? itemType = BReflection.GetItemType(customers);
GetItemType riconosce liste generiche dirette e tipi che implementano IList<T>; restituisce null per una lista non generica senza tipo determinabile.
