这会导致编译时异常:
public sealed class ValidatesAttribute<T> : Attribute
{
}
[Validates<string>]
public static class StringValidation
{
}
Run Code Online (Sandbox Code Playgroud)
我意识到C#不支持通用属性.然而,经过大量的谷歌搜索,我似乎无法找到原因.
有谁知道为什么泛型类型无法衍生出来Attribute?任何理论?
我想设置一个绑定.问题是目标是string类型,但源是double类型.在以下代码中,VersionNumber的类型为double.当我运行它时,文本块是空的,没有抛出任何异常.我该如何设置此绑定?
<Style TargetType="{x:Type MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MyControl}">
<TextBlock Text="{TemplateBinding Property=VersionNumber}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)