Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PropMediator
- {
- public Dictionary<Type,IList> Hardware { get; set; }
- public PropMediator()
- {
- this.Hardware = new Dictionary<Type,IList>();
- }
- public void Add<T>(HardwareProp<T> hardware)
- {
- MethodInfo mi = this.GetType().GetMethod("AddIn", BindingFlags.NonPublic | BindingFlags.Instance);
- MethodInfo miConstructed = mi.MakeGenericMethod(hardware.GetType().GetGenericArguments()[0]);
- miConstructed.Invoke(this, new object[] { hardware.val });
- }
- private void AddIn<T>(T val)
- {
- if(!this.Hardware.ContainsKey(typeof(T)))
- {
- this.Hardware[typeof(T)] = new List<T>();
- }
- this.Hardware[typeof(T)].Add(val);
- }
- }
- public class HardwareProp<T>
- {
- public T val { get; set; }
- }
- public class SomeClass
- {
- public int i { get; set; }
- public SomeClass(int i)
- {
- this.i = i;
- }
- }
- void Main()
- {
- var mediator = new PropMediator();
- foreach( int i in Enumerable.Range(0,4).ToList() )
- {
- var prop = new HardwareProp<int>();
- prop.val = i;
- mediator.Add(prop);
- }
- foreach( int i in Enumerable.Range(0,4).ToList() )
- {
- var prop = new HardwareProp<string>();
- prop.val = "val" + i;
- mediator.Add(prop);
- }
- foreach( int i in Enumerable.Range(0,4).ToList() )
- {
- var prop = new HardwareProp<SomeClass>();
- prop.val = new SomeClass(i);
- mediator.Add(prop);
- }
- foreach( var item in mediator.Hardware )
- {
- Console.WriteLine(item.Key);
- foreach(var val in (IList)item.Value)
- {
- Console.WriteLine(val);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment