J
Jeff Louie
KJ...With .NET 2.0 you can implement delegation using generics!
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericMI
{
// first we create the abstractions
public interface I1
{
void SayHello();
}
public interface I2
{
int GetValue();
}
public interface IProgram : I1, I2 { }
// second we write the concrete classes that implement the
abstractions
public class Implementation1 : I1
{
public void SayHello()
{
Console.WriteLine("Hello.");
}
}
public class Implementation2 : I2
{
private int i = 1;
public int GetValue()
{
return i;
}
}
// finally we write the wrapper class that contains
// the concrete classes
public class GenericMI<T1, T2> : IProgram where T1 : class, I1,
new()
where T2: class,I2, new()
{
private T1 pimplI1= new T1();
private T2 pimplI2 = new T2();
// we forward calls to the contained object
public void SayHello()
{
pimplI1.SayHello();
}
public int GetValue()
{
return pimplI2.GetValue();
}
}
class Program
{
static void Main(string[] args)
{
GenericMI<Implementation1,Implementation2> mi=
new GenericMI<Implementation1,Implementation2>();
mi.SayHello();
Console.WriteLine(mi.GetValue());
Console.ReadLine();
}
}
}
Regards,
Jeff
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericMI
{
// first we create the abstractions
public interface I1
{
void SayHello();
}
public interface I2
{
int GetValue();
}
public interface IProgram : I1, I2 { }
// second we write the concrete classes that implement the
abstractions
public class Implementation1 : I1
{
public void SayHello()
{
Console.WriteLine("Hello.");
}
}
public class Implementation2 : I2
{
private int i = 1;
public int GetValue()
{
return i;
}
}
// finally we write the wrapper class that contains
// the concrete classes
public class GenericMI<T1, T2> : IProgram where T1 : class, I1,
new()
where T2: class,I2, new()
{
private T1 pimplI1= new T1();
private T2 pimplI2 = new T2();
// we forward calls to the contained object
public void SayHello()
{
pimplI1.SayHello();
}
public int GetValue()
{
return pimplI2.GetValue();
}
}
class Program
{
static void Main(string[] args)
{
GenericMI<Implementation1,Implementation2> mi=
new GenericMI<Implementation1,Implementation2>();
mi.SayHello();
Console.WriteLine(mi.GetValue());
Console.ReadLine();
}
}
}
Regards,
Jeff