我写了一个方法来获取属性值属性:
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;
}
Run Code Online (Sandbox Code Playgroud)
例如:
我有属性 [Map(Name = "MenuItem, Availability" )]
我调用GetAttributeValueByNameAttributeAndProperty(codeclass,"Map","Name")之后,该方法获取CodeAttribute.Value并返回字符串:Name ="MenuItem,Availability"删除"Name ="和额外字符后,按","拆分
但我的高级程序员告诉我,这种方法不灵活,我需要找到一种更方便的方法来获取CodeAttribute.Value中的内部数据.
你有什么想法/例子吗?
所以我有一个 T4 模板,我试图在设计时运行,但它一直给我以下错误。
Running transformation: System.IO.FileNotFoundException: Could not load
file or assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system
cannot find the file specified.
File name: 'System.Runtime, Version=4.2.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'
Run Code Online (Sandbox Code Playgroud)
我的 Visual Studios 解决方案包含 10 个项目,所有项目都针对 .Net Core 2.0 框架。我的 T4 模板目前看起来是这样的:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="Services.Resources.DataTransferObjects.Infrastructures" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ assembly name="$(TargetDir)Services.dll" #> …Run Code Online (Sandbox Code Playgroud)