J
Joanna Carter \(TeamB\)
Hi folks
I have a Generic Value Type and I want to detect when the internal value
changes.
///////////////////////////////
public delegate void ValueTypeValidationHandler<T>(T oldValue, T newValue);
public struct TestType<T>
{
private T value;
public event ValueTypeValidationHandler<T> valueChanged;
public ValueType(T value)
{
this.value = value;
valueChanged = null;
}
public T Value
{
get { return value; }
set
{
if(valueChanged != null)
valueChanged(this.value, value);
this.value = value;
}
}
}
//////////////////////
I want to be able to add TestType<> fields to a containing class and have
the containing class assign a delegate to each of the TestType<> fields
without naming them specifically, in the constructor of the containing
class.
So far I have got this far but I want to know how to attach the delegates to
the events.
/////////////////////////
public ContainingType()
{
FieldInfo[] fia = GetType().GetFields(BindingFlags.Instance |
BindingFlags.NonPublic);
foreach(FieldInfo fi in fia)
{
Type ft = fi.FieldType;
object fObj = fi.GetValue(this);
// the next line doesn't compile; how do I pass the type of each field into
the
generic parameters ?
((TestType<ft>) fObj).valueChanged += new
ValueTypeValidationHandler<ft>(HandleValueChanged<ft>);
}
////////////////////////////
TIA
Joanna
I have a Generic Value Type and I want to detect when the internal value
changes.
///////////////////////////////
public delegate void ValueTypeValidationHandler<T>(T oldValue, T newValue);
public struct TestType<T>
{
private T value;
public event ValueTypeValidationHandler<T> valueChanged;
public ValueType(T value)
{
this.value = value;
valueChanged = null;
}
public T Value
{
get { return value; }
set
{
if(valueChanged != null)
valueChanged(this.value, value);
this.value = value;
}
}
}
//////////////////////
I want to be able to add TestType<> fields to a containing class and have
the containing class assign a delegate to each of the TestType<> fields
without naming them specifically, in the constructor of the containing
class.
So far I have got this far but I want to know how to attach the delegates to
the events.
/////////////////////////
public ContainingType()
{
FieldInfo[] fia = GetType().GetFields(BindingFlags.Instance |
BindingFlags.NonPublic);
foreach(FieldInfo fi in fia)
{
Type ft = fi.FieldType;
object fObj = fi.GetValue(this);
// the next line doesn't compile; how do I pass the type of each field into
the
generic parameters ?
((TestType<ft>) fObj).valueChanged += new
ValueTypeValidationHandler<ft>(HandleValueChanged<ft>);
}
////////////////////////////
TIA
Joanna