Wrapper<T> Class
Definition
- Namespace
- Cuemon.Extensions
- Assemblies
- 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
| Name | Description |
|---|---|
| Wrapper() | Initializes a new instance of the Wrapper<T> class. |
| Wrapper(T, MemberInfo) | Initializes a new instance of the Wrapper<T> class. |
Properties
| Name | Description |
|---|---|
| Data | Gets a collection of key/value pairs that provide additional user-defined information about this wrapper object. |
| HasMemberReference | Gets a value indicating whether this instance has a member reference. |
| Instance | Gets the object that this wrapper represents. |
| InstanceType | Gets the type of the object that this wrapper represents. |
| MemberReference | Gets the member from where Instance was referenced. |
Methods
| Name | Description |
|---|---|
| InstanceAs<TResult>() | Returns a value that is equivalent to the instance of the object that this wrapper represents. |
| InstanceAs<TResult>(IFormatProvider) | Returns a value that is equivalent to the instance of the object that this wrapper represents. |
| ToString() | Returns a string that represents this instance. |