5
[assembly: CLSCompliant(true)]

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

我唯一能想到的就是将课程标记为[CLSCompliant(false)],但我想知道是否有更好的方法来解决这个问题?

4

1 回答 1

3

作为一种解决方法,您可以实现自己的 CLS 兼容ModuleExportAttribute,它使用逗号分隔列表而不是字符串数组:

[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; }
}

用法:

[StringListModuleExport(typeof(ModuleA), DependsOnModuleNameList = "ModuleB,ModuleC")]
public class ModuleA : IModule
{
    public void Initialize()
    {
        Debug.WriteLine("ModuleA init");
    }
}
于 2011-11-17T06:30:49.053 回答