设置一个非常量表达式为DisplayName。

时间:2022-01-18 19:22:26

I need to load the DisplayName for a property dynamically (from a database). For example something like this:

我需要动态地(从数据库中)加载属性的DisplayName。例如:

[DisplayName(getDescriptionForLanguage("test"))]
public string test{ get; set; }

But it's only possible to load a DisplayName dynamically, only constants are allowed. Is there some way to get the DisplayName as result of a method and NOT from a Resourcefile or a constant?

但是只能动态加载DisplayName,只允许常量。是否有某种方法可以将DisplayName作为方法的结果,而不是来自Resourcefile或常量?

2 个解决方案

#1


2  

It's possible to call a method that returns a non-constant string.
You have to create a new Attributclass, for example like this:

可以调用返回非常量字符串的方法。您必须创建一个新的Attributclass,例如:

class DisplayNameLanguage : DisplayNameAttribute
{
    private readonly string resourceName;
    public DisplayNameLanguage(string resourceName)
        : base()
    {
        this.resourceName = resourceName;
    }

    public override string DisplayName
    {
        get
        {
            return getDescriptionForLanguage(resourceName);
        }
     }
}

Now you have to create a partial subclass of your model. There you can use the new Attribute that gets the description from your method getDescriptionForLanguage:

现在必须创建模型的部分子类。在这里,您可以使用新属性,该属性从方法getDescriptionForLanguage中获取描述:

[MetadataType(typeof(TestMD))]
public partial class Test { }
public partial class TestMD
{
    [DisplayNameLanguage("Test")]
    public string Prop1 { get; set; }
}

#2


0  

There is no way to change that behavior. The value passed to the attribute needs to be a compile-time constant, meaning that even using a static property of a static class won't do the trick.

没有办法改变这种行为。传递给该属性的值需要是编译时常量,这意味着即使使用静态类的静态属性也不会起作用。

As suggested by dtb you could however stop using the DisplayName-attribute to get the display name and instead build your own mechanism at the place where the value of the DisplayName-attribute is being evaluated.

但是,正如dtb建议的那样,您可以停止使用displayname -属性来获取显示名,而是在计算displayname -属性值的地方构建您自己的机制。

#1


2  

It's possible to call a method that returns a non-constant string.
You have to create a new Attributclass, for example like this:

可以调用返回非常量字符串的方法。您必须创建一个新的Attributclass,例如:

class DisplayNameLanguage : DisplayNameAttribute
{
    private readonly string resourceName;
    public DisplayNameLanguage(string resourceName)
        : base()
    {
        this.resourceName = resourceName;
    }

    public override string DisplayName
    {
        get
        {
            return getDescriptionForLanguage(resourceName);
        }
     }
}

Now you have to create a partial subclass of your model. There you can use the new Attribute that gets the description from your method getDescriptionForLanguage:

现在必须创建模型的部分子类。在这里,您可以使用新属性,该属性从方法getDescriptionForLanguage中获取描述:

[MetadataType(typeof(TestMD))]
public partial class Test { }
public partial class TestMD
{
    [DisplayNameLanguage("Test")]
    public string Prop1 { get; set; }
}

#2


0  

There is no way to change that behavior. The value passed to the attribute needs to be a compile-time constant, meaning that even using a static property of a static class won't do the trick.

没有办法改变这种行为。传递给该属性的值需要是编译时常量,这意味着即使使用静态类的静态属性也不会起作用。

As suggested by dtb you could however stop using the DisplayName-attribute to get the display name and instead build your own mechanism at the place where the value of the DisplayName-attribute is being evaluated.

但是,正如dtb建议的那样,您可以停止使用displayname -属性来获取显示名,而是在计算displayname -属性值的地方构建您自己的机制。