20

I know the .NET Core replacement for Assembly.GetExecutingAssembly() is typeof(MyType).GetTypeInfo().Assembly, but what about the replacement for

Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false)

I have tried appending the last bit of the code after assembly for the first solution mentioned like so:

typeof(VersionInfo).GetTypeInfo().Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute));

but it gives me a "Can't implicitly convert to object[] message.

UPDATE: Yes, as comments below indicate, I believe it is linked to the output type.

Here is the code snippet, and I am just trying to change it to be compatible with .Net Core:

public class VersionInfo 
{
    public static string AssemlyTitle 
    {
        get 
        {
            object[] attributes = Assembly
                .GetExecutingAssembly()
                .GetCustomAttributes(typeof (AssemblyTitleAttribute), false);
          // More code follows
        }
    }
}

I have tried changing this to use CustomAttributeExtensions.GetCustomAttributes() but I don't know how to implement the same code as above. I still get mixed up about MemberInfo and Type and the like.

Any help is extremely appreciated!

spaleet
  • 838
  • 2
  • 10
  • 23
PirateJubber
  • 458
  • 1
  • 4
  • 11

5 Answers5

30

Wouldn't this be the simplest?

string title = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title;

Just say'in.

  • 3
    In some situations, one may consider substituting `GetEntryAssembly` instead of `GetExecutingAssembly`, such as when creating a progress dialog box in a class library that you want to have display the calling application's title. – Wyck Feb 20 '20 at 01:49
12

I suspect that the issue is in code you have not shown: where you use the result of GetCustomAttributes(). That's because Assembly.GetCustomAttributes(Type, bool) in .Net Framework returns object[], while CustomAttributeExtensions.GetCustomAttributes(this Assembly, Type) in .Net Core returns IEnumerable<Attribute>.

So you need to modify your code accordingly. The simplest way would be to use .ToArray<object>(), but a better solution might be to change your code so that it can work with IEnumerable<Attribute>.

svick
  • 236,525
  • 50
  • 385
  • 514
9

This works for me in .NET Core 1.0:

using System;
using System.Linq;
using System.Reflection;

namespace SO_38487353
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var attributes = typeof(Program).GetTypeInfo().Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute));
            var assemblyTitleAttribute = attributes.SingleOrDefault() as AssemblyTitleAttribute;

            Console.WriteLine(assemblyTitleAttribute?.Title);
            Console.ReadKey();
        }
    }
}

AssemblyInfo.cs

using System.Reflection;

[assembly: AssemblyTitle("My Assembly Title")]

project.json

{
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    },
    "System.Runtime": "4.1.0"
  },
  "frameworks": {
    "netcoreapp1.0": { }
  }
}
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
  • I neglected to share the entire situation of what I was trying to do, but you are right, your solution does fix the error. Is there any way to rework it to work with object[]? – PirateJubber Jul 20 '16 at 21:49
6

This works for me:

public static string GetAppTitle()
{
    AssemblyTitleAttribute attributes = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false);

    return attributes?.Title;
}
Thiago Falcao
  • 4,463
  • 39
  • 34
2

This is what i use:

private string GetApplicationTitle => ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false))?.Title ?? "Unknown Title";
Mike
  • 1,525
  • 1
  • 14
  • 11