如何将 c# 属性转换为文本内容
本文关键字:文本 转换 属性 | 更新日期: 2023-09-27 18:33:33
我有一个解决方案,其中包含几个C#类库,这些库是更大系统中的插件。该系统在Windows的注册表中注册其插件。我想做的是以可以从编译的 DLLS 中提取然后放入文本 (.reg) 文件的方式描述注册表项,最好作为构建过程的一部分。
此类元数据是否有预定义的属性?是否有命令行工具或 MSBuild 任务可以提取它们?
想象一下这个属性
[System.AttributeUsage(System.AttributeTargets.Class,
AllowMultiple = true) // Multiuse attribute.]
public class RegistryKey : System.Attribute
{
public string Name {get; private set;}
public string Type {get; private set;}
public string Data {get; private set;}
public RegistryKey(string name, string type, string data)
{
Name = name;
Type = type;
Data = data;
}
}
像这样用反思来消费它
var attrs = System.Attribute.GetCustomAttributes(typeof(MahClass));
foreach (var attr in attrs)
{
if (attr is RegistryKey)
{
var name = attr.Name;
...
}
}