Generic runtime instantiation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter K

    #1

    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 DefaultDeviceFa ctory<T: IDeviceFactory< T>
    {
    ...
    }

    public class AVDeviceFactory <T: IDeviceFactory< T>
    {
    ...
    }


    And from a program I can instantiate these "device factories" in the
    following fashion:

    IDeviceFactory< ITelevisiontvDe viceFactory = 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< ifaceInstantiat eDeviceFactory( string type, string
    iface)
    {
    IDeviceFactory< ifacefactory = new type<iface>();
    return factory;
    }



    Thanks,
    Peter
  • Nicholas Paldino [.NET/C# MVP]

    #2
    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.co m


    "Peter K" <xdzgor@hotmail .comwrote in message
    news:eoRL1EmPIH A.536@TK2MSFTNG P06.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 DefaultDeviceFa ctory<T: IDeviceFactory< T>
    {
    ...
    }
    >
    public class AVDeviceFactory <T: IDeviceFactory< T>
    {
    ...
    }
    >
    >
    And from a program I can instantiate these "device factories" in the
    following fashion:
    >
    IDeviceFactory< ITelevisiontvDe viceFactory = 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< ifaceInstantiat eDeviceFactory( string type, string
    iface)
    {
    IDeviceFactory< ifacefactory = new type<iface>();
    return factory;
    }
    >
    >
    >
    Thanks,
    Peter

    Comment

    • Peter K

      #3
      Re: Generic runtime instantiation

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
      in news:uAWMnKmPIH A.4912@TK2MSFTN GP06.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 "AVDeviceFactor y" is declared like:
      public class AVDeviceFactory <T: IDeviceFactory< T>

      At runtime I have strings like
      type="Alpha.Dev iceFactory.AVDe viceFactory`1, Alpha.DeviceFac tory"

      and
      iface="Alpha.De vice.Contract.I Television, Alpha.Device.Co ntract"


      So I want to try to instantiate the class "AVDeviceFactor y" based on these
      strings.

      Obviously this works:
      IDeviceFactory< ITelevisiontvFa ctory = new AVDeviceFactory <ITelevsion>( );

      So I was hoping I could somehow achieve this "dynamicall y".


      Thanks,
      Peter

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Generic runtime instantiation

        Peter,

        In your example, if type represents the open generic type
        (AVDeviceFactor y<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.co m

        "Peter K" <xdzgor@hotmail .comwrote in message
        news:%23mL5EhnP IHA.4740@TK2MSF TNGP02.phx.gbl. ..
        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
        in news:uAWMnKmPIH A.4912@TK2MSFTN GP06.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 "AVDeviceFactor y" is declared like:
        public class AVDeviceFactory <T: IDeviceFactory< T>
        >
        At runtime I have strings like
        type="Alpha.Dev iceFactory.AVDe viceFactory`1, Alpha.DeviceFac tory"
        >
        and
        iface="Alpha.De vice.Contract.I Television, Alpha.Device.Co ntract"
        >
        >
        So I want to try to instantiate the class "AVDeviceFactor y" based on these
        strings.
        >
        Obviously this works:
        IDeviceFactory< ITelevisiontvFa ctory = new AVDeviceFactory <ITelevsion>( );
        >
        So I was hoping I could somehow achieve this "dynamicall y".
        >
        >
        Thanks,
        Peter

        Comment

        Working...