Class Wrapper<T>
- Namespace
- Cuemon.Extensions
- Assembly
- Cuemon.Extensions.Core.dll
Provides a way to wrap an object of type T.
public class Wrapper<T> : IWrapper<T>, IData
Type Parameters
TThe type of the object to wrap.
- Inheritance
-
Wrapper<T>
- Implements
-
IWrapper<T>
- Derived
- Extension Methods
Examples
The following example demonstrates how to use Wrapper<T> to wrap an object with optional member reference metadata.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using Cuemon.Extensions;
namespace MyApp.Examples;
public static class WrapperExample
{
private sealed class WrapperExampleModel
{
public string Name { get; set; } = string.Empty;
}
public static void Demonstrate()
{
PropertyInfo nameProperty = typeof(WrapperExampleModel).GetProperty(nameof(WrapperExampleModel.Name), BindingFlags.Public | BindingFlags.Instance);
var answer = new Wrapper<int>(42, nameProperty);
answer.Data["category"] = "number";
var fromText = new Wrapper<string>("42");
var bytes = new Wrapper<byte[]>(new byte[] { 1, 2, 3, 4 });
var type = new Wrapper<Type>(typeof(Dictionary<string, int>));
Console.WriteLine(answer.Instance);
Console.WriteLine(answer.MemberReference?.Name);
Console.WriteLine(answer.InstanceAs<string>(CultureInfo.InvariantCulture));
Console.WriteLine(fromText.InstanceAs<int>());
Console.WriteLine(answer.Data["category"]);
Console.WriteLine(Wrapper.ParseInstance(bytes));
Console.WriteLine(type.ToString());
}
}
Constructors
Wrapper()
Initializes a new instance of the Wrapper<T> class.
protected Wrapper()
Wrapper(T, MemberInfo)
Initializes a new instance of the Wrapper<T> class.
public Wrapper(T instance, MemberInfo memberReference = null)
Parameters
instanceTThe instance that this wrapper object represents.
memberReferenceMemberInfoThe member from where
instancewas referenced.
Exceptions
- ArgumentNullException
instanceis null.
Properties
Data
Gets a collection of key/value pairs that provide additional user-defined information about this wrapper object.
public virtual IDictionary<string, object> Data { get; }
Property Value
- IDictionary<string, object>
An object that implements the IDictionary<TKey, TValue> interface and contains a collection of user-defined key/value pairs.
HasMemberReference
Gets a value indicating whether this instance has a member reference.
public virtual bool HasMemberReference { get; }
Property Value
- bool
trueif this instance has a member reference; otherwise,false.
Instance
Gets the object that this wrapper represents.
public virtual T Instance { get; protected set; }
Property Value
- T
The object that this wrapper represents.
InstanceType
Gets the type of the object that this wrapper represents.
public virtual Type InstanceType { get; protected set; }
Property Value
- Type
The type of the that this wrapper represents.
MemberReference
Gets the member from where Instance was referenced.
public virtual MemberInfo MemberReference { get; protected set; }
Property Value
- MemberInfo
The member from where Instance was referenced.
Methods
InstanceAs<TResult>()
Returns a value that is equivalent to the instance of the object that this wrapper represents.
public TResult InstanceAs<TResult>()
Returns
- TResult
A value that is equivalent to the instance of the object that this wrapper represents.
Type Parameters
TResultThe type of the return value.
Exceptions
- InvalidCastException
The conversion is not supported - or - Instance does not implement the IConvertible interface.
- FormatException
Instance is not in a format for
Trecognized by InvariantCulture.- OverflowException
Instance represents a number that is out of the range of
T.
InstanceAs<TResult>(IFormatProvider)
Returns a value that is equivalent to the instance of the object that this wrapper represents.
public TResult InstanceAs<TResult>(IFormatProvider provider)
Parameters
providerIFormatProviderAn object that supplies culture-specific formatting information.
Returns
- TResult
A value that is equivalent to the instance of the object that this wrapper represents.
Type Parameters
TResultThe type of the return value.
Exceptions
- InvalidCastException
The conversion is not supported - or - Instance does not implement the IConvertible interface.
- FormatException
Instance is not in a format for
Trecognized byprovider.- OverflowException
Instance represents a number that is out of the range of
T.
ToString()
Returns a string that represents this instance.
public override string ToString()