【发布时间】:2015-11-07 05:03:11
【问题描述】:
如果某个方法处于设计时或该方法只能在运行时执行,是否有办法阻止该方法执行。
我在创建自定义控件时遇到了这个问题,因为构造函数中有一个仅在运行时有效的方法调用。
现在在设计时在设计表单并使用该控件时,表单将产生错误。 现在我在用户控件的构造函数中尝试了这个
public ctrl_information()
{
InitializeComponent();
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;
SomeMethod();
//Other code
}
我现在想要达到的目标是这样的。
[ExecuteOnlyAtRuntime]
public void SomeMethod()
{
//Code here
}
那就这样称呼吧。
public ctrl_information()
{
InitializeComponent();
//if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return; -- removing this line
SomeMethod();
}
有可能吗?
请对此有所了解。
谢谢
【问题讨论】:
-
为包含参数的类创建第二个构造函数。传递 LicenseManager.UsageMode 或 LicenseUsageMode.DesignTime 作为参数。
-
你用的是winform吗?
-
是的,我正在使用 winforms
-
它被否决的任何原因?
标签: c# methods attributes