"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...
What exactly do you mean by "actual object"?
I want to assign a delegate to an event on a value type inside a method of a
base containing class that does not know what fields are going to be added
in the sub-classes
I'm afraid I don't understand exactly what you mean. Could you post a
short but complete program which demonstrates the problem?
///////////////////////////////
public struct TestType<T>
{
public delegate void ValueTypeValidationHandler(T oldValue, T newValue);
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[] fieldInfos = GetType().GetFields(BindingFlags.Instance |
BindingFlags.NonPublic);
foreach(FieldInfo fi in fieldInfos)
{
Type ft = fi.FieldType;
object fObj = fi.GetValue(this);
((TestType<ft>) fObj).valueChanged += new
ValueTypeValidationHandler<ft>(HandleValueChanged< ft>);
}
////////////////////////////
Joanna
--
Joanna Carter
Consultant Software Engineer