0

The question already explains what I'm trying to do, here is an example:

[Display(Name = Localization.City)]
public string City { get; set; }

The error is, but (for me) doesn't make sense: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Custodio
  • 8,594
  • 15
  • 80
  • 115

4 Answers4

6

In order to use DisplayAttribute with resources you need to use

[Display(ResourceType=typeof(Localization), Name="City")]
public string City {get;set;}

And don't forget to open resource file and set access modifier to public instead of internal.

STO
  • 10,390
  • 8
  • 32
  • 32
  • This is not working with GridView and AutoGenerateColumns. The Columns names are showing properties names. Any Clue? And yes my resource is public – Custodio Nov 21 '11 at 21:45
  • Are you talking about WinForms GridView? As far as I know it uses TypeDescriptor.GetProperties that does not respect DisplayName attribute. You may create your custom type descriptor and get display name from the attribute. – STO Nov 21 '11 at 22:25
  • @STO, I have a class which i added Display annotation from resources and i used GetAttributeFrom method from [Here](http://stackoverflow.com/questions/7027613/how-to-retrieve-data-annotations-from-code-programmatically) to retrieve data of the property but it doesn't show the localized one! – Dark_Knight Jul 24 '16 at 15:29
1

Attribute paramters must be fixed at compile time. Refer to Jon Skeet's answer here on SO:

An expression E is an attribute-argument-expression if all of the following statements are > true: •The type of E is an attribute parameter type (§17.1.3). •At compile-time, the value of E can be resolved to one of the following: •A constant value. •A System.Type object. •A one-dimensional array of attribute-argument-expressions.

Can you show how you have declared 'Localization.City'?

Community
  • 1
  • 1
santiagoIT
  • 9,411
  • 6
  • 46
  • 57
1

Poached from: C# attribute text from resource file?

public class CustomAttribute : Attribute
{

    public CustomAttribute(Type resourceType, string resourceName)
    {
                Message = ResourceHelper.GetResourceLookup(resourceType, resourceName);
    }

    public string Message { get; set; }
}

public class ResourceHelper
{
    public static  string GetResourceLookup(Type resourceType, string resourceName)
    {
        if ((resourceType != null) && (resourceName != null))
        {
                PropertyInfo property = resourceType.GetProperty(resourceName, BindingFlags.Public | BindingFlags.Static);
                if (property == null)
                {
                        throw new InvalidOperationException(string.Format("Resource Type Does Not Have Property"));
                }
                if (property.PropertyType != typeof(string))
                {
                        throw new InvalidOperationException(string.Format("Resource Property is Not String Type"));
                }
                return (string)property.GetValue(null, null);
        }
        return null; 
        }
}
Community
  • 1
  • 1
Brian
  • 1,845
  • 1
  • 22
  • 37
0

The error message is clear.

Localization.City is not constant. I assume that it is just a static 'read-only' field/property.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • I Agree! I just doesn't understand why the framework is throwing this error. What's the problem with static property? – Custodio Nov 18 '11 at 11:38