I would do something like this...
Master.cs:
Code:
public class Master
{
private static Master _Instance;
public static Master Instance { get { if (_Instance == null) _Instance = new Master(); return _Instance; } }
private List<BaseClass> _ObjectList = new List<BaseClass>();
public void LinkIn(BaseClass obj)
{
for(int i = 0;i < _ObjectList.Count;++i)
{
if (_ObjectList[i].GetType() == obj.GetType())
{
_ObjectList.RemoveAt(i);
break;
}
}
_ObjectList.Add(obj);
}
public T GetObject<T>() where T: BaseClass
{
foreach (BaseClass o in _ObjectList)
if (o is T)
return o as T;
return null;
}
}
BaseClass.cs:
Code:
public class BaseClass
{
}
Class1:
Code:
public class Class1: BaseClass
{
public Class1()
{
Master.Instance.LinkIn(this);
}
public void DoSomethingInClass2()
{
Class2 obj = Master.Instance.GetObject<Class2>();
// Do stuff with the Class2 obj
}
}
Class2:
Code:
public class Class2: BaseClass
{
public Class2()
{
Master.Instance.LinkIn(this);
}
public void DoSomethingInClass1()
{
Class1 obj = Master.Instance.GetObject<Class1>();
// Do stuff with the Class1 obj
}
}
I did this in VS 2010 and it worked great. I don't know much about the Mono feature set, but generics have been around since C# 2.0, so I'm guessing Mono supports them by now.
NOTE: If your classes already need to be derived from another base class, then the BaseClass that I made can simply be turned into an interface instead.