I have the following code in a class library:
public class Manager
{
private static readonly Manager instance = new Manager();
public static IHelper Helper { get { return Manager.instance.helper; } }
[Import(typeof(IHelper))]
internal IHelper helper { get; set; }
private Manager()
{
using (DirectoryCatalog catalog =new DirectoryCatalog(@"c:\Dev\Plugins"))
{
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
}
I am using the above class to set custom user-interface settings at run-time, will the constructor run every time I access a member (will it scan the directory)?
An example call might be lblMask.Text = Helper.SearchMask;
The directory scanning only happens in the instance constructor. The instance constructor only happens when new Manager()
is used (assuming nobody cheats with reflection), which only happens once, in the static field initializer.
So: no. It should only happen once - the first time per app-domain.
However, you could just stick in a break-point / some kind of output, and find out...