I am trying to convert a class I have to generics and I can't seem to find
any possible why to implement it. I'm beginning to think I'm doing something
I shouldn't or I hit generics limitation.
Here is an example of the old way:
static void Main(string[] args)
{
MonitorCenter monitor = new MonitorCenter() ;
monitor.SetMoni tor(new ExceptionMonito r());
monitor.SetMoni tor(new StringMonitor() );
}
}
public class MonitorCenter
{
public void SetMonitor(IMon itor monitor)
{
_monitors.Add(m onitor);
}
public Collection<obje ctExecuteAllMon itors()
{
Collection<obje ctallMonitorRet urns = new Collection<obje ct>();
foreach (IMonitor monitor in _monitors)
{
Collection<obje ctobjs = monitor.Execute Monitor();
foreach (object obj in objs)
allMonitorRetur ns.Add(obj);
}
return allMonitorRetur ns;
}
Collection<obje ct_monitors = new Collection<obje ct>();
}
public interface IMonitor
{
Collection<obje ctExecuteMonito r();
}
public class ExceptionMonito r : IMonitor
{
public Collection<obje ctExecuteMonito r()
{
Collection<obje ctexceptions = new Collection<obje ct>();
exceptions.Add( new Exception("exce ption test"));
return exceptions;
}
}
public class StringMonitor : IMonitor
{
public Collection<obje ctExecuteMonito r()
{
Collection<obje ctmsgs = new Collection<obje ct>();
msgs.Add("strin g test");
return msgs;
}
}
Here is the new way which for obvious reasons it will never compile:
public class MonitorCenter
{
public void SetMonitor<T>(I Monitor<Tmonito r)
{
_monitors.Add(m onitor);
}
public Collection<TExe cuteAllMonitors ()
{
//once again because I want to have
//different types in the collection,
//I can't use this.
Collection<Tall MonitorReturns = new Collection<T>() ;
foreach (IMonitor<Tmoni tor in _monitors)
{
Collection<Tobj s = monitor.Execute Monitor();
foreach (T obj in objs)
allMonitorRetur ns.Add(obj);
}
return allMonitorRetur ns;
}
//don't know what type to make T???
Collection<T_mo nitors = new Collection<T>() ;
}
public interface IMonitor<T>
{
Collection<TExe cuteMonitor();
}
public class ExceptionMonito r : IMonitor<Except ion>
{
public Collection<Exce ptionExecuteMon itor()
{
Collection<Exce ptionexceptions = new Collection<Exce ption>();
exceptions.Add( new Exception("exce ption test"));
return exceptions;
}
}
public class StringMonitor : IMonitor<String >
{
public Collection<stri ngExecuteMonito r()
{
Collection<stri ngmsgs = new Collection<stri ng>();
msgs.Add("strin g test");
return msgs;
}
}
Seems like the only why I would be able to make this work is my
MonitorCenter a generic type which limits me to 1 type per instance, which I
don't want. Anyway, if anyone know a way around my issue please let me know.
However, i'm starting to think I will not be able to turn IMonitor into a
generic type.
any possible why to implement it. I'm beginning to think I'm doing something
I shouldn't or I hit generics limitation.
Here is an example of the old way:
static void Main(string[] args)
{
MonitorCenter monitor = new MonitorCenter() ;
monitor.SetMoni tor(new ExceptionMonito r());
monitor.SetMoni tor(new StringMonitor() );
}
}
public class MonitorCenter
{
public void SetMonitor(IMon itor monitor)
{
_monitors.Add(m onitor);
}
public Collection<obje ctExecuteAllMon itors()
{
Collection<obje ctallMonitorRet urns = new Collection<obje ct>();
foreach (IMonitor monitor in _monitors)
{
Collection<obje ctobjs = monitor.Execute Monitor();
foreach (object obj in objs)
allMonitorRetur ns.Add(obj);
}
return allMonitorRetur ns;
}
Collection<obje ct_monitors = new Collection<obje ct>();
}
public interface IMonitor
{
Collection<obje ctExecuteMonito r();
}
public class ExceptionMonito r : IMonitor
{
public Collection<obje ctExecuteMonito r()
{
Collection<obje ctexceptions = new Collection<obje ct>();
exceptions.Add( new Exception("exce ption test"));
return exceptions;
}
}
public class StringMonitor : IMonitor
{
public Collection<obje ctExecuteMonito r()
{
Collection<obje ctmsgs = new Collection<obje ct>();
msgs.Add("strin g test");
return msgs;
}
}
Here is the new way which for obvious reasons it will never compile:
public class MonitorCenter
{
public void SetMonitor<T>(I Monitor<Tmonito r)
{
_monitors.Add(m onitor);
}
public Collection<TExe cuteAllMonitors ()
{
//once again because I want to have
//different types in the collection,
//I can't use this.
Collection<Tall MonitorReturns = new Collection<T>() ;
foreach (IMonitor<Tmoni tor in _monitors)
{
Collection<Tobj s = monitor.Execute Monitor();
foreach (T obj in objs)
allMonitorRetur ns.Add(obj);
}
return allMonitorRetur ns;
}
//don't know what type to make T???
Collection<T_mo nitors = new Collection<T>() ;
}
public interface IMonitor<T>
{
Collection<TExe cuteMonitor();
}
public class ExceptionMonito r : IMonitor<Except ion>
{
public Collection<Exce ptionExecuteMon itor()
{
Collection<Exce ptionexceptions = new Collection<Exce ption>();
exceptions.Add( new Exception("exce ption test"));
return exceptions;
}
}
public class StringMonitor : IMonitor<String >
{
public Collection<stri ngExecuteMonito r()
{
Collection<stri ngmsgs = new Collection<stri ng>();
msgs.Add("strin g test");
return msgs;
}
}
Seems like the only why I would be able to make this work is my
MonitorCenter a generic type which limits me to 1 type per instance, which I
don't want. Anyway, if anyone know a way around my issue please let me know.
However, i'm starting to think I will not be able to turn IMonitor into a
generic type.
Comment