473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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<ITelevisiontvDeviceFactory = new AVDeviceFactory
<ITelevsion>();

and call

ITelevision tv = tvDeviceFactory.GetDevice();

(The AVDeviceFactory<Tobject 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<ifaceInstantiateDeviceFactory(strin g type, string
iface)
{
IDeviceFactory<ifacefactory = new type<iface>();
return factory;
}

Thanks,
Peter
Dec 14 '07 #1
3 2143
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]
- mv*@spam.guard.caspershouse.com
"Peter K" <xd****@hotmail.comwrote in message
news:eo*************@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<ITelevisiontvDeviceFactory = new AVDeviceFactory
<ITelevsion>();

and call

ITelevision tv = tvDeviceFactory.GetDevice();

(The AVDeviceFactory<Tobject 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<ifaceInstantiateDeviceFactory(strin g type, string
iface)
{
IDeviceFactory<ifacefactory = new type<iface>();
return factory;
}

Thanks,
Peter

Dec 14 '07 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in news:uA**************@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<ITelevisiontvFactory = new AVDeviceFactory<ITelevsion>();

So I was hoping I could somehow achieve this "dynamically".
Thanks,
Peter
Dec 14 '07 #3
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]
- mv*@spam.guard.caspershouse.com

"Peter K" <xd****@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in news:uA**************@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<ITelevisiontvFactory = new AVDeviceFactory<ITelevsion>();

So I was hoping I could somehow achieve this "dynamically".
Thanks,
Peter

Dec 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
2
by: Jasper Kent | last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to avoid long instantiation names. I can do this okay: using BigDictionary = System.Collections.Generic.Dictionary<int,...
4
by: Cool Guy | last post by:
I don't understand the third paragraph under the heading 'Generic type instantiations' on <http://msdn.microsoft.com/vcsharp/2005/overview/language/generics/>: | The .NET Common Language Runtime...
0
by: Mikkel Blanné | last post by:
I haven't been able to find any references to using this combination of technologies (remoting + generic methods + method overloading). I don't think the problem has to do with C#, but I couldn't...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: Johs | last post by:
I have read that when you are using templates you are making generic programs. But I don't see whats so generic about templates. You can make generic programs without templates through the use of...
3
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
11
by: paul.gibson | last post by:
A simple code example is easier than trying to describe the issue. I have: public class myClassA {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.