What about creating an event on the Manager
class, something like a RequestChangeSpecialData
event. The Manager
fires the event, and the SomeClass
will change the SpecialData
instance.
public class SomeClass
{
private Manager _m;
public Manager M
{
get { return _m}
set
{
// register/unregister event on property assignment
if(_m != null)
_m.RequestChangeSpecialData -= RequestChangeSpecialData;
_m = value;
if(_m != null)
_m.RequestChangeSpecialData += RequestChangeSpecialData;
}
}
public SpecialData Data { get; private set; }
private void RequestChangeSpecialData(object sender, ChangeSpecialDataEventArgs e)
{
// set the new reference
Data = e.SpecialData;
}
}
public class Manager
{
public void DoSomething()
{
// the manager class wants to do something, and wants to change the SpecialData instance.., so it fires the event RequestChangeSpecialData
SpecialData data = new SpecialData();
// if the event is bound.
if(RequestChangeSpecialData != null)
RequestChangeSpecialData(this, new ChangeSpecialDataEventArgs(data));
}
public event EventHandler<ChangeSpecialDataEventArgs> RequestChangeSpecialData;
}
public class ChangeSpecialDataEventArgs : EventArgs
{
public SpecialData Data {get; private set; }
public ChangeSpecialDataEventArgs(SpecialData Data)
{
Data = data;
}
}
UNTESTED (wrote in notepad)
Now the Manager
is able to change the SpecialData
property. This way the manager is not dependent from the SomeClass
/interface or assembly.
internal
is unsafe or unclean? Do you mean that it is so in general, or are you suggesting it's not suited to your specific design? I would say it's a perfect solution here, though truth be told I'm not sure why you want to do this in the first place. – Dwight