Faithlife.Reflection provides helpers for reflecting over .NET types.
The Faithlife.Reflection class library makes reflecting over .NET types more intuitive and more efficient compared to using raw .NET reflection.
For now, the focus is on DTOs and tuples.
Consult the reference documentation for specific details.
A DTO (data transfer object) typically has one or more read/write properties and a default constructor.
The DtoInfo
static class makes it easy to:
Anonymous types, value types, and other DTO types with read-only properties are also supported. Enumerating properties and getting property values will work fine, but other operations like setting properties will fail at runtime.
To access the information for a DTO, call DtoInfo.GetInfo
. The generic overload, DtoInfo.GetInfo<T>()
, is slightly more efficient and returns information with stronger types than the non-generic overload, DtoInfo.GetInfo(Type)
.
The following method creates a dictionary of property names and values from an arbitrary DTO instance. (Try it!)
static IReadOnlyDictionary<string, object> ConvertDtoToDictionary(object dto) =>
DtoInfo.GetInfo(dto.GetType()).Properties.ToDictionary(x => x.Name, x => x.GetValue(dto));
For example, ConvertDtoToDictionary(new { one = 1, two = "II" })
returns new Dictionary<string, object> { ["one"] = 1, ["two"] = "II" }
.
The following method creates a new instance of the specified type and sets its Id
property to the specified string. (Try it!)
static T CreateWithId<T>(string id)
{
var dtoInfo = DtoInfo.GetInfo<T>();
var dto = dtoInfo.CreateNew();
dtoInfo.GetProperty<string>("Id").SetValue(dto, id);
return dto;
}
For example, CreateWithId<Widget>("xyzzy")
returns new Widget { Id = "xyzzy" }
.
Better yet, pass tuples of property names and values to automatically initialize properties, even if they are read-only. (Try it!)
static T CreateWithId<T>(string id) => DtoInfo.GetInfo<T>().CreateNew(("Id", id));
C# 7 introduced syntax for tuples, which use the System.ValueTuple<...>
types. Before that, the System.Tuple<...>
types were commonly used. This library supports both kinds of tuples.
The TupleInfo
static class makes it easy to:
To access the information for a tuple, call TupleInfo.GetInfo
. The generic overload, TupleInfo.GetInfo<T>()
, is slightly more efficient and returns information with stronger types than the non-generic overload, TupleInfo.GetInfo(Type)
.
The following method splits a string and converts the substrings to items of the specified tuple. (Try it!)
static T SplitString<T>(string text, char delim)
{
var tupleInfo = TupleInfo.GetInfo<T>();
var items = new object[tupleInfo.ItemTypes.Count];
var strings = text.Split(delim, tupleInfo.ItemTypes.Count);
for (int i = 0; i < strings.Length; i++)
{
var itemType = tupleInfo.ItemTypes[i];
itemType = Nullable.GetUnderlyingType(itemType) ?? itemType;
items[i] = Convert.ChangeType(strings[i], itemType, CultureInfo.InvariantCulture);
}
return tupleInfo.CreateNew(items);
}
For example, SplitString<(bool, string, int?)>("true,hey,2", ',')
returns the tuple (true, "hey", 2)
.