P
Peter K
Hi
I have an interface for a "factory" defined like this:
public interface IDeviceFactory<T> : IDisposable
{
T GetDevice();
}
Then I can define various factories like:
public class DefaultDeviceFactory<T> : IDeviceFactory<T>
{
...
}
public class AVDeviceFactory<T> : IDeviceFactory<T>
{
...
}
And from a program I can instantiate these "device factories" in the
following fashion:
IDeviceFactory<ITelevision> tvDeviceFactory = new AVDeviceFactory
<ITelevsion>();
and call
ITelevision tv = tvDeviceFactory.GetDevice();
(The AVDeviceFactory<T> object knows how to instantiate the appropriate
"devices").
But how can I write a "generic" factory instantiation function? I know
the interface "IDeviceFactory" at compile time, but the particular type T
I only know at runtime. Is this possible?
I mean, how do I write a method like the following, where I know the
"factory type" (eg AVDeviceFactory) and the interface it should return
(eg ITelevision) at runtime.
public IDeviceFactory<iface> InstantiateDeviceFactory(string type, string
iface)
{
IDeviceFactory<iface> factory = new type<iface>();
return factory;
}
Thanks,
Peter
I have an interface for a "factory" defined like this:
public interface IDeviceFactory<T> : IDisposable
{
T GetDevice();
}
Then I can define various factories like:
public class DefaultDeviceFactory<T> : IDeviceFactory<T>
{
...
}
public class AVDeviceFactory<T> : IDeviceFactory<T>
{
...
}
And from a program I can instantiate these "device factories" in the
following fashion:
IDeviceFactory<ITelevision> tvDeviceFactory = new AVDeviceFactory
<ITelevsion>();
and call
ITelevision tv = tvDeviceFactory.GetDevice();
(The AVDeviceFactory<T> object knows how to instantiate the appropriate
"devices").
But how can I write a "generic" factory instantiation function? I know
the interface "IDeviceFactory" at compile time, but the particular type T
I only know at runtime. Is this possible?
I mean, how do I write a method like the following, where I know the
"factory type" (eg AVDeviceFactory) and the interface it should return
(eg ITelevision) at runtime.
public IDeviceFactory<iface> InstantiateDeviceFactory(string type, string
iface)
{
IDeviceFactory<iface> factory = new type<iface>();
return factory;
}
Thanks,
Peter