![]() |
|
|
#1 |
|
Guest
Posts: n/a
|
Generic runtime instantiation
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: Generic runtime instantiation
Peter,
Well, you aren't going to be able to do what you want directly, since iface is an interface type, and you can't create interface types. However, you could do typeof(iface) to get the Type instance of the interface, and then based on that Type instance, map it to another Type instance which you can use to create the implementation through Reflection. -- - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "Peter K" <xdzgor@hotmail.com> wrote in message news:eoRL1EmPIHA.536@TK2MSFTNGP06.phx.gbl... > 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 |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Re: Generic runtime instantiation
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote
in news:uAWMnKmPIHA.4912@TK2MSFTNGP06.phx.gbl: > Well, you aren't going to be able to do what you want directly, > since > iface is an interface type, and you can't create interface types. > > However, you could do typeof(iface) to get the Type instance of > the > interface, and then based on that Type instance, map it to another > Type instance which you can use to create the implementation through > Reflection. Thanks for your reply. I admit I don't quite follow you - reflection & generics are both relatively new to me - so I'll need to investigate a little more. Note that I am not actually instantiating "iface" (at least I don't think I am, am I?) I am tring to instantiate a type like AVDeviceFactory<ITelevsion> So it is a concrete type, where "AVDeviceFactory" is declared like: public class AVDeviceFactory<T> : IDeviceFactory<T> At runtime I have strings like type="Alpha.DeviceFactory.AVDeviceFactory`1, Alpha.DeviceFactory" and iface="Alpha.Device.Contract.ITelevision, Alpha.Device.Contract" So I want to try to instantiate the class "AVDeviceFactory" based on these strings. Obviously this works: IDeviceFactory<ITelevision> tvFactory = new AVDeviceFactory<ITelevsion>(); So I was hoping I could somehow achieve this "dynamically". Thanks, Peter |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Re: Generic runtime instantiation
Peter,
In your example, if type represents the open generic type (AVDeviceFactory<T>) and iface represents ITelevision, then you can call GetType to get the Type instances. From there, you can create the closed generic type by calling the MakeGenericType method on the type that represents AVDeviceFactory<T>, passing the Type instance for ITelevision (which you would have obtained by calling the static GetType method, passing the string of the type name into it). Then, with that Type instance, you can pass that to the static CreateInstance method on the Activator class, and it will return your constructed type for you. -- - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "Peter K" <xdzgor@hotmail.com> wrote in message news:%23mL5EhnPIHA.4740@TK2MSFTNGP02.phx.gbl... > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote > in news:uAWMnKmPIHA.4912@TK2MSFTNGP06.phx.gbl: > >> Well, you aren't going to be able to do what you want directly, >> since >> iface is an interface type, and you can't create interface types. >> >> However, you could do typeof(iface) to get the Type instance of >> the >> interface, and then based on that Type instance, map it to another >> Type instance which you can use to create the implementation through >> Reflection. > > Thanks for your reply. I admit I don't quite follow you - reflection & > generics are both relatively new to me - so I'll need to investigate a > little more. > > Note that I am not actually instantiating "iface" (at least I don't think > I > am, am I?) > > I am tring to instantiate a type like > AVDeviceFactory<ITelevsion> > > So it is a concrete type, where "AVDeviceFactory" is declared like: > public class AVDeviceFactory<T> : IDeviceFactory<T> > > At runtime I have strings like > type="Alpha.DeviceFactory.AVDeviceFactory`1, Alpha.DeviceFactory" > > and > iface="Alpha.Device.Contract.ITelevision, Alpha.Device.Contract" > > > So I want to try to instantiate the class "AVDeviceFactory" based on these > strings. > > Obviously this works: > IDeviceFactory<ITelevision> tvFactory = new AVDeviceFactory<ITelevsion>(); > > So I was hoping I could somehow achieve this "dynamically". > > > Thanks, > Peter |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
| New To Site? | Need Help? |