Class MemberParser
- Namespace
- Cuemon.Reflection
- Assembly
- Cuemon.Core.dll
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
MemberParser(Type, IEnumerable<MemberArgument>)
Initializes a new instance of the MemberParser class.
public MemberParser(Type source, IEnumerable<MemberArgument> memberArguments)
Parameters
sourceTypeThe Type of the source to rehydrate.
memberArgumentsIEnumerable<MemberArgument>The arguments associated with the
source.
Properties
MemberArguments
Gets the arguments associated with the source to rehydrate.
public IEnumerable<MemberArgument> MemberArguments { get; }
Property Value
- IEnumerable<MemberArgument>
The arguments associated with the source to rehydrate.
ProcessedMemberArguments
Gets the processed arguments associated with the source to rehydrate.
public IEnumerable<MemberArgument> ProcessedMemberArguments { get; }
Property Value
- IEnumerable<MemberArgument>
The arguments associated with the source to rehydrate.
Remarks
This can be useful for debugging why an instance is not rehydrated as expected.
Source
Gets the Type of the source to rehydrate.
public Type Source { get; }
Property Value
Methods
CreateInstance(Func<ConstructorInfo, bool>)
Creates an instance of the Type associated with this parser.
public object CreateInstance(Func<ConstructorInfo, bool> predicate = null)
Parameters
predicateFunc<ConstructorInfo, bool>A function to test each ConstructorInfo for a condition.