如何将 c# 属性转换为文本内容?
How do I turn a c# attribute into a text content?
我有一个解决方案,其中包含几个 C# class 库,它们是较大系统中的插件。该系统在 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;
...
}
}
我有一个解决方案,其中包含几个 C# class 库,它们是较大系统中的插件。该系统在 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;
...
}
}