How do you put a Func in a C# attribute (annotation)?

I don’t think it is possible, c# only allows you to use Attributes with constant values only.

One way of doing this would be to create a class instead of a func, and then you pass the type of that class into the attribute.
You’d then need to instantiate it with reflection and execute a method in it.

So an example would be:

public interface IDoStuff
{
    IList<string> Execute();
}

then your attribute would take a type instead of a Func

public OperationInfo(VisibilityType v, string title, Type type)
{
   ///...
}

you then would create a class that implements your interface and pass it into the attribute. Although this would mean that (at least initially, and without knowing your problem) you will need to create a new class every time you want to return different results.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)