What’s the best way to avoid (or control) the initialisation by the designer of a heavy custom property in .NET? Sometimes it is important to have a property set to something initially without that setting being acted upon when initially set.
In the imaginary example below, I want to achieve the flexibility of having something like UpdateSetting as a property, but not the inconvenience of having the database set to zero every time the application starts up. All I can think of is another property that unlocks a flag.
public class A : UserControl
{
public int UpdateSetting
{
// Write to database or some such thing
}
...
public void InitializeComponent()
{
A a = new A();
a.UpdateSetting = 0; // Causes database write
}
}
Assuming you control the component (the A class), apply DesignerSerializationVisibilityAttribute to the expensive property, with the Hidden option to say “don’t generate a setter for this property”:
(EDIT: On re-reading I’m not sure whether you want to prevent initialisation by the designer, or make it optional, or allow it to be done through the designer but defer the actual execution of the initialisation. This addresses only the first of those scenarios.)