|
using System;
using System.Data;
using System.Reflection;
namespace Light.Model
{
/// <summary>
/// Represents a property based column.
/// </summary>
class PropertyColumn : AbstractColumn
{
PropertyInfo property;
MethodInfo getter;
MethodInfo setter;
/// <summary>
/// Creates a new property column.
/// </summary>
/// <param name="property">underlying class property</param>
/// <param name="name">column name</param>
/// <param name="dbtype">database data type</param>
/// <param name="primaryKey">whether this column is a primary key column</param>
/// <param name="identity">whether this column is an identity column</param>
internal PropertyColumn(PropertyInfo property, string name, DbType dbtype,
bool primaryKey, bool identity)
: this(property, name, dbtype, 0, primaryKey, identity)
{
}
/// <summary>
/// Creates a new property column.
/// </summary>
/// <param name="property">underlying class property</param>
/// <param name="name">column name</param>
/// <param name="dbtype">database data type</param>
/// <param name="size">size of the database data type</param>
/// <param name="primaryKey">whether this column is a primary key column</param>
/// <param name="identity">whether this column is an identity column</param>
internal PropertyColumn(PropertyInfo property, string name, DbType dbtype,
int size, bool primaryKey, bool identity)
: this(property, name, dbtype, size, 0, 0, primaryKey, identity)
{
}
/// <summary>
/// Creates a new property column.
/// </summary>
/// <param name="property">underlying class property</param>
/// <param name="name">column name</param>
/// <param name="dbtype">database data type</param>
/// <param name="size">size of the database data type</param>
/// <param name="precision">precision of the database data type</param>
/// <param name="scale">scale of the database data type</param>
/// <param name="primaryKey">whether this column is a primary key column</param>
/// <param name="identity">whether this column is an identity column</param>
internal PropertyColumn(PropertyInfo property, string name, DbType dbtype, int size,
byte precision, byte scale, bool primaryKey, bool identity)
: base(name, dbtype, size, precision, scale, primaryKey, identity)
{
this.property = property;
this.getter = property.GetGetMethod(true);
this.setter = property.GetSetMethod(true);
}
/// <summary>
/// True if this column can be written to. That is,
/// whether calling the Write method can actually
/// attempt to write a value into a target object.
/// </summary>
public override bool Writeable
{
get { return setter != null; }
}
/// <summary>
/// True if this column can be read from. That is,
/// whether calling the Read method will return a valid value.
/// </summary>
public override bool Readable
{
get { return getter != null; }
}
/// <summary>
/// Attempts to write a given value info a given target object.
/// </summary>
/// <param name="target">target object</param>
/// <param name="value">value to write</param>
public override void Write(object target, object value)
{
if(!Writeable)
return;
setter.Invoke(target, new object[] { value });
}
/// <summary>
/// Attempts to read a value from a given source object.
/// </summary>
/// <param name="source">source object</param>
/// <returns>value of this column from the source object</returns>
public override object Read(object source)
{
if(!Readable)
return null;
return getter.Invoke(source, null);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.