2

I need to decorate a large list of enums with a function to use, if it's this enum. I currently have this :

public enum AnimalType
{
    [CustomSet(Helpers.ToUpper)]
    Dog,
    Snake,
    Bird
}

public class CustomSet : Attribute
{
    public Func<string, string> Function { get; set; }

    public CustomSet(Func<string, string> function)
    {
        Function = function;
    }

    public string GetFix (string value)
    {
        return Function(value);
    }

}

public static class Helpers
{
    public static string ToUpper(string value)
    {
        return value.ToUpper();
    }
}

However, the attribute on the enum gives me an error of

Error   CS0181  Attribute constructor parameter 'function' has type 'Func<string, string>', which is not a valid attribute parameter type   

Which I assume is due to the method not being a native type. Can anyone recommend a better way of doing this?

Thanks in advance

4
  • What do you want to do with the function?
    – Sweeper
    Jan 16, 2018 at 6:37
  • In the scenario that I need it for I have around 50 different types of fields, each represented with an enum. Certain fields needs a custom way to correct the value in the field, for instance, pad it with zeros to six chars. In that case, I want to give the enum an attribute of PadLeft(6, '0').
    – WynDiesel
    Jan 16, 2018 at 6:39
  • 1
    Have you tried using a dictionary with enum values as keys and Funcs as values?
    – Sweeper
    Jan 16, 2018 at 6:42
  • I like that idea! Maybe not decorate the attribute itself, but rather keeping a separate dictionary. I might end up doing this.
    – WynDiesel
    Jan 16, 2018 at 6:47

2 Answers 2

3

There's no clean way to express a method / delegate in an attribute. One lazy approach might be:

[CustomSet(typeof(Helpers), nameof(Helpers.ToUpper))]

i.e. a Type/string pair that you later use to resolve the actual method via reflection. However, I wonder if a better approach might be to make the attribute itself the "doer of things", i.e.

abstract class FormatAttribute : Attribute {
    public abstract string Format(string value);
}
class UpperCaseAttribute : FormatAttribute {
    public override string Format(string value) => value.ToUpper();
}

with:

[UpperCase]
Dog,

Now, when you use reflection via GetCustomAttribute looking for the abstract FormatAttribute, you will get the concrete instance (in this case: UpperCaseAttribute, although you won't know that). Just invoke the .Format(...) method on the attribute you get.

1
  • Currently I'm doing other functionality on the enums in this way as well. Ie, create an attrib for each type of fix I want to implement. It works, but most of these fixes Im going to use wont ever be re-used, and I thought being able to specify the method to use in the attrib itself would be ideal, but yeah, I'm starting to lean back/more towards the way that you suggested as well.
    – WynDiesel
    Jan 16, 2018 at 6:50
2

You can create a Dictionary<AnimalType, Func<string, string>>:

public static readonly Dictionary<AnimalType, Func<string, string>> AnimeTypeMappers = 
    new Dictionary<AnimalType, Func<string, string>>() {
        {AnimalType.Dog, Helpers.ToUpper}
    };

Now you can just get the Func like this:

AnimalTypeMappers[AnimalType.Dog]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.