110,538
社区成员
发帖
与我相关
我的任务
分享
public class MyComponent : Component, ISupportInitialize
{
public int Max { get; set; }
public int Min { get; set; }
public void BeginInit()
{
}
public void EndInit()
{
if (Max < Min) throw new InvalidOperationException("Max must not less then Min");
}
}
完整一些可以这样写:
public class MyComponent : Component, ISupportInitialize
{
int max;
public int Max
{
get { return max; }
set { ValidateMaxMin(value, min); max = value; }
}
int min;
public int Min
{
get { return min; }
set { ValidateMaxMin(max, value); min = value; }
}
bool initializing;
void ValidateMaxMin(int max, int min)
{
if (max < min && !initializing) throw new InvalidOperationException("Max must not less then Min");
}
public void BeginInit()
{
initializing = true;
}
public void EndInit()
{
initializing = false;
ValidateMaxMin(max, min);
}
}