Table of Contents

MemberParser Class

Definition

Namespace
Cuemon.Reflection
Assemblies
Cuemon.Core.dll
Source
src/Cuemon.Core/Reflection/MemberParser.cs

Provides a generic way to rehydrate serialized objects.

public class MemberParser
Inheritance
MemberParser

Examples

The following example shows how to hydrate a type from named reflection arguments.

using System;
using System.Collections.Generic;
using System.Linq;
using Cuemon.Reflection;

namespace MyApp.Examples;

public static class MemberParserExample
{
    public static void Demonstrate()
    {
        var arguments = new List<MemberArgument>
        {
            new("sku", "SKU-42"),
            new("price", 19.95m),
            new("stock", 12)
        };

        var parser = new MemberParser(typeof(CatalogItem), arguments);
        var item = (CatalogItem)parser.CreateInstance(ctor => ctor.GetParameters().Length == 2);

        Console.WriteLine(item.Sku);
        Console.WriteLine(item.Price);
        Console.WriteLine(item.Stock);
        Console.WriteLine(string.Join(", ", parser.ProcessedMemberArguments.Select(argument => argument.Name)));
    }
}

public sealed class CatalogItem
{
    public CatalogItem(string sku, decimal price)
    {
        Sku = sku;
        Price = price;
    }

    public string Sku { get; }

    public decimal Price { get; }

    public int Stock { get; set; }
}

Constructors

Name Description
MemberParser(Type, IEnumerable<MemberArgument>)

Initializes a new instance of the MemberParser class.

Properties

Name Description
MemberArguments

Gets the arguments associated with the source to rehydrate.

ProcessedMemberArguments

Gets the processed arguments associated with the source to rehydrate.

Source

Gets the Type of the source to rehydrate.

Methods

Name Description
CreateInstance(Func<ConstructorInfo, bool>)

Creates an instance of the Type associated with this parser.