Examples of using Ikc5.TypeLibrary

This post lists examples of the using classes from NuGet package Ikc5.TypeLibrary. Full code is accessible on GitHub Blog repository.

static class TypeExtensions

Class contains the extensions that manipulate DefaultValueAttribute and properties through reflection.

bool SetDefaultValue(this object thisObject, string propertyName)
Method sets value to the property from DefaultValue attribute or do nothing if the attribute is not defined.
Returns true if property value is set.

bool SetDefaultValue(this object thisObject, T defaultValue, string propertyName)
Method sets value to the property from DefaultValue attribute or assigns provided default value if the attribute is not defined.
Returns true if property value is set.

These methods could be used in class constructor and allow initialize properties from current or/and base classes. Example of the usage:

public DefaultsClass1()
{
	// set value of Name property to default value "Instance1"
	this.SetDefaultValue<string>(nameof(Name));
	// set value of Index property to default value, 1
	this.SetDefaultValue<int>(nameof(Index));
	// do nothing, as DefaultValue attribute is not used for Checked property
	this.SetDefaultValue<bool>(nameof(Checked));
}

void SetDefaultValues(this object thisObject)
Method sets default values to those properties that has DefaultValue attribute. Method could be used in class constructor and allows initialize all properties of the class.

Example of the usage:

public DefaultsClass()
{
	this.SetDefaultValues();
}

bool GetDefaultValue(this Type thisObjectType, ref T value, string propertyName = null)
Method assigns value to the variable from DefaultValue attribute or do nothing if the attribute is not defined. Let’s note, that this overload version should be used only if you haven’t instance of the object.
Returns true if variable value is set.

bool GetDefaultValue(this object thisObject, ref T value, string propertyName = null)
Method assigns value to the variable from DefaultValue attributes or do nothing if the attribute is not defined.
Returns true if variable value is set.

bool GetDefaultValue(this Type thisObjectType, ref T value, T defaultValue, string propertyName = null)
Method assigns value to the variable from DefaultValue attribute or assigns provided default value if the attribute is not defined. Let’s note, that this overload version should be used only if you haven’t instance of the object.
Returns true if variable value is set.

bool GetDefaultValue(this object thisObject, ref T value, T defaultValue, string propertyName = null)
Method assigns value to the variable from DefaultValue attribute or assigns provided default value if the attribute is not defined.
Returns true if variable value is set.

Example of the usage of overloaded methods on Object object

public DefaultsClass2()
{
	// set value of Name property to default value "Instance2"
	this.GetDefaultValue(ref _name, nameof(Name));
	// set value of Index property to default value, 2
	this.GetDefaultValue(ref _index, 5, nameof(Index));
	// set value of Checked property to TRUE despite DefaultValue attribute is not used
	this.GetDefaultValue(ref _checked, true, nameof(Checked));
}

Similarly, there is the example of the usage of overloaded methods on Type object

public DefaultsClass3()
{
	// set value of Name property to default value "Instance3"
	GetType().GetDefaultValue(ref _name, nameof(Name));
	// set value of Index property to default value, 3
	GetType().GetDefaultValue(ref _index, 5, nameof(Index));
	// set value of Checked property to TRUE despite DefaultValue attribute is not used
	GetType().GetDefaultValue(ref _checked, true, nameof(Checked));
}

object CopyValuesFrom(this object thisObject, object anotherObject, bool top = true)
Method copies property values to the current object from the another object using reflection. It considers properties that are declared in the thisObject’s type. If value of top parameter is true, lite object type contains properties that belong exactly to the type of parent object. Otherwise, lite object contains all read-write properties of parent object.
Returns thisObject with updates values.

object CopyValuesTo(this object thisObject, object anotherObject, bool top = true)
Method copies property values from the current object to the another object using reflection. It considers properties that are declared in the thisObject’s type. If value of top parameter is true, lite object type contains properties that belong exactly to the type of parent object. Otherwise, lite object contains all read-write properties of parent object.
Returns thisObject with updates values.

Example of the usage (taken from LiteObjectService class):

var liteObjectType = GetLiteType(parentObject.GetType(), top);
var liteObject = Activator.CreateInstance(liteObjectType);
return parentObject.CopyValuesTo(liteObject, top);

and

var liteObjectType = GetLiteType(parentObject.GetType(), top);
if (liteObjectType == null || liteObjectType != liteObject.GetType())
	return null;
return parentObject.CopyValuesFrom(liteObject, top);

interface ILiteObjectService

Interface contains methods that allow to create and manipulate lite object that are objects with public write-read properties of provided parent object. It is provided with implementation, LiteObjectService class, and intended to be used in dependency injection container as singleton. For example,

var liteObjectType = GetLiteType(parentObject.GetType(), top);
var liteObject = Activator.CreateInstance(liteObjectType);
return parentObject.CopyValuesTo(liteObject, top);
Container.RegisterType<ILiteObjectService, LiteObjectService>(new ContainerControlledLifetimeManager());

Type GetLiteObjectType(object parentObject, bool top = true)
Method returns Type object that describes lite object with public properties from parent object. If value of top parameter is true, lite object type contains properties that belong exactly to the type of parent object. Otherwise, lite object contains all read-write properties of parent object.
Returns Type of the lite object. Could be used for object creation.

object GetLiteObject(object parentObject, bool top = true)
Method returns the instance of the lite object with public properties values that are taken from parent object. If value of top parameter is true, lite object type contains properties that belong exactly to the type of parent object. Otherwise, lite object contains all read-write properties of parent object.
Returns the instance of the lite object. Could be used for serialization.

Example of the usage:

var liteUserSettings = LiteObjectService.GetLiteObject(userSettings);
(new XmlSerializer(liteUserSettings.GetType())).Serialize(writer, liteUserSettings);

object CopyLiteObjectValues(object parentObject, object liteObject, bool top = true)
Method copies values of public properties from the lite object to the correspond public properties of parent object. If value of top parameter is true, lite object type contains properties that belong exactly to the type of parent object. Otherwise, lite object contains all read-write properties of parent object.
Method returns parent object with updated values.

Example of usage:

var liteUserSettingsType = LiteObjectService.GetLiteObjectType(userSettings);
var sourceSettings = (new XmlSerializer(liteUserSettingsType)).Deserialize(reader);
LiteObjectService.CopyLiteObjectValues(userSettings, sourceSettings);

1. All used IP-addresses, names of servers, workstations, domains, are fictional and are used exclusively as a demonstration only.
2. Information is provided «AS IS».

3 thoughts on “Examples of using Ikc5.TypeLibrary

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s