Class ObjectDecoratorExtensions
- Namespace
- Cuemon
- Assembly
- Cuemon.Core.dll
Extension methods for the Object class hidden behind the IDecorator<T> interface.
public static class ObjectDecoratorExtensions
- Inheritance
-
ObjectDecoratorExtensions
Examples
ObjectDecoratorExtensions provides extension methods on Decorator.Enclose for type conversion, hierarchical tree traversal, and property resolution via reflection. This example converts string representations of "42", "2024-01-15T10:30:00Z", "not-a-number", and "Ascending" into their target types using ChangeType<T> and ChangeTypeOrDefault<T> with fallback support. It also builds a three-level TreeNode hierarchy and visits all nodes with TraverseWhileNotEmpty, and reads a property value through DefaultPropertyValueResolver. Console output confirms each type conversion succeeded and lists all visited node names in depth-first traversal order.
using System;
using System.Collections.Generic;
using System.Globalization;
using Cuemon;
namespace DocExamples
{
public static class ObjectDecoratorExamples
{
public static void Main()
{
// Convert a string to an integer
object input = "42";
int result = Decorator.Enclose(input).ChangeType<int>();
Console.WriteLine($"Converted string to int: {result} (type: {result.GetType().Name})");
// Convert a string to DateTime (UTC)
object dateInput = "2024-01-15T10:30:00Z";
DateTime dateResult = Decorator.Enclose(dateInput).ChangeType<DateTime>();
Console.WriteLine($"Converted to DateTime: {dateResult} (Kind: {dateResult.Kind})");
// Convert with a fallback value
object invalidInput = "not-a-number";
int fallbackResult = Decorator.Enclose(invalidInput).ChangeTypeOrDefault<int>(42);
Console.WriteLine($"Conversion with fallback: {fallbackResult}");
// Convert a string to an enum
object enumInput = "Ascending";
var enumResult = Decorator.Enclose(enumInput).ChangeType<SortOrder>();
Console.WriteLine($"Converted to SortOrder: {enumResult}");
// Traverse a hierarchical tree structure
var grandchild = new TreeNode { Name = "Grandchild" };
var child1 = new TreeNode { Name = "Child1", Children = { grandchild } };
var child2 = new TreeNode { Name = "Child2" };
var root = new TreeNode { Name = "Root", Children = { child1, child2 } };
var allNodes = Decorator.Enclose(root).TraverseWhileNotEmpty(node => node.Children);
foreach (var node in allNodes)
{
Console.WriteLine($"Visited: {node.Name}");
}
var property = typeof(TreeNode).GetProperty(nameof(TreeNode.Name));
var resolvedName = Decorator.Enclose((object)root).DefaultPropertyValueResolver(property);
Console.WriteLine($"Resolved property value: {resolvedName}");
}
private class TreeNode
{
public string Name { get; set; }
public List<TreeNode> Children { get; } = new List<TreeNode>();
}
}
}
Methods
ChangeType(IDecorator<object>, Type, Action<ObjectFormattingOptions>)
Returns an Object of a specified targetType whose value is equivalent to the enclosed Object of the specified decorator.
public static object ChangeType(this IDecorator<object> decorator, Type targetType, Action<ObjectFormattingOptions> setup = null)
Parameters
decoratorIDecorator<object>The IDecorator<T> to extend.
targetTypeTypeThe type of the object to return.
setupAction<ObjectFormattingOptions>The ObjectFormattingOptions which may be configured.
Returns
Remarks
What differs from the ChangeType(object, Type) is, that this converter supports generics and enums. Fallback uses TypeDescriptor and checks if the underlying IFormatProvider of FormatProvider is a CultureInfo, then this will be used in the conversion together with DescriptorContext.
Exceptions
- ArgumentNullException
decoratorcannot be null.- AggregateException
The enclosed Object of
decoratorcould not be converted.
- See Also
ChangeTypeOrDefault<T>(IDecorator<object>, T, Action<ObjectFormattingOptions>)
Returns an Object of the specified T whose value is equivalent to the enclosed Object of the specified decorator.
public static T ChangeTypeOrDefault<T>(this IDecorator<object> decorator, T fallbackResult = default, Action<ObjectFormattingOptions> setup = null)
Parameters
decoratorIDecorator<object>The IDecorator<T> to extend.
fallbackResultTThe value to return when a conversion is not possible. Default is
defaultofT.setupAction<ObjectFormattingOptions>The ObjectFormattingOptions which may be configured.
Returns
- T
An Object of type
Tequivalent to the enclosed Object of the specifieddecoratorwhen a conversion is possible; otherwisefallbackResultis returned.
Type Parameters
TThe type of the object to return.
Remarks
This method first checks if the enclosed Object of the specified decorator is compatible with T; if incompatible the method continues with ChangeType<T>(IDecorator<object>, Action<ObjectFormattingOptions>) for the operation.
ChangeType<T>(IDecorator<object>, Action<ObjectFormattingOptions>)
Returns an Object of the specified T whose value is equivalent to the enclosed Object of the specified decorator.
public static T ChangeType<T>(this IDecorator<object> decorator, Action<ObjectFormattingOptions> setup = null)
Parameters
decoratorIDecorator<object>The IDecorator<T> to extend.
setupAction<ObjectFormattingOptions>The ObjectFormattingOptions which may be configured.
Returns
Type Parameters
TThe type of the object to return.
Remarks
What differs from the ChangeType(object, Type) is, that this converter supports generics and enums. Fallback uses TypeDescriptor and checks if the underlying IFormatProvider of FormatProvider is a CultureInfo, then this will be used in the conversion together with DescriptorContext.
Exceptions
- ArgumentNullException
decoratorcannot be null.- AggregateException
The enclosed Object of
decoratorcould not be converted.
- See Also
DefaultPropertyValueResolver(IDecorator<object>, PropertyInfo)
Resolves the default value of a property from the enclosed object of the specified decorator.
public static object DefaultPropertyValueResolver(this IDecorator<object> decorator, PropertyInfo pi)
Parameters
decoratorIDecorator<object>The IDecorator<T> to extend.
piPropertyInfoThe PropertyInfo of the property to resolve the value for.
Returns
- object
The value of the property if the enclosed object is not null; otherwise,
null.
Remarks
This API supports the product infrastructure and is not intended to be used directly from your code.
TraverseWhileNotEmpty<TSource>(IDecorator<TSource>, Func<TSource, IEnumerable<TSource>>)
Invokes the specified traversal path of the enclosed object of the specified decorator until obstructed by an empty sequence.
public static IEnumerable<TSource> TraverseWhileNotEmpty<TSource>(this IDecorator<TSource> decorator, Func<TSource, IEnumerable<TSource>> traversal) where TSource : class
Parameters
decoratorIDecorator<TSource>The IDecorator<T> to extend.
traversalFunc<TSource, IEnumerable<TSource>>The function delegate that is invoked until the traveled path is obstructed by an empty sequence.
Returns
- IEnumerable<TSource>
An IEnumerable<T> sequence equal to the traveled path of the enclosed object of the specified
decorator.
Type Parameters
TSource
Exceptions
- ArgumentNullException
decorator-or-traversalis null.