Read Me
Commons.net
-----------
A subset of mostly used modules in Jakarta commons in C#.
Samples
-------
For samples please look into the unit test cases. Source can be obtained from https://commonsnet.svn.sourceforge.net/svnroot/commonsnet
This release contains the following:
------------------------------------
TypeUtils
CopyType - copies a object content to another object based on either field names or property names. Inclusions and exclusions are allowed by name. It can also type an object to an interfacem, in which case Castle's dynamic proxy will be used to create an instance of a class implementing the interface. Only fields/properties in the destination object, with matching fields/properties in the source object, are being copied.
TypeUtils.CopyType(destination, source) //copies all private fields in destination from matching private fields in source
TypeUtils.CopyTypeProperties(destinationObject, sourceObject) //copies all public mutable properties in destination from matching public mutable proerties in source
VerfifyProperties - Verifies that all read/write properties defined on an instance (or a class, in this case default constructor will be used to create an instance of that class) are maintaining the values passed to setters. It can be used to verify all classes defined in the assembly.
TypeUtils.VerfifyProperties(new Person()); // will verify all read/write properties defined on person instance
TypeUtils.VerfifyProperties(typeof(Person)); // will verify all read/write properties defined on person instance. Will use default constructor to create a new instance of Person.
TypeUtils.VerfifyProperties(this.GetType().Assembly); // will verify all read/write properties defined on all classes defined in the current assembly. Will use default constructors to create new instances.
The way VerifyProperties work is by using a sample value for each type. For Enum properties, the very first value of the Enum is used. For other types an internal sample values map per type is being used. Initialy this map is populated for some types as following:
+ 1 for Int32
+ 1.0 for float
+ 1.0 for double
+ 1 for byte
+ 1L for long
+ true for bool
+ string.Empty for string
+ DateTime.Today for DateTime
+ 'a' for char
+ Guid.NewGuid() for Guid
, this map can be overriden by passing a map when calling verify that defines another sample value for a specific type.
TypeUtils.VerifyProperties(MapUtils.Map<Type, object>(MapUtils.Entry(typeof (string), "a")), typeof (ClassMaintainsConditionalProperties)));
In the previous call we are testing a class that has a string property that does not accept string.Empty. This was overriden by passing sample value of "a" for type string.
MapUtils
Merge - The Ruby map merge method.
MapUtils.Merge(IDictionary destinationMap, IDictionary sourceMap)
Map, MapEntry - helping achieve a single line map creation syntax.
MapUtils.Map(MapUtils.Entry("name", "john"), MapUtils.Entry("age", 10));
ToStringBuilder
ToString - implement the ToString for an object using reflection. Field based or property based. Fields/Properties can included/excluded by name.
StringUtils.ToString(source); // returns string representation for source object based on all private fields defined on it.
EqualsToBuilder
EqualsTo - checks for equality between two objects using reflection. Fields based only. Field can be included/excluded with type or name..
EqualsToBuilder.AreEqual(objectOne, objectTwo); // compares objectOne to objectTwo (same class) based on all fields defined.
BeanIntrospector
GetFields - returns the list of fields declared in an object. Fields can be included/excluded by name.
GetMutableProperties - returns the list of read-and-write properties declared in an object. Properties can be included/excluded by name.
ListUtils
Slice - from a list of objects this method (set of overloaded methods) will return a list of object representing a single field/property values.
IList<Person> persons = ListUtils.NewList<Person>(new Person(10, "John"), new Person(20, "Mike"));
IList<int> ages = ListUtils.Slice<Person, int>(persons);
ToImmutableList - wraps a list with an immutable list that does not allow modifications on the source list. Could be used when we have to return a
list that should be internal.
ListUtils.ToImmutableList(sourceList);
ToTypesList - Converts un-typed list to a typed one.
IList<String> typedList = ListUtils.ToTypedList<String>(new ArrayList(new String[] {"a"}));
Join - Concatenates the values of list items.
ListUtils.Join(new List<String>(new String[] {"a", "b"})); // will return "a, b"