Generic runtime instantiation

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
P

Peter K

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top