-
February 13th, 2010, 04:03 AM
#1
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...
-
February 13th, 2010, 04:38 AM
#2
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.
-
February 13th, 2010, 04:59 AM
#3
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.
-
February 13th, 2010, 07:02 AM
#4
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.
-
February 13th, 2010, 05:31 PM
#5
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.
-
February 13th, 2010, 07:16 PM
#6
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.
-
February 13th, 2010, 07:44 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|