【发布时间】:2016-02-17 10:18:45
【问题描述】:
我想为AssemblyInfo 添加自定义属性,并且我创建了一个名为AssemblyMyCustomAttribute 的扩展类
[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyMyCustomAttribute : Attribute
{
private string myAttribute;
public AssemblyMyCustomAttribute() : this(string.Empty) { }
public AssemblyMyCustomAttribute(string txt) { myAttribute = txt; }
}
然后我在AssemblyInfo.cs 中添加了对该类的引用并添加了值
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("My Project")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("My Project")]
[assembly: AssemblyMyCustomAttribute("testing")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
现在我想在剃刀视图中获取值 ("testing")
我尝试了以下方法但没有成功:
@ViewContext.Controller.GetType().Assembly.GetCustomAttributes(typeof(AssemblyMyCustomAttribute), false)[0].ToString();
不确定这是否是向我的AssemblyInfo 添加自定义属性的最佳方法。我似乎找不到正确的方法来获取属性的值。
【问题讨论】:
-
"我似乎找不到正确的方法来获取属性的值"是什么意思?您希望这段代码做什么以及它实际做什么?
-
我正在尝试获取 AssemblyMyCustomAttribute 的值,因此是最后两行代码。在我看来,我想显示属性的值,即“测试”
-
我明白了,但是它有什么作用呢?第二行看起来你很接近,但我不知道
.Value应该做什么。另请参阅How to read assembly attributes。 -
它现在在视图中通过文本显示:MyProject.AssemblyInfoExtensions.AssemblyMyCustomAttribute(我已经编辑了主帖子,使用 .ToString() 而不是 .Value() 因为 Value 不是一种方法可用。我已经尝试了您链接中的代码,但是在设置属性值时它返回 null
-
请重新发布您的答案@CodeCaster。它有效:)
标签: c# asp.net-mvc assemblyinfo