覆盖静态构造函数中的元数据?
我有一个继承 TextBox
类的类,将其命名为 MyTextBox
我想重新定义该类的默认 Background
值。
所以我寻找一种方法来做到这一点,并找到了一个不错的选择:调用 BackgroundProperty.OverrideMetadata()
问题是:我可以把它放在哪里?
在 App.OnStartup()
中?丑陋且不实用,我希望将其包含在我的类的代码文件中。
在类的构造函数中?我得到一个例外:
PropertyMetadata 已注册 对于类型“MyTextBox”。
(对我来说似乎很好,我明白为什么我完美地得到了这个)
所以我再次查看了关于 C# 中的静态构造函数的发现(之前没有发现这一点,多遗憾)
所以这是我的代码:
public class MyTextBox : TextBox
{
static MyTextBox()
{
MyTextBox.BackgroundProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(App.Current.Resources["CustomBackgroundBrush"]));
}
}
现在,我很高兴有这个,但微软不是。也就是说,当我使用代码分析功能时,我得到以下信息:
因此我的问题是:我能做些什么呢?
- 忽略警告? >>>我不想忽略警告
- 将调用移至 overrideMetadata 方法? >>>我很想去,但是去哪里呢?
欢迎任何提示,谢谢
编辑: 我要补充一点,我不完全理解为什么会收到此警告,因为我没有在静态构造函数中初始化任何内容,或者是吗?
I have a class that inherits the TextBox
Class, call it MyTextBox
I'd like to redefine the default Background
value for this class.
So I looked for a way to do so and found a good option: call BackgroundProperty.OverrideMetadata()
trouble is: where can I put this?
in the App.OnStartup()
? Ugly and not practical, I'd like that to be in my Class's code file.
in the Class's contructor? I get an exception:
PropertyMetadata is already registered
for the type 'MyTextBox'.
(seems fine to me, I understand why I get this perfectly)
So I looked again a found about the static constructor in C# (did not no about that earlier, what a pity)
so here's my code:
public class MyTextBox : TextBox
{
static MyTextBox()
{
MyTextBox.BackgroundProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(App.Current.Resources["CustomBackgroundBrush"]));
}
}
now, I'm pretty happy whith this, but Microsoft isn't. Namely, when I use the code analysis feature, I get this:
CA1810: Initialize reference type static fields inline
Hence my question: what can I do about it?
- ignore the warning? >> I don't like to ignore warnings
- move the call to the overrideMetadata method? >> I'd like to, but where?
any hints welcome, thanks
Edit: I'll add that I don't fully understand why I get this warning, since I am not initializing anything per say in my static constructor, or am I?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是来自 MSDN 的链接,用于覆盖 覆盖依赖项属性的元数据< /a>:
它指出:
“必须在属性系统使用该属性之前覆盖依赖属性上的元数据(这相当于实例化注册该属性的对象的特定实例的时间)。对 OverrideMetadata 的调用必须在静态将自身提供为 OverrideMetadata 的 forType 参数的类型的构造函数。”
以及您发布到 CA1810 的链接中有关何时抑制警告的措辞:
何时抑制警告
CA1810
如果性能不是问题,则可以安全地抑制此规则的警告;或者,如果由静态初始化引起的全局状态更改代价高昂,或者必须保证在调用该类型的静态方法或创建该类型的实例之前发生。
因此,您当前对警告的实施和抑制可能是正确的路线。
Here is the link from MSDN for overridding metadata for overridding metadata for a dependency property:
It states:
"Overriding metadata on a dependency property must be done prior to that property being placed in use by the property system (this equates to the time that specific instances of objects that register the property are instantiated). Calls to OverrideMetadata must be performed within the static constructors of the type that provides itself as the forType parameter of OverrideMetadata."
And the wording from the link you posted to CA1810 about when to suppress warnings:
When to Suppress Warnings
CA1810
It is safe to suppress a warning from this rule if performance is not a concern; or if global state changes that are caused by static initialization are expensive or must be guaranteed to occur before a static method of the type is called or an instance of the type is created.
So, your current implementation and suppression of the warning is probably the correct route.