如何在CodeAttribute中获取属性值 [英] How get Attribute Value in CodeAttribute
问题描述
我编写了一种按属性获取属性值的方法:
I wrote a method to get Attribute Value By Property:
public string GetAttributeValueByNameAttributeAndProperty(CodeClass cc, string nameAttribute, string nameProperty)
{
var value = "";
foreach(CodeAttribute ca in cc.Attributes)
{
if(ca.Name.Contains(nameAttribute) && ca.Value.Contains(nameProperty))
{
value = ca.Value.Remove(0,ca.Value.IndexOf(nameProperty));
value = value.Replace(" ","");
if(value.Contains(","))
value = value.Remove(ca.Value.IndexOf(","));
}
}
return value;
}
例如:
我有属性[Map(Name = "MenuItem, Availability" )]
我叫GetAttributeValueByNameAttributeAndProperty(codeclass,"Map","Name") 在该方法之后,获取CodeAttribute.Value并返回字符串:Name ="MenuItem,Availability" 在我删除"Name ="和多余的字符并用,"分隔后
I call GetAttributeValueByNameAttributeAndProperty( codeclass, "Map" , "Name") After that method get CodeAttribute.Value and return string: Name = "MenuItem, Availability" After I remove "Name = " and extra characters and Split by ","
但是我的高级程序员告诉我,这种方法不灵活,我需要找到一种更方便的方法来获取CodeAttribute.Value中的内部数据.
But my Senior Programmer told me that this method is inflexible and I need to find a more convenient way get inner data in CodeAttribute.Value.
您有什么想法/例子吗?
Do you have any ideas / examples?
推荐答案
您可以使用 CodeClass.Attributes
属性以获取类的属性.每个属性的类型为 CodeAttribute
,并具有 Name
和 CodeAttributeArgument
,其中具有 Name
和
You can use CodeClass.Attributes
property to get attributes of a class. Each attribute is of type of CodeAttribute
and has a Name
and a Children
property which contains arguments of the attribute. Each argument is of type of CodeAttributeArgument
which has Name
and Value
properties.
示例
现在,您拥有从CodeAttribute
获取属性值所需的所有信息.这是一个例子.我已经用[MySample(Property1 = "Something")]
属性装饰了Program
类
Now you have all information which you need to get attribute value from CodeAttribute
. Here is an example. I've decorated Program
class with a [MySample(Property1 = "Something")]
attribute
using System;
namespace ConsoleSample
{
[MySample(Property1 = "Something")]
class Program
{
static void Main(string[] args)
{
}
}
public class MySampleAttribute : Attribute
{
public string Property1 { get; set; }
}
}
这是示例T4
模板:
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".txt" #>
<#@ assembly Name="System.Core" #>
<#@ assembly name="EnvDte" #>
<#@ assembly name="EnvDte80" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
<#
var env = (this.Host as IServiceProvider).GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
var project = env.Solution.FindProjectItem(this.Host.TemplateFile).ContainingProject
as EnvDTE.Project;
var codeClass = project.ProjectItems.Item("Program.cs").FileCodeModel.CodeElements
.OfType<CodeNamespace>().ToList()[0]
.Members.OfType<CodeClass>().ToList()[0];
var attribute = codeClass.Attributes.Cast<CodeAttribute>()
.Where(a=>a.Name=="MySample").FirstOrDefault();
if(attribute!=null)
{
var property = attribute.Children.OfType<CodeAttributeArgument>()
.Where(a=>a.Name=="Property1").FirstOrDefault();
if(property!=null)
{
var value = property.Value;
WriteLine(value);
}
}
#>
如果运行模板,您将在输出中收到"Something"
.
If you run the template, you will receive "Something"
in output.
这篇关于如何在CodeAttribute中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!