custom injections examples
Valentin Plamadeala edited this page Aug 16, 2015
·
6 revisions
DateTime to String
public class DateToStrInjection : LoopInjection
{
protected override bool MatchTypes(Type source, Type target)
{
return source == typeof(DateTime) && target == typeof(string);
}
protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp)
{
var val = (DateTime)sp.GetValue(source);
tp.SetValue(target, val.ToShortDateString());
}
}
handling enums
public class IntToEnum : LoopInjection
{
protected override bool MatchTypes(Type source, Type target)
{
return source == typeof(int) && target.IsSubclassOf(typeof(Enum));
}
}
public class EnumToInt : LoopInjection
{
protected override bool MatchTypes(Type source, Type target)
{
return source.IsSubclassOf(typeof(Enum)) && target == typeof(int);
}
}
handling nullables
public class NullablesToNormal : LoopInjection
{
protected override bool MatchTypes(Type source, Type target)
{
return Nullable.GetUnderlyingType(source) == target;
}
protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp)
{
var val = sp.GetValue(source);
if (val != null)
{
tp.SetValue(target, val);
}
}
}
public class NormalToNullables : LoopInjection
{
protected override bool MatchTypes(Type source, Type target)
{
return source == Nullable.GetUnderlyingType(target);
}
}
from ICollection<Entity> to int[]
, Entity
can be any Subclass
of Entity
( used in ProDinner )
public class EntitiesToInts : LoopInjection
{
protected override bool MatchTypes(Type src, Type trg)
{
return trg == typeof(int[])
&& src.IsGenericType
&& src.GetGenericTypeDefinition() == typeof(ICollection<>)
&& src.GetGenericArguments()[0].IsSubclassOf(typeof(Entity));
}
protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp)
{
var val = sp.GetValue(source);
if (val != null)
{
tp.SetValue(target, (val as IEnumerable<Entity>).Select(o => o.Id).ToArray());
}
}
}
IDataReader to object
public class ReaderInjection : KnownSourceInjection<IDataReader>
{
protected override void Inject(IDataReader source, object target)
{
for (var i = 0; i < source.FieldCount; i++)
{
var trgProp = target.GetType().GetProperty(source.GetName(i), BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
if (trgProp == null) continue;
var value = source.GetValue(i);
if (value == DBNull.Value) continue;
trgProp.SetValue(target, value);
}
}
}
string to textbox (windows forms), textbox name is "txt" + PropName
public class StringToTextBox : KnownTargetInjection<Form>
{
protected override void Inject(object source, ref Form form)
{
var sourceProps = source.GetType().GetProperties();
foreach (var sourceProp in sourceProps)
{
var textBox = form.Controls["txt" + sourceProp.Name] as TextBox;
if (textBox == null) continue;
textBox.Text = (string)sourceProp.GetValue(source);
}
}
}
used in testing to check that 2 objects matching properties have the same values
public class AreEqual : LoopInjection
{
protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp)
{
Assert.AreEqual(sp.GetValue(source), tp.GetValue(target));
}
}