简体   繁体   English

是否有结构继承的实用替代方法? (C#)

[英]Is there a practical alternative to struct inheritance? (C#)

I am writing code that will populate the Margin , Padding and BorderThickness properties of classes in the System.Windows.Documents namespace. 我正在编写将填充System.Windows.Documents命名空间中的类的MarginPaddingBorderThickness属性的代码。 Each of these properties accepts a value in the form of a System.Windows.Thickness , which is a struct. 这些属性中的每一个都接受System.Windows.Thickness形式的值,这是一个结构。

However, I wish to associate some additional data with each of these property assignments, which may subsequently be retrieved by my code. 但是,我希望将一些附加数据与每个属性分配相关联,随后可以通过我的代码检索这些属性。 If Thickness were a class, I would inherit from it and define properties in the subclass to store my additional data items. 如果Thickness是一个类,我将继承它并在子类中定义属性以存储我的其他数据项。 But since it is a struct, inheritance is not possible. 但由于它是一个结构,继承是不可能的。

Is there any practical way to achieve this, while maintaining type-compatibility with the properties I am populating? 有没有任何实用的方法来实现这一点,同时保持与我填充的属性的类型兼容性?

Thanks for your ideas, 谢谢你的想法,

Tim 蒂姆

There are no good alternatives. 没有好的选择。

Depending on what you are trying to do, you could perhaps define your own class with the properties you need, and define the implicit conversion operator to do a implicit conversion to the correct struct type. 根据您要执行的操作,您可以使用所需的属性定义自己的类,并定义隐式转换运算符以执行隐式转换为正确的结构类型。 Then you would be able to pass in your class to all methods expecting a Thickness parameter. 然后你就可以将你的类传递给所有期望厚度参数的方法。

This would go against the recommendation of using the implicit conversion operator, though, since it states that the implicit conversion should not lose any information. 但这违背了使用隐式转换运算符的建议,因为它声明隐式转换不应丢失任何信息。 You will not be able to get the Thickness back from the property you are reading, and see the extra information you attached. 您将无法从正在阅读的酒店获得厚度,并查看您附加的额外信息。

This is how you could implement it: 这是你如何实现它:

public class ThicknessEx
{
    public string ExtraData { get; set; }
    public Thickness Thickness { get; set; }

    public static implicit operator Thickness(ThicknessEx rhs)
    {
        return rhs.Thickness;
    }
}

However, you are probably better off by storing the extra data elsewhere. 但是,通过将额外数据存储在其他地方,您可能会更好。 How to do it would depend on your needs and application. 如何做到这将取决于您的需求和应用。

You might be able to use Attached Dependency Properties of type AugmentedThickness and then, when they change, update the underlying properties they are intended to update. 您可以使用AugmentedThickness类型的Attached Dependency Properties ,然后在更改时更新它们要更新的基础属性。 This requires all access to be performed using your Attached Properties, as simply setting the Thickness property will not use your AugmentedThickness. 这需要使用附加属性执行所有访问,因为只需设置Thickness属性就不会使用AugmentedThickness。 If necessary, you could also (though it might be a bit evil) listen for explicit changes to Thickness properties (that you didn't initiate) and force it back to the value specified by your AugmentedThickness. 如果有必要,你也可以(虽然它可能有点邪恶)听取厚度属性的明确更改(你没有启动)并强制它回到AugmentedThickness指定的值。

你能否存储一个字典,其中键是结构的哈希码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM