How to create binding proxies dynamically?
I have a config object that it has enum
, and I wanted to bind a binding RadioButton
to the same property. Since I see myself repeating this sometimes, I was trying to come up with a generic solution to bind multiple elements to the same property. I found a solution to create a proxy that executes an expression. See the code below.
public enum GenderEnum
{
Male,
Female
}
public class DataClass
{
public GenderEnum Gender { get; set; }
}
class ExpressionBinder<T>
{
public Func<T> Getter;
public Action<T> Setter;
public T Value
{
get { return Getter.Invoke(); }
set { Setter.Invoke(value); }
}
public ExpressionBinder(Func<T> getter, Action<T> setter)
{
Getter = getter;
Setter = setter;
}
}
I can use it like this:
radioMale.Tag = GenderEnum.Male;
radioFemale.Tag = GenderEnum.Female;
var proxyM = new ExpressionBinder<bool>(
() => DataObj.Gender == (GenderEnum)radioMale.Tag,
(val) => { if (val) DataObj.Gender = (GenderEnum)radioMale.Tag; });
var proxyF = new ExpressionBinder<bool>(
() => DataObj.Gender == (GenderEnum)radioFemale.Tag,
(val) => { if (val) DataObj.Gender = (GenderEnum)radioFemale.Tag; });
radioMale.DataBindings.Add("Checked", proxyM, "Value");
radioFemale.DataBindings.Add("Checked", proxyF, "Value");
This is just an example, I wanted to keep it simple. But this approach actually works . On the issue:
I want to encapsulate comparison and set expressions inside a derived class.
class ComparisonBinder : ExpressionBinder {}
Unfortunately, I can only say how I would like to use it.
radioMale.DataBindings.Add("Checked",
new ComparisonBinder<ConfigClass>((c) c.Gender, GenderEnum.Male),
"Value");
radioFemale.DataBindings.Add("Checked",
new ComparisonBinder<ConfigClass>((c) c.Gender, GenderEnum.Female),
"Value");
So how would you implement this? Feel free to change my ExpressionBinder class if you need to.
Final code
(edited again: you cannot omit the TValue parameter using the object type. The compiler will add a call when converting the lambda expression Convert()
.)
class ComparisonBinder<TSource, TValue> : ExpressionBinder<bool>
{
private TSource instance;
private TValue comparisonValue;
private PropertyInfo pInfo;
public ComparisonBinder(TSource instance, Expression<Func<TSource, TValue>> property, TValue comparisonValue)
{
pInfo = GetPropertyInfo(property);
this.instance = instance;
this.comparisonValue = comparisonValue;
Getter = GetValue;
Setter = SetValue;
}
private bool GetValue()
{
return comparisonValue.Equals(pInfo.GetValue(instance, null));
}
private void SetValue(bool value)
{
if (value)
{
pInfo.SetValue(instance, comparisonValue,null);
}
}
/// <summary>
/// Adapted from surfen answer (http://stackoverflow.com/a/10003320/219838)
/// </summary>
private PropertyInfo GetPropertyInfo(Expression<Func<TSource, TValue>> propertyLambda)
{
Type type = typeof(TSource);
MemberExpression member = propertyLambda.Body as MemberExpression;
if (member == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a method, not a property.",
propertyLambda));
PropertyInfo propInfo = member.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a field, not a property.",
propertyLambda));
if (type != propInfo.ReflectedType &&
!type.IsSubclassOf(propInfo.ReflectedType))
throw new ArgumentException(string.Format(
"Expresion '{0}' refers to a property that is not from type {1}.",
propertyLambda,
type));
return propInfo;
}
}
Using:
radioMale.DataBindings.Add("Checked",
new ComparisonBinder<DataClass,GenderEnum>(DataObj, (x) => x.Gender, GenderEnum.Male), "Value");
radioFemale.DataBindings.Add("Checked",
new ComparisonBinder<DataClass,GenderEnum>(DataObj, (x) => x.Gender, GenderEnum.Female), "Value");
source to share
I think it can be done with PropertyInfo
some lambda magic too:
class ComparisonBinder<TSource,TValue> : ExpressionBinder<bool>
{
public ComparisonBinder(TSource source, Expression<Func<TSource, bool>> propertyLambda, TValue comparisonValue) :base(null,null)
{
var propertyInfo = GetPropertyInfo<TSource,bool>(source, propertyLambda);
this.Getter = () => comarisonValue.Equals((TValue)propertyInfo.GetValue(source));
this.Setter = (bool value) => { if(value) propertyInfo.SetValue(source, comparisonValue); };
}
}
Using:
radioMale.DataBindings.Add("Checked",
new ComparisonBinder<ConfigClass, GenderEnum>(config, c => c.Gender, GenderEnum.Male),
"Value");
radioFemale.DataBindings.Add("Checked",
new ComparisonBinder<ConfigClass, GenderEnum>(config, c => c.Gender, GenderEnum.Female),
"Value");
I have not tested it, but I hope you can fix any errors and use this solution.
GetPropertyInfo (): Copied from fooobar.com/questions/10379 / ...
public PropertyInfo GetPropertyInfo<TSource, TProperty>(
TSource source,
Expression<Func<TSource, TProperty>> propertyLambda)
{
Type type = typeof(TSource);
MemberExpression member = propertyLambda.Body as MemberExpression;
if (member == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a method, not a property.",
propertyLambda.ToString()));
PropertyInfo propInfo = member.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a field, not a property.",
propertyLambda.ToString()));
if (type != propInfo.ReflectedType &&
!type.IsSubclassOf(propInfo.ReflectedType))
throw new ArgumentException(string.Format(
"Expresion '{0}' refers to a property that is not from type {1}.",
propertyLambda.ToString(),
type));
return propInfo;
}
source to share