C# PropertyGrid 和 Interface 属性

发布于 2024-09-19 11:06:00 字数 259 浏览 6 评论 0原文

问题: 当对象具有:

class A
{
    public ISomeinterface PropertyName { get; set; }
}

然后将该类的实例分配给 propertyGrid.SelectedObject = new A(); 然后,当尝试编辑 PropertyName 的值时,会显示有关无法创建 ISomeinterface 实例的异常(当然是有道理的),问题是如何在不破坏我的类和接口的情况下解决此问题。

Problem:
when object is having:

class A
{
    public ISomeinterface PropertyName { get; set; }
}

and then an instance of that class is assigned to propertyGrid.SelectedObject = new A();
then when trying to edit the value of PropertyName, an exception about fail to make instance of ISomeinterface is shown (make sense of course) the question is how to workaround this without break my class's and interfaces.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2024-09-26 11:06:01

是的,PropertyGrid 无法猜测如何分配该值。因此,隐藏它:

class A
{
    [Browsable(false)]
    public ISomeinterface PropertyName { get; set; }
}

如果需要属性分配,那么您需要为该属性实现 UITypeEditor。

Right, PropertyGrid has no hope of guessing how to assign the value. So, hide it:

class A
{
    [Browsable(false)]
    public ISomeinterface PropertyName { get; set; }
}

If property assignment is a requirement then you'll need to implement a UITypeEditor for the property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文