views:

42

answers:

1

As I havent seen an answer to this question in my Google and site searches I thought I would pose it the great minds on this site.

As C# does not support generic attributes (which does make sense), I was wondering if there is a way to restrict the type of an argument/property to the attribute based on the type it is decorating. Example:

[AttributeUsage(AttributeTargets.Property)]
public class ColumnBindAttribute : Attribute
{
    public string ColumnName { get; set; }
    public object DefaultValue { get; set; }
}

[ColumnBind(ColumnName = "Category", DefaultValue = "No Category")]
public int CategoryId { get; set; }

Now clearly when I attempt to bind this property to its default value, I will receive a cast error. Curious if there is anyway to enforce the type of DefaultValue to an int or am I limited to a runtime check.

Thanks in advance

+2  A: 
jrista