CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2010
    Posts
    4

    Lazy loading Constructors

    I have a Lazy object whos Value has a constructor that I need to call.
    But I can't find a way to do it with out initializing the class at least once using a default constructor.
    Code:
                        if (plugin.IsValueCreated)
                        {
                            return Activator.CreateInstance(plugin.Value.GetType(), parameters);
                        }
                        else
                        {
                            //I need to figure out how to initilize the value with parameters..
                            return Activator.CreateInstance(plugin.Value.GetType(), parameters);
                        }
    After creating the value once, I can create a new one like above, but I would like to change it to make it so we don't have a useless image in memory...

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Lazy loading Constructors

    You can use the Activator.CreateInstance Method (String, String, Object[]) overload.
    I takes the assembly name and type name, that way you can make it configurable too. And no need at all to create an instance first.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Feb 2010
    Posts
    4

    Re: Lazy loading Constructors

    Is there a way to get the type of a Lazy's Value without initilizing it?
    I have a Lazy<Object> which I populate using using MEF imports.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Arrow Re: Lazy loading Constructors

    Lazy<SomeClass> x;


    x.GetType ().GetGenericParameters [0]

    The above (or something like it) will return "typeof (SomeClass)".

    For the original question:

    Type valueType = Type.Find ("Full.NameSpace.To.Value");

    Or something similar
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Feb 2010
    Posts
    4

    Re: Lazy loading Constructors

    That doesnt work because like I said, I have to use Lazy<Object>, so The type of the generic parameter is typeof(Object)
    As for using Type.Find I don't know the fill name space!
    I'm using MEF and importing lots of extensions.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Lazy loading Constructors

    As for using Type.Find I don't know the fill name space!
    The 'Type' class has methods for creating a Type object using just a string. This is what you want.

    That doesnt work because like I said, I have to use Lazy<Object>, so The type of the generic parameter is typeof(Object)
    Maybe it'd be possible to refactor things so you don't have to use Lazy<object> but can use strongly typed versions instead.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Feb 2010
    Posts
    4

    Re: Lazy loading Constructors

    Its impossible to use strongly typed vars instead because we are importing things from a arbitrary assembily.
    I can do something like:
    [ImportMany(typeof(PluginBase))]
    Collection(Lazy<PluginBase>) _plugins;

    [Export(typeof(PluginBase))]
    public class TestPlugin : PluginBase{}

    but if I try and get the type of any of the plugins, it'll return typeof(PluginBase) not typeof(TestPlugin);

    No use to me at all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured