Skip to content

About

Dynamic creating Anonymous (similar) Types at program runtime

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

DynamicAnonymousType NuGet version

Create anonymous-like .NET types at runtime without defining a class in advance.

DynamicAnonymousType lets you generate a CLR type with named properties and then create instances of it on demand. It is useful when you need a lightweight DTO-like object, a dynamic shape for data transfer, or a runtime-generated type that behaves similarly to an anonymous object.

Features

  • Generate CLR types at runtime with strongly typed properties
  • Create instances dynamically using property/value tuples
  • Reuse the same generated type for repeated property sets
  • Includes basic Equals, GetHashCode, and ToString behavior for generated instances

Installation

dotnet add package DynamicAnonymousType

Quick start

using DynamicAnonymousType;

Type type = DynamicFactory.CreateType(
    ("Id", typeof(int)),
    ("Name", typeof(string)),
    ("Date", typeof(DateTime?)));

dynamic instance = type.CreateInstance(
    ("Id", 1),
    ("Name", "Text1"),
    ("Date", DateTime.Now));

Console.WriteLine(instance);
Console.WriteLine(instance.Name);

Example output:

{ Id = 1, Name = Text1, Date = 11/12/2026 10:20:35 }
Text1

You can also create an instance first and assign values later:

dynamic person = type.CreateInstance();
person.Id = 2;
person.Name = "Text2";

Why use it?

This library is a compact way to build runtime-shaped objects when you do not want to define a concrete model class up front. It is especially handy for:

  • data mapping and transformations
  • temporary DTOs
  • scaffolding code or tests
  • dynamic serialization scenarios

Example project

See the sample app in Examples/Program.cs.

License

This project is licensed under the MIT License.

About

Dynamic creating Anonymous (similar) Types at program runtime

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages