CS3016 - How do we get around this when working with Prism + MEF ExportModule?
-
02-03-2021 - |
Question
[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?
Solution
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");
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow