An interesting question, which I've voted-up out of curiosity.
And, I'll respond to it "in depth" because what you are dealing with here indirectly relates to some experiments I am doing with using interfaces: so it's inherently of value to me to try and implement a test case based on your ideas.
These comments (too long for just a standard thread comment, and they include code which I want displayed formatted) are offered here
not because I believe I have a "solution" for your design scenario (I don't pretend to understand it fully), but with the hope they may, at least, contribute to clarification of the question asked, or be of some benefit to other CP members.
One thing that immediately comes to mind: is that since you are using a Type object as the key to a Dictionary: that means one-and-only-one Type is going be able to be added to the Dictionary (you'll get a duplicate key error if you add more than one of any Type).
If there's one-and-only-one instance of each Type: why use a Dictionary here ?
Which raises a secondary question, for me: since you want one Dictionary entry per instance of object (you are now passing in the objects' instances for the 'Value argument in the Dictionary): don't you want to use a Dictionary<obj<T>, Type>) ?
In which case 'AddService now might look like:
public static void AddService<T>(T obj)
{
Services.Add(obj, typeof(T));
}
I did create a simple test example using this approach in which I verified that it would compile, using this for a test interface:
using System.Windows.Forms;
using System.Drawing;
public interface IWhatEver
{
Size bSize { get; set;}
Point bLoc { get; set; }
}
Then, I could make a calls like this;
private void AddModButton(IWhatEver theMButton)
{
ServiceManager.AddService(theMButton);
}
Prior to testing, I added a method to the ServiceManager class, to expose the Services Dictionary:
public static Dictionary<object, Type> GetServices()
{
return Services;
}
The code I used to test this little sandbox experiment was
ServiceManager.AddService(new Button());
ServiceManager.AddService(new ModButton(bName: "inherited Button"));
ServiceManager.AddService(new TextBox());
ServiceManager.AddService(new ListBox());
ModButton mb2 = new ModButton(bName: "second inherited Button");
AddModButton(mb2 as IWhatEver);
IWhatEver mb3 = new ModButton(bName: "third inherited Button");
AddModButton(mb3);
Then, I examined the contents of the Services Dictionary like this:
private void InspectServices()
{
Console.WriteLine();
foreach (var theKVP in ServiceManager.GetServices())
{
Console.WriteLine("object = "
+ theKVP.Key.ToString()
+ " : type = "
+ theKVP.Value.ToString());
if (theKVP.Value.BaseType != null)
{
Console.WriteLine("base class = "
+ theKVP.Value.BaseType.ToString());
}
else if (theKVP.Key is IWhatEver)
{
Console.WriteLine("This is an IWhatEver interface");
Console.WriteLine("This IWhatEver cast back to Button is : "
+ (theKVP.Key as Button).ToString()
+ " : Name = "
+ (theKVP.Key as Button).Name);
}
Console.WriteLine();
}
}