Declare Custom Attribute that create a new object

I'm not sure what i am trying to do is actually possible.

I want to create a new Custom Attribute where by when the attribute is declared the user creates a new object.

I'm looking at Lucene.Net and i want to add a custom attribute to my class property, so i can determine multiple parameters.

Here is my Custom Attribute, It takes in a Lucene.Net Field object :-

[AttributeUsage(AttributeTargets.Property)]
    public class PropertyAnalysed : Attribute
    {
        public Field Field;

        public PropertyAnalysed(Field field)
        {
            this.Field = field;
        }
    }

When I declare the custom attribute on property, I want to do the following :-

 [LuceneIndex("SampleIndex")]
    public class SampleClass
    {
        [LuceneProperty]
        [PropertyAnalysed(new Field("","",Field.Store.YES, Field.Index.ANALYZED))]
        public int Id { get; set; }
    }

However, i get the following error :-

"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"

Can anyone help on what i can do?

Jon Skeet
people
quotationmark

The simplest approach would be to take several separate parameters, and create the Field instance based on those parameters. You can only configure attributes with compile-time constants, and new Field(...) isn't a compile-time constant.

You may not need all the parameters anyway - for example, Field.Index.ANALYZED sounds like it will be pointless in a PropertyAnalysed attribute, as surely all fields would have that...

people

See more on this question at Stackoverflow