Example:
I have an user control with 1 property (that one editable through Property Grid) called “Title”.
There is a way to throw an compile-time exception if the property “Title” is empty? Or all design-time properties is optional?
Example: I have an user control with 1 property (that one editable through Property
Share
There is no way at compile time to ensure a particular property is set. You can use the #error directive to cause a compile time error or #warning for a compile time warning if code is compiled but this is not what you want.
There are two ways of ensuring a particular property or method is set. The first is to require it in a constructor or check that it has been set in a subsequent method or property. This is a runtime check, not compile time and, as you say, you have certain restrictions in a UserControl.
The second way is to use a code analysis tool like FXCop. You could write a custom rule that identifies all instances of your UserControl and ensures the property has been set on all instances. It may also be possible with Roslyn or PostSharp but I am not familiar enough with those tools to say so.
The problem with code analysis tools like FXCop is there is a rather steep learning curve. Programming languages are complex and these tools need to expose the complexity. My recommendation is use a default value like “insert title here” or, in the unlikely even that there is no suitable default, throw an exception when the control is rendered with a descriptive message.
See Good way to ensure that a property on a UserControl gets set? for a similar case.