B-Arts DocumentationIndice generale

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.

Creare uno schema JSON da un tipo

public class Risultato
{
  public string Descrizione { get; set; } = "";
  public int Quantita { get; set; }
  public double Totale { get; set; }
  public DateTime Data { get; set; }
}

Dictionary<string, object> schema =
  BReflection.CreateJsonSchema(typeof(Risultato));

Stringhe, date, Guid e TimeSpan diventano string; gli interi diventano integer, float/double diventano number, gli enum espongono i nomi e array/List<T> diventano array. Le proprietà con JsonIgnore(Always) vengono escluse e JsonPropertyName modifica il nome nello schema.

Schema dei parametri e invocazione di un delegate

static async Task<double> CalcolaTotaleAsync(int quantita, double prezzo)
{
  await Task.CompletedTask;
  return quantita * prezzo;
}

Func<int, double, Task<double>> function = CalcolaTotaleAsync;

Dictionary<string, object> parameters =
  BReflection.CreateParametersJsonSchema(function.Method);

object? value = await BReflection.InvokeDelegateAsync(
  function,
  "{\"quantita\":3,\"prezzo\":12.5}");

I parametri mancanti usano il valore predefinito quando presente; altrimenti viene generata ArgumentException. La deserializzazione usa BJSON.DefaultOptions.