5
[assembly: CLSCompliant(true)]

//CS3016: Arrays as attribute arguments is not CLS-compliant.
[ModuleExport(typeof(ModuleA), DependsOnModuleNames = new [] { "ModuleB" })]
public class ModuleA : IModule { }

The only thing I can think of is to mark the class as [CLSCompliant(false)], but I was wondering if there is a better way to get around this?

myermian
  • 31,823
  • 24
  • 123
  • 215

1 Answers1

3

As a workaround you can implement your own CLS compliant ModuleExportAttribute which uses a comma separated list instead of an string array:

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class StringListModuleExportAttribute : ExportAttribute, IModuleExport
{
    public StringListModuleExportAttribute(Type moduleType)
        : base(typeof(IModule))
    {
        ModuleName = moduleType.Name;
        ModuleType = moduleType;
    }

    public string ModuleName { get; private set; }
    public Type ModuleType { get; private set; }
    public InitializationMode InitializationMode { get; private set; }
    public string[] DependsOnModuleNames
    {
        get
        {
            if (string.IsNullOrEmpty(DependsOnModuleNameList))
                return new string[0];
            return DependsOnModuleNameList.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
        }
    }

    public string DependsOnModuleNameList { get; set; }
}

Usage:

[StringListModuleExport(typeof(ModuleA), DependsOnModuleNameList = "ModuleB,ModuleC")]
public class ModuleA : IModule
{
    public void Initialize()
    {
        Debug.WriteLine("ModuleA init");
    }
}
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • While this works, I really was hoping for a solution without having to extend from the existing class. But, perhaps that is the only way (outside of suppressing the warning). – myermian Nov 22 '11 at 16:51