Is there a way to stipulate that the clients of a class should specify a value for a set of properties in a class. For example (see below code), Can i stipulate that "EmploymentType" property in Employment class should be specified at compile time? I know i can use parametrized constructor and such. I am specifically looking for outputting a custom warning or error during compile time. Is that possible?
public class Employment
{
public EmploymentType EmploymentType {get; set;}
}
public enum EmploymentType
{
FullTime = 1,
PartTime= 2
}
public class Client
{
Employment e = new Employment();
// if i build the above code, i should get a error or warning saying you should specify value for EmploymentType
}
-
You could achieve what you want to do by not having a default constructor, and instead defining a constructor that takes employment type as an argument. If someone attempted to instantiate the class using a parameter-less constructor, they would get a compile error.
EDIT code sample
public Employment(EmploymentType eType) { this.EmploymentType = eType; }
vzhnt2 : yes. you r rite on that. But what if in scenarios where a default constructor is required. Like for serialization? then the client may chose the default over the parametrized constructor.cmsjr : If you have to have a public default constructor, I don't know of a way that you can cause a compile time error, for what will be a run-time state. -
As cmsjr stated what you need to do is this:
public class Employment { public Employment(EmploymentType employmentType) { this.EmploymentType = employmentType; } public EmploymentType EmploymentType { get; set; } }
This will force callers to pass in the value at creation like this:
Employment e = new Employment(EmploymentType.FullTime);
In the situation where you need to have a default constructor (like serialization) but you still want to enforce the rule then you would need some sort of state validation. For instance anytime you attempt to perform an operation on the Employment class you can have it check for a valid state like this:
public EmploymentType? EmploymentType { get; set; } // Nullable Type public void PerformAction() { if(this.Validate()) // Perform action } protected bool Validate() { if(!EmploymentType.HasValue) throw new InvalidOperationException("EmploymentType must be set."); }
If you're looking throw custom compiler warnings, this is not exactly possible. I asked a similar question here Custom Compiler Warnings
cmsjr : +1 for more thorough code sample than mine ; ) -cmsjrvzhnt2 : yea.. I wanted to know whether there is a way to trigger custom error/warning based on state. Seems like its not possible. Your link to Custom Compiler Warnings was helpful. -
OO principles dictate that an object should never be in an invalid state. So this should be a constructor parameter.
There is no way to indicate that a property is required at compile time.
sixlettervariables : +1 right answer to the underlying question -
You mentioned in your original post that you already know about doing this with constructors, which is definitely the right way to do it. I don't believe there's going to be any way to do what you want, even with attributes (which is normally how you would manipulate compiler warnings, etc). Since the object could be created and then passed to another method to set the parameter, it's not necessarily as obvious as "client has to specify value".
You could try and create a custom FxCop or VSTS static analysis rule, but I don't think you'll be able to do this with just the C# compiler.
0 comments:
Post a Comment