1

I have the following class:

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ModuleActivationButtonAttribute : ExportAttribute
{
    public Enum TargetRegion { get; set; }

    public ModuleActivationButtonAttribute(Enum targetRegion) : base(typeof(IModuleActivationButton))
    {
        TargetRegion = targetRegion;
    }
}

The class compiles fine, but when I decorate my property with it:

[ModuleActivationButton(Regions.Tabs)]
public IModuleActivationButton ModuleActivationButton
{
    get { return new ModuleActivationButton() as IModuleActivationButton; }
    set { ModuleActivationButton = value; }
}

public enum Regions
{
    Content,
    Tabs
}

The compiler spits out:

Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type C:\...\CompanyX.Modules.Home\HomeModule.cs 28 33 CompanyX.Modules.Home

cmaduro
  • 1,672
  • 3
  • 21
  • 40
  • platform,language, and compiler, please – Muad'Dib May 20 '10 at 17:41
  • Ok, I've deleted my response because I'm not convinced it helps you and I don't know enough about MEF to go much further. But you might want to include a bit more detail about what you're trying to do; looking at the example of ExportAttribute on MSDN, I think you may be using it wrong. It seems to have Exports on classes and Imports on properties. – pdr May 20 '10 at 18:39
  • According to: http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.exportattribute.aspx you can export on classes and properties and methods and fields. – cmaduro May 20 '10 at 21:40
  • It seems that I can box the enum to an object and pass it as that, then unbox it when I interpret the metadata. But then I can enforce this only by throwing an exception if upon interpreting it does not unbox to an Enum. – cmaduro May 20 '10 at 22:04

1 Answers1

1

It seems that I can box the enum to an object and pass it as that, then unbox. But then I can enforce this only by throwing an exception if upon interpreting it does not unbox to an Enum

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ModuleActivationButtonAttribute : ExportAttribute
{
    public Enum TargetRegion { get; set; }

    public ModuleActivationButtonAttribute(object targetRegion) : base(typeof(IModuleActivationButton))
    {
        TargetRegion = targetRegion as Enum;
    }
}
cmaduro
  • 1,672
  • 3
  • 21
  • 40