Faithlife.Reflection 1.2.4

Prefix Reserved
dotnet add package Faithlife.Reflection --version 1.2.4
                    
NuGet\Install-Package Faithlife.Reflection -Version 1.2.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Faithlife.Reflection" Version="1.2.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Faithlife.Reflection" Version="1.2.4" />
                    
Directory.Packages.props
<PackageReference Include="Faithlife.Reflection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Faithlife.Reflection --version 1.2.4
                    
#r "nuget: Faithlife.Reflection, 1.2.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Faithlife.Reflection@1.2.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Faithlife.Reflection&version=1.2.4
                    
Install as a Cake Addin
#tool nuget:?package=Faithlife.Reflection&version=1.2.4
                    
Install as a Cake Tool

Faithlife.Reflection

Faithlife.Reflection provides helpers for reflecting over .NET types.

NuGet

Overview

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 source code for specific details.

DTOs

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:

  • enumerate the public non-static properties and fields of a DTO type, both read/write and read-only
  • get the name, type, and other metadata for each property/field of the DTO type
  • get or set the value of a property/field of an instance of the DTO
  • create a new instance of the DTO, using constructor arguments if needed to initialize read-only properties
  • shallow clone an existing instance of the DTO

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.

Accessing DTO info

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).

Getting properties

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" }.

Setting properties

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));

Tuples

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:

  • determine if a type is a tuple type
  • enumerate the types of the tuple items, even for tuples with more than seven items
  • create a tuple from a list of objects

Accessing tuple info

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).

Creating a tuple

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).

Contributing

See Contributing for setup and contribution guidelines. For a history of notable changes, see the Release Notes.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Faithlife.Reflection:

Package Downloads
Faithlife.Data

Helpers for querying ADO.NET-compatible databases.

RegexMatchValues

Converts regular expression matches to strong types.

Faithlife.Testing.WebRequests

Faithlife.Testing.WebRequests class library.

Faithlife.FakeData

A fake in-memory relational database for prototypes and unit tests.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.4 44 5/27/2026
1.2.3 96,102 4/26/2023
1.2.2 10,940 10/5/2022
1.2.1 176,380 2/12/2022
1.2.0 614 2/12/2022
1.1.0 180,717 11/30/2020
1.0.1 59,121 10/3/2019
1.0.0 724 10/3/2019
0.2.1 161,187 5/21/2019
0.2.0 4,371 3/23/2019
0.1.0 753 3/23/2019