Подтвердить что ты не робот

С# атрибут текст из файла ресурсов?

У меня есть атрибут, и я хочу загрузить текст в атрибут из файла ресурсов.

[IntegerValidation(1, 70, ErrorMessage = Data.Messages.Speed)]
private int i_Speed;

Но я продолжаю получать "Аргумент атрибута должен быть константным выражением, выражением typeof или выражением создания массива типа параметра атрибута"

Он отлично работает, если я добавляю строку вместо Data.Messages.Text, например:

[IntegerValidation(1, 70, ErrorMessage = "Invalid max speed")]

Любые идеи?

4b9b3361

Ответ 1

Значения атрибутов жестко закодированы в сборке при компиляции. Если вы хотите что-то сделать во время выполнения, вам нужно будет использовать константу в качестве ключа, а затем поместить некоторый код в класс атрибута для загрузки ресурса.

Ответ 2

Вот мое решение. Я добавил свойства resourceName и resourceType для атрибута, например, в Microsoft DataNet.

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; 
    }
}

Ответ 3

Вот модифицированная версия той, которую я собрал:

[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = false)]
public class ProviderIconAttribute : Attribute
{
    public Image ProviderIcon { get; protected set; }

    public ProviderIconAttribute(Type resourceType, string resourceName)
    {
        var value = ResourceHelper.GetResourceLookup<Image>(resourceType, resourceName);

        this.ProviderIcon = value;
    }
}

    //From http://stackoverflow.com/questions/1150874/c-sharp-attribute-text-from-resource-file
    //Only thing I changed was adding NonPublic to binding flags since our images come from other dll's
    // and making it generic, as the original only supports strings
    public class ResourceHelper
    {
        public static T GetResourceLookup<T>(Type resourceType, string resourceName)
        {
            if ((resourceType != null) && (resourceName != null))
            {
                PropertyInfo property = resourceType.GetProperty(resourceName, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
                if (property == null)
                {
                    return default(T);
                }

                return (T)property.GetValue(null, null);
            }
            return default(T);
        }
    }

Ответ 4

Я столкнулся с этой проблемой с отображаемым именем для атрибута, и сделал следующие изменения:

В нашем файле ресурсов я изменил свойство настраиваемого инструмента на PublicResXFileCodeGenerator

Затем добавили это в атрибут:

[Display(Name = "MyResourceName", ResourceType = typeof(Resources.MyResources))]

Ответ 5

Используйте строку, которая является именем ресурса..NET делает это с некоторыми внутренними атрибутами.

Ответ 6

Характер атрибутов таков, что данные, которые вы ввели в свойства атрибута, должны быть константами. Эти значения будут сохранены в сборке, но никогда не приведут к выполнению скомпилированного кода. Таким образом, вы не можете иметь значения атрибутов, которые полагаются на выполнение, чтобы рассчитать результаты.

Ответ 7

Здесь что-то я написал, так как не смог найти ничего другого, что делает это.

Ввод

Напишите постоянный класс строк в проекте A.

[GenerateResource]
public static class ResourceFileName
{
    public static class ThisSupports
    {
        public static class NestedClasses
        {
            [Comment("Comment value")]
            public const string ResourceKey = "Resource Value";
        }
    }
}

Выход

И в проекте будет создан ресурс, содержащий класс констант.

enter image description here

Все, что вам нужно сделать, это где-то этот код:

Источник

public class CommentAttribute : Attribute
{
    public CommentAttribute(string comment)
    {
        this.Comment = comment;
    }

    public string Comment { get; set; }
}

public class GenerateResourceAttribute : Attribute
{
    public string FileName { get; set; }
}

public class ResourceGenerator
{
    public ResourceGenerator(IEnumerable<Assembly> assemblies)
    {
        // Loop over the provided assemblies.
        foreach (var assembly in assemblies)
        {
            // Loop over each type in the assembly.
            foreach (var type in assembly.GetTypes())
            {
                // See if the type has the GenerateResource attribute.
                var attribute = type.GetCustomAttribute<GenerateResourceAttribute>(false);
                if (attribute != null)
                {
                    // If so determine the output directory.  First assume it the current directory.
                    var outputDirectory = Directory.GetCurrentDirectory();

                    // Is this assembly part of the output directory?
                    var index = outputDirectory.LastIndexOf(typeof(ResourceGenerator).Assembly.GetName().Name);
                    if (index >= 0)
                    {
                        // If so remove it and anything after it.
                        outputDirectory = outputDirectory.Substring(0, index);

                        // Is the concatenation of the output directory and the target assembly name not a directory?
                        outputDirectory = Path.Combine(outputDirectory, type.Assembly.GetName().Name);
                        if (!Directory.Exists(outputDirectory))
                        {
                            // If that is the case make it the current directory. 
                            outputDirectory = Directory.GetCurrentDirectory();
                        }
                    }

                    // Use the default file name (Type + "Resources") if one was not provided.
                    var fileName = attribute.FileName;
                    if (fileName == null)
                    {
                        fileName = type.Name + "Resources";
                    }

                    // Add .resx to the end of the file name.
                    fileName = Path.Combine(outputDirectory, fileName);
                    if (!fileName.EndsWith(".resx", StringComparison.InvariantCultureIgnoreCase))
                    {
                        fileName += ".resx";
                    }

                    using (var resx = new ResXResourceWriter(fileName))
                    {
                        var tuples = this.GetTuplesRecursive("", type).OrderBy(t => t.Item1);
                        foreach (var tuple in tuples)
                        {
                            var key = tuple.Item1 + tuple.Item2.Name;

                            var value = tuple.Item2.GetValue(null);

                            string comment = null;
                            var commentAttribute = tuple.Item2.GetCustomAttribute<CommentAttribute>();
                            if (commentAttribute != null)
                            {
                                comment = commentAttribute.Comment;
                            }

                            resx.AddResource(new ResXDataNode(key, value) { Comment = comment });
                        }
                    }
                }
            }
        }
    }

    private IEnumerable<Tuple<string, FieldInfo>> GetTuplesRecursive(string prefix, Type type)
    {
        // Get the properties for the current type.
        foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static))
        {
            yield return new Tuple<string, FieldInfo>(prefix, field);
        }

        // Get the properties for each child type.
        foreach (var nestedType in type.GetNestedTypes())
        {
            foreach (var tuple in this.GetTuplesRecursive(prefix + nestedType.Name, nestedType))
            {
                yield return tuple;
            }
        }
    }
}

И затем создайте небольшой проект, который ссылается на все ваши сборки с помощью [GenerateResource]

public class Program
{
    static void Main(string[] args)
    {
        var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
        string path = Directory.GetCurrentDirectory();
        foreach (string dll in Directory.GetFiles(path, "*.dll"))
        {
            assemblies.Add(Assembly.LoadFile(dll));
        }
        assemblies = assemblies.Distinct().ToList();

        new ResourceGenerator(assemblies);
    }
}

Затем ваши атрибуты могут использовать статический класс ResourceFileName.ThisSupports.NestedClasses.ResourceKey, в то время как другой код может использовать файл ресурсов.

Вам может потребоваться адаптировать его к вашим конкретным потребностям.

Ответ 8

У меня есть аналогичный случай, когда мне нужно поместить строки ресурсов в атрибуты. В С# 6 мы имеем возможность nameof(), и это похоже на трюк.

В моем случае я могу использовать [SomeAttribute(nameof(Resources.SomeResourceKey))], и он компилируется отлично. Затем мне нужно немного поработать на другом конце, чтобы использовать это значение, чтобы получить правильную строку из файла ресурсов.

В вашем случае вы можете попробовать:

[IntegerValidation(1, 70, ErrorMessageResourceKey = nameof(Data.Messages.Speed))]
private int i_Speed;

Затем вы можете сделать что-то по строкам (псевдокод):

Properties.Resources.ResourceManager.GetString(attribute.ErrorMessageResourceKey);

Ответ 9

Если вы используете .NET 3.5 или новее, вы можете использовать параметры ErrorMessageResourceName и ErrorMessageResourceType.

Например [Required(ErrorMessageResourceName ="attribute_name" , ErrorMessageResourceType = typeof(resource_file_type))]