本文介绍了静态成员和接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚经历了极少数的关于这一主题做题,发现这是(目前?)不能在接口定义静态成员或使静态方法虚拟的。我现在疲于应付的局面。让我尝试用一​​个简单的例子描述它:

I just went through a handful of SO questions on this topic and found out that this is (currently?) not possible to define static members in interfaces or make static methods virtual. I'm now struggling with a situation. Let me try to depict it with a simple example:

我有一个名为接口说 IAnimal 的定义如下:

I have an interface named say IAnimal with the following definition:

interface IAnimal {
    ...
    int Chromosomes {get; } //should ideally be static, but that's not allowed!
    ...
}

然后,我有一个基类动物,它实现 IAnimal 并提供执行共同所有动物的一些方法:

Then I have a base class Animal that implements IAnimal and provides implementation of some methods common to all animals:

abstract class Animal : IAnimal {
    ...
    public abstract int Chromosomes {get; } //necessary becuz of IAnimal
    ...
}

下面这个我那么有斑马动物继承的类,并为他们的物种提供这个属性的具体实现。

Below this I then have Dog, Cat and Zebra classes that inherit from Animal and provide concrete implementations of this property for their species.

现在的问题:这些类被发现在运行时(通过 Assembly.LoadFile() becuz可插拔模块的)。所发现的类都保存在一个名单,其中,System.Type的> ,我们以后用它来创建具体的狗和猫。对于建立一个动物的输入参数是染色体数目,因此,例如他们会问:创建具有72条染色体类型的动物的所以我需要以某种方式得到的染色体属性的值。每个键入列表键,返回第一个匹配的键入

Now the problem: These classes are discovered at runtime (through Assembly.LoadFile() becuz of the pluggable-modules). The discovered classes are kept in a List<System.Type> that we later use to create concrete dogs and cats. The input parameter for the creation of an animal is chromosomes number, so for example they'll ask: Create an animal of the type that has 72 chromosomes. So I need to somehow get the Chromosome property's value of each Type in the List and return the first matching Type.

由于染色体不是静态的(如果是这样,我可以很容易的一个静态成员),我是被迫创建每个类型的临时对象,然后调用染色体的,或者是有没有更好的办法?在我的列表中的元素保证为被以 IAnimal 如果是这样的帮助。

Since Chromosomes is not static (if it were, I could easily run a static member of a given Type), am I forced to create a temporary object of each type and then call Chromosomes with it, or is there a better way? All the elements in my List are guarenteed to be IAnimal if that is of help.

推荐答案

基本上,C#类型系统没有作好准备,为这一点。这不是往往的一个问题,但如果它确实提出了这是一个痛苦:(你可能要考虑一些选择,所有这些都被删除现有的属性开始:

Basically the C# type system isn't geared up for this. It's not often a problem, but when it does come up it's a pain :( Some options you might want to think about, all of which start off by removing the existing property:

  • 创建的类型单独类型层次结构本身( DogType AnimalType 等),这也可能是负责创建实例,如果这​​是很有用的。这往往是一个有点疼痛。
  • 添加属性的类型,表明染色体的数目 - 这只能当值当然是恒定的。然后,您可以找到属性值与反思。它不会在编译时虽然执行。
  • 请可与反射调用静态属性,然后添加单元测试,以确保每一个类型,它实现接口的的具有静态属性
  • Create a separate type hierarchy for the types themselves (DogType, AnimalType etc) which could also be responsible for creating instances if that's useful. That tends to be a bit of a pain.
  • Add an attribute to the type, indicating the number of chromosomes - this only works when the value is constant, of course. You can then find the attribute value with reflection. It won't be enforced at compile-time though.
  • Make a static property which can be invoked with reflection, and then add unit tests to ensure that every type which implements the interface also has the static property

这篇关于静态成员和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:13