red_asparagus
red_asparagus

Reputation: 359

Arrays of Types in attributes

I want to pass array of Types to attribute, I tried do it this way

class CustomAttribute: Attribute 
{
   Type[] included;
   Type[] excluded;
}

[CustomAttribute{included = new Type[] {typeof(ComponentA)}, 
                 excluded = new Type[] {typeof(ComponentB)} }]//dont work  
class Processor 
{
   // something here
}

but VS show cant pass Types in attribute.

Do somebody know how to resolve this issue or eventual workaround ?

Upvotes: 0

Views: 251

Answers (3)

mnemonic
mnemonic

Reputation: 1032

There is some access modifiers missing and you're not using the attribute correctly.

  1. You need to make the included and exluded properties in the `CustomAttribute``` class public:

    class CustomAttribute: Attribute 
    {
       public Type[] Included { get; set; }
       public Type[] Excluded { get; set; }
    }
    
  2. When you specify values for the members of the Attribute you need to use () and not {}

    your line is:

    [CustomAttribute{included = new Type[] {typeof(ComponentA)},excluded = new Type[] {typeof(ComponentB)} }]
    

    and it should be:

    [CustomAttribute(Included = new Type[] {typeof(ComponentA)}, Excluded = new Type[] {typeof(ComponentB)} )]
    

    note the ( after CustomAttribute and the ) before the last ].

Upvotes: 0

Älskar
Älskar

Reputation: 2577

Your Type fields are private. Consider using public properties instead of making fields public. "Why it is preferable to use public properties instead of public fields"

Your code won't compile since your syntax for passing parameters to an attribute is incorrect. You need:

class CustomAttribute: Attribute 
{
    public Type[] Included { get; set; }
    public Type[] Excluded { get; set; }
}

[CustomAttribute(Included = new Type[] {typeof(ComponentA)}, Excluded = new 
Type[] {typeof(ComponentB)} )]  
class Processor 
{
   // something here
}

Notice the parentheses when passing parameters (and not {}) to the attribute constructor.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500225

It looks like this has nothing to do with arrays, but everything to do with your general syntax. You're using { } for attribute arguments instead of ( ), and your attribute doesn't have public properties, which are required for named arguments in attributes. Additionally, your attribute class doesn't use [AttributeUsage(...)].

Here's a complete example which does compile:

using System;

[AttributeUsage(AttributeTargets.All)]
class CustomAttribute : Attribute 
{
    public Type[] Included { get; set; }
    public Type[] Excluded { get; set; }
}

[CustomAttribute(Included = new Type[] { typeof(string) },
                 Excluded = new Type[] { typeof(int), typeof(bool) })]
class Processor 
{
}

To make things briefer:

  • You can provide a constructor with parameters
  • You can use new[] instead of new Type[]

So:

using System;

[AttributeUsage(AttributeTargets.All)]
class CustomAttribute : Attribute 
{
    public Type[] Included { get; set; }
    public Type[] Excluded { get; set; }

    // Keep a parameterless constructor to allow for
    // named attribute arguments
    public CustomAttribute()
    {
    }

    public CustomAttribute(Type[] included, Type[] excluded)
    {
        Included = included;
        Excluded = excluded;
    }
}

[CustomAttribute(new[] { typeof(string) }, new[] { typeof(int), typeof(bool) })]
class Processor 
{
    // Provide the named version still works
    [CustomAttribute(Included = new[] { typeof(string) },
                     Excluded = new[] { typeof(int), typeof(bool) })]
    public void Method()
    {
    }
}

Upvotes: 4

Related Questions