向矩形类添加自定义属性 [英] Adding custom property to the rectangle class

查看:39
本文介绍了向矩形类添加自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向 Rectangle 类添加一个自定义属性.我试图子类化 Rectangle 类,但没有用.有没有办法做到这一点.谢谢

I need to add a custom property to the Rectangle class. I tried to subclass the Rectangle class but it didn't work. Is there a way to do that. Thanks

推荐答案

你不能从 System.Drawing.Rectangle 继承,因为它不是一个类,它是一个 struct代码>.您可以做的是创建自己的类(或 struct)来包装 Rectangle,例如:

You can't inherit from System.Drawing.Rectangle, because it's not a class, it's a struct. What you can do is to create your own class (or struct) that wraps Rectangle, something like:

class MyRectangle
{
    public Rectangle ActualRectangle { get; set; }
    public SomeType AdditionalProperty { get; set; }
}

或者您可以隐藏 Rectangle 并提供与调用它们的 Rectangle 的方法和属性相同的方法和属性.

Or you could hide the Rectangle and provide methods and properties that mirror those of Rectangle that call them.

这篇关于向矩形类添加自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆