提供了继承静态属性(概念) [英] Providing Inherited Static Properties (conceptually)

查看:121
本文介绍了提供了继承静态属性(概念)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#基类,我是将信息与每个类型(而不是每个实例)的基础上关联。基本上我希望所有子类呈现一个图标表示该类型的FriendlyName。我想的的必须创建一个类型的实例来获取此信息,我想要求所有子类提供此信息。理想情况下是在该基地从派生的接口。

I have a C# base class that I was to associate information with on a per-type (rather than per-instance) basis. Essentially I want all child classes to present an Icon and a FriendlyName that represents the type. I'd like to not have to create a type instance to get this information and I want to require that all child classes provide this information. Ideally it would be in the interface that the base derives from.

我试图用一个类属性,但它需要一个恒定值,因此,虽然我可以设置的FriendlyName ,我不能设置图标,我看不到一个方法,使反正所需的属性,所以子类可以与没有它(直到运行时)离开。

I tried to use a class Attribute, but it requires a constant value, so while I can set the FriendlyName, I can't set the Icon, and I can't see a way to make the Attribute required anyway, so child classes could get away with not having it (until run time).

我添加了一个静态属性,以孩子可以隐藏基地,但是这是一种丑陋,从基本的价值观真的很荒谬的。它可以不知道名字会是怎样的一个孩子。我可以在属性的基础扔,但同样,这将不会被抓到,直到运行时间。

I added a static Property to the base that children can hide, but that's kind of ugly and the values from the base are really nonsensical. It can't know what the name would be for a child. I could have the base throw in the Property, but again, that wouldn't get caught until run time.

任何人有我如何实现这样的事情任何想法?有没有我缺少一个模式? 。任何超出现成思维鼓励以及

Anyone have any ideas on how I might implement something like this? Is there a pattern I'm missing? Any out-of-the-box thinking is encouraged as well.

编辑:真的是我需要,要求类的能力以及属性继承树并能够使用嵌入资源来设置的属性。所有这一切都将返回真的是静态的,在编译期已知的信息。我只是不能想出一个办法很好地揭露它

Really what I need to the ability to require class Attributes along an inheritance tree and be able to use an embedded resource to set those attributes. All of the information that will be returned really is static and known at compile. I just can't figure out a way to nicely expose it.

编辑2:这里是什么,我需要一个更具体的例子:

Edit 2: Here's a more concrete example of what I need:

让我们假设我有一个基类:

Let's assume I have a base class:

abstract class Vehicle { ... }

然后我有两个孩子:

class Car : Vehicle { ... }

class Truck : Vehicle { ... }

我希望能够得到代表汽车或卡车的图标,而无需创建一个实例(一拉Car.Icon)因为给定类型的所有车辆都将有相同的图标。

I'd like to be able to get an Icon representing a "Car" or a "Truck" without having to create an instance of one (a la Car.Icon) since all vehicles of a given type will all have the same icon.

我也希望确保的所有的车辆派生类型在未来公开相同的方式来得到这个图标,万一有人出现了,增加了一个新的类

I also want to ensure that all Vehicle-derived Types expose the same way to get this Icon in case someone comes along in the future and adds a new class

class Airplane : Vehicle  { ... }

修改3:为什么我需要这个?好了,我们有一个应用程序,可以利用的插件。在插头由类型限定,并且装入的类型时,我们会显示用户的图标和名称。当他们单击该图标,它会创建一个类型的实例(并让他们有一个对话框命名实例等)。我不需要或不希望有创建类型,以获得图标的一个实例。

Edit 3: Why do I need this? Well we have an app that can take plug-ins. The plug in is defined by a type, and when the type is loaded, we show the user an icon and name. When they click the icon, it creates an instance of that type (and gives them a dialog for naming the instance, etc). I don't need or want to have to create an instance of the type to get the icon.

推荐答案

我想点你可能错过的是,你不能一个很好的理由改写静态属性 - 即当你编写代码来调用它,你没有一个instace,所以编译器将静态当时挂靠 - - 如果你需要多态,你需要用一个实例来工作,运行工作了调用该方法的哪个版本

I think the point you may have missed is that you can't override static properties for a good reason -- namely that when you're writing code to call it, you don't have an instace, so the complier will statically link it at that time -- if you need polymorphism, you need to be working with an instance for the runtime to work out which version of the method to call.

编辑:(在回答你的EDIT2)

(In response to your EDIT2)

你想什么情况下被调用此?

What scenario would you want to be calling this from?

如果你想展示你对一个实例的图标,你已经有了一个实例,那么为什么不把它作为实例属性(因为,毕竟,有人可能会覆盖你的来创建一个跑车,并希望为它不同的图标) - 实际上你要求为你的对象的属性,尽管它相匹配的概念模型(所以它不是车轮的数量,最大速度等;但事情把它想象到最终用户)

If you're wanting to show your icon against an instance, you've got an instance, so why not have it as a property on the instance (because, after all, somebody might override your Car to create a SportsCar and want a different icon for it) -- in effect you're asking for a property of your object, albeit one that matches the conceptual model (so it's not the number of wheels, the max speed, etc; but something to visualise it to the end user)

EDIT2:(在回答你的EDIT3)

(In response to your EDIT3)

这种方法我会做会使用属性,以确定其嵌入的资源作为各个类型(所以你并不需要一个实例)的图标使用沿着线的东西,并使用反射来读取的值。你可以定义自己的自定义属性,还是看的 ToolboxItemAttribute 其中的可能的是有用的。

The approach I'd do would be something along the lines using an attribute to identify which embedded resource to use as the icon for the respective type (so you don't need an instance), and use reflection to read the value. You could define your own custom attribute, or look at ToolboxItemAttribute which might be of use.

这篇关于提供了继承静态属性(概念)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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