PeanutButter
Loading...
Searching...
No Matches
PeanutButter.Utils.MetadataExtensions Class Reference

Provides extension methods to set and retrieve metadata on any object. Under the hood, these methods use a ConditionalWeakTable to store your metadata, so the metadata is garbage-collected when your managed objects are garbage-collected. More...

Static Public Member Functions

static void SetMetadata (this object parent, string key, object value)
 Sets an arbitrary piece of metadata on a managed object. This metadata has the same lifetime as your object, meaning it will be garbage-collected when your object is garbage-collected, assuming nothing else is referencing it.
 
static T GetMetadata< T > (this object parent, string key)
 Attempts to retrieve a piece of metadata for an object. When the metadata key for the object is unknown, returns the default value for the type requested, eg 0 for ints, null for strings and objects. Note that if metadata exists for the requested key but not for the type requested, a type-casting exception will be thrown.
 
static T GetMetadata< T > (this object parent, string key, T defaultValue)
 Attempts to retrieve a piece of metadata for an object. When the metadata key for the object is unknown, returns the default value for the type requested, eg 0 for ints, null for strings and objects. Note that if metadata exists for the requested key but not for the type requested, a type-casting exception will be thrown. This overload allows specifying a default value.
 
static bool HasMetadata< T > (this object parent, string key)
 Tests if a parent object has a piece of metadata with the provided type.
 
static bool HasMetadata (this object parent)
 Tests if an object has any metadata stored against it at all.
 
static bool HasMetadata (this object parent, string key)
 Tests if an object has metadata with the provided key.
 
static void DeleteMetadata (this object parent)
 Deletes all metadata associated with the object, if any.
 
static void DeleteMetadata (this object parent, string key)
 Deletes the metadata identified by the key for this object, if found.
 
static bool TryGetMetadata< T > (this object parent, string key, out T result)
 Try get the named metadata for the provided type.
 
static void CopyAllMetadataTo (this object parent, object target)
 Clones all the metadata from parent to target.
 

Detailed Description

Provides extension methods to set and retrieve metadata on any object. Under the hood, these methods use a ConditionalWeakTable to store your metadata, so the metadata is garbage-collected when your managed objects are garbage-collected.

Member Function Documentation

◆ CopyAllMetadataTo()

static void PeanutButter.Utils.MetadataExtensions.CopyAllMetadataTo ( this object  parent,
object  target 
)
static

Clones all the metadata from parent to target.

  • will overwrite target data with the same key!
Parameters
parent
target

◆ DeleteMetadata() [1/2]

static void PeanutButter.Utils.MetadataExtensions.DeleteMetadata ( this object  parent)
static

Deletes all metadata associated with the object, if any.

Parameters
parent

◆ DeleteMetadata() [2/2]

static void PeanutButter.Utils.MetadataExtensions.DeleteMetadata ( this object  parent,
string  key 
)
static

Deletes the metadata identified by the key for this object, if found.

Parameters
parent
key

◆ GetMetadata< T >() [1/2]

static T PeanutButter.Utils.MetadataExtensions.GetMetadata< T > ( this object  parent,
string  key 
)
static

Attempts to retrieve a piece of metadata for an object. When the metadata key for the object is unknown, returns the default value for the type requested, eg 0 for ints, null for strings and objects. Note that if metadata exists for the requested key but not for the type requested, a type-casting exception will be thrown.

Parameters
parentParent object to query against
keyKey to query for
Template Parameters
TType of data
Returns

◆ GetMetadata< T >() [2/2]

static T PeanutButter.Utils.MetadataExtensions.GetMetadata< T > ( this object  parent,
string  key,
defaultValue 
)
static

Attempts to retrieve a piece of metadata for an object. When the metadata key for the object is unknown, returns the default value for the type requested, eg 0 for ints, null for strings and objects. Note that if metadata exists for the requested key but not for the type requested, a type-casting exception will be thrown. This overload allows specifying a default value.

Parameters
parentParent object to query against
keyKey to query for
defaultValue
Template Parameters
TType of data
Returns

◆ HasMetadata() [1/2]

static bool PeanutButter.Utils.MetadataExtensions.HasMetadata ( this object  parent)
static

Tests if an object has any metadata stored against it at all.

  • will always return false for a null object
Parameters
parent
Returns

◆ HasMetadata() [2/2]

static bool PeanutButter.Utils.MetadataExtensions.HasMetadata ( this object  parent,
string  key 
)
static

Tests if an object has metadata with the provided key.

  • will always return false for a null object
Parameters
parent
key
Returns

◆ HasMetadata< T >()

static bool PeanutButter.Utils.MetadataExtensions.HasMetadata< T > ( this object  parent,
string  key 
)
static

Tests if a parent object has a piece of metadata with the provided type.

Parameters
parentParent object to search against
keyKey to search for
Template Parameters
TExpected type of metadata
Returns

◆ SetMetadata()

static void PeanutButter.Utils.MetadataExtensions.SetMetadata ( this object  parent,
string  key,
object  value 
)
static

Sets an arbitrary piece of metadata on a managed object. This metadata has the same lifetime as your object, meaning it will be garbage-collected when your object is garbage-collected, assuming nothing else is referencing it.

Parameters
parentObject to store metadata against
keyName of the metadata item to set
valueValue to store

◆ TryGetMetadata< T >()

static bool PeanutButter.Utils.MetadataExtensions.TryGetMetadata< T > ( this object  parent,
string  key,
out T  result 
)
static

Try get the named metadata for the provided type.

Parameters
parent
key
result
Template Parameters
T
Returns

The documentation for this class was generated from the following file: