6

I'd like to add custom attributes to the AssemblyInfo, and I've created an extension class named AssemblyMyCustomAttribute

[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyMyCustomAttribute : Attribute
{
    private string myAttribute;

    public AssemblyMyCustomAttribute() : this(string.Empty) { }
    public AssemblyMyCustomAttribute(string txt) { myAttribute = txt; }
}

Then I've added reference to the class in the AssemblyInfo.cs and added the value

// 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("")]

Now I would like to get the value ("testing") in a razor view

I've tried the following with no success:

@ViewContext.Controller.GetType().Assembly.GetCustomAttributes(typeof(AssemblyMyCustomAttribute), false)[0].ToString();

Not sure if this is the best approach, to add custom attributes to my AssemblyInfo. I can't seem to find the correct method to get the value of the attribute.

Kman
  • 4,809
  • 7
  • 38
  • 62
  • What does _"I can't seem to find the correct method to get the value of the attribute"_ mean? What do you expect this code to do and what does it actually do? – CodeCaster Feb 17 '16 at 10:52
  • I am trying to get the value of the AssemblyMyCustomAttribute, hence the last two lines of code. In my view, I would like to display the value of the attribute, which is "testing" – Kman Feb 17 '16 at 10:55
  • I get that, but what _does_ it do? The second line looks like you're close, but I don't know what `.Value` is supposed to do. See also [How to read assembly attributes](http://stackoverflow.com/questions/187495/how-to-read-assembly-attributes). – CodeCaster Feb 17 '16 at 10:55
  • It now displays by text in the view : MyProject.AssemblyInfoExtensions.AssemblyMyCustomAttribute (I've edited the main post, as to use .ToString() and not .Value() as Value is not a method available. I've tried the code in your link, but it returns null while setting the attribute value – Kman Feb 17 '16 at 11:00
  • Please repost your answer @CodeCaster. It works :) – Kman Feb 18 '16 at 14:02

1 Answers1

8

You need to provide a public member that exposes what you want to display:

[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyMyCustomAttribute : Attribute
{
    public string Value { get; private set; }

    public AssemblyMyCustomAttribute() : this("") { }
    public AssemblyMyCustomAttribute(string value) { Value = value; }
}

Then cast the attribute and access the member:

var attribute = ViewContext.Controller.GetType().Assembly.GetCustomAttributes(typeof(AssemblyMyCustomAttribute), false)[0];

@(((AssemblyMyCustomAttribute)attribute).Value)
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thnx. I'll give it a try at once – Kman Feb 17 '16 at 11:03
  • It displays just the text "....AssemblyMyCustomAttribute.Value". Adding attribute.Value in Debug/Watch, I get Cannot convert method group 'Value' to non-delegate type 'object'. Did you intend to invoke.. – Kman Feb 17 '16 at 11:15
  • Creating a instance of the class worked. AssemblyMyCustomAttribue attribute = new AssemblyMyCustomAttribute() – Kman Feb 17 '16 at 11:29
  • But then you're instantiating an attribute to solely pass around a string, pretty useless. The shown code should work and get the attribute from your assembly's metadata. – CodeCaster Feb 17 '16 at 11:30
  • 2
    I realize this post is 8 months old but I had the same issue as KMAN with it displaying the the text "...AssemblyMyCustomAttribute.Value" as well. The issue is that in the example there is a missing set of Parenthesis. The last line should read: @(((AssemblyMyCustomAttribute)attribute).Value) – Steve P Oct 11 '16 at 12:20
  • Good remark with `@()` --> use it for Razor/Blazor pages --> don't use it for "plain" C# code files – Beauty Aug 11 '23 at 12:29
  • To access the attribute from an application **without** `ViewContext`, you can do it like this: `string myString = ((AssemblyMyCustomAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyMyCustomAttribute), false)[0]).Value;` . . . . . Remark: You need to add `using System.Reflection;` to the name spaces. – Beauty Aug 11 '23 at 12:33
  • @CodeCaster, you habe a typo in your code. Maybe you fix it? `@(((AssemblyMyCustomaAttribute)attribute).Value)` --> `Customa` without `a` – Beauty Aug 11 '23 at 12:46
  • @Beauty and you have a typo in your 'have', but thanks, fixed. Edit: ah, ja, das habe ich. – CodeCaster Aug 11 '23 at 13:21