2
我已经创建了属于MEF的自定义属性,我希望定义与该类相关的id列表,以便我可以查询它们。将列表添加到系统属性
而且该类必须包含在自身定义,这是很重要的,这就是为什么我想过使用:
[SignalSystemData("ServiceIds", new List<int>(){1})]
我应如何处理?
我的搜索的实现如下:
var c = new Class1();
var v = c.EditorSystemList;
foreach (var lazy in v.Where(x=>x.Metadata.LongName=="ServiceIds"))
{
if (lazy.Metadata.ServiceId.Contains(serviceIdToCall))
{
var v2 = lazy.Value;
// v2 is the instance of MyEditorSystem
Console.WriteLine(serviceIdToCall.ToString() + " found");
}else
{
Console.WriteLine(serviceIdToCall.ToString() + " not found");
}
}
我与定义导出类应该是这样的:
[Export(typeof(IEditorSystem))]
[SignalSystemData("ServiceIds", new List<int>{1})]
public class MyEditorSystem1 : IEditorSystem
{
void test()
{
Console.WriteLine("ServiceID : 1");
}
}
public interface IEditorSystem
{
}
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class SignalSystemDataAttribute : ExportAttribute
{
public SignalSystemDataAttribute(string longName, List<int> serviceId)
: base(typeof (IEditorSystem))
{
LongName = longName;
ServiceId = serviceId;
}
public string LongName { get; set; }
public List<int> ServiceId { get; set; }
}
public interface IEditorSystemMetadata
{
string LongName { get; }
List<int> ServiceId { get; }
}
这不会飞。 [属性值必须是(编译时)常量。](http://msdn.microsoft.com/en-us/library/yd21828z.aspx) –
@ Christian.K这就是我所知道的,所以我试图找到方式会怎样,而不是诉诸于使用昏迷分离的字符串。 – cpoDesign
啊,对不起。你可能想在你的问题中指出这一点。我的答案也是如此。 –