2012-04-18 19 views
12

czytałem przez książkę .NET 2.0 i natknąłem się na ten przykładowy kod, który dostaje się z opisem montażu Zastosowania:Uproszczony sposób na uzyskanie opisu montażu w języku C#?

static void Main(string[] args) 
{ 
    Assembly assembly = Assembly.GetExecutingAssembly(); 
    object[] attributes = 
     assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); 
    if (attributes.Length > 0) 
    { 
     AssemblyDescriptionAttribute descriptionAttribute = 
      (AssemblyDescriptionAttribute)attributes[0]; 
     Console.WriteLine(descriptionAttribute.Description); 
    } 
    Console.ReadKey(); 
} 

To dość dużo kodu po prostu dostać się z opisem montażu i chciałbym wiedzieć, czy jest prostszy sposób robienia tego w .NET 3.5+ przy użyciu wyrażeń LINQ lub lambda?

+7

myślę, że ten kod jest na tyle dobry –

Odpowiedz

27

Nie ma, naprawdę. Można zrobić to nieco 'bardziej płynna' tak:

var descriptionAttribute = assembly 
     .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) 
     .OfType<AssemblyDescriptionAttribute>() 
     .FirstOrDefault(); 

if (descriptionAttribute != null) 
    Console.WriteLine(descriptionAttribute.Description); 

[EDIT zmienił Zgromadzenie do ICustomAttributeProvider, cf. odpowiedzieć Simon Svensson)

A jeśli potrzebujesz tego rodzaju kodu dużo, uczynić metodę rozszerzenia na ICustomAttributeProvider:

public static T GetAttribute<T>(this ICustomAttributeProvider assembly, bool inherit = false) 
where T : Attribute 
{ 
    return assembly 
     .GetCustomAttributes(typeof(T), inherit) 
     .OfType<T>() 
     .FirstOrDefault(); 
} 
1

Podczas gdy ten kod jest już stosunkowo zwięzły, ty mógł dźwigni trochę LINQ, aby wyczyścić to dotknięcie.

AssemblyDescriptionAttribute attribute = assembly 
    .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) 
    .OfType<AssemblyDescriptionAttribute>() 
    .SingleOrDefault(); 

if(attribute != null) 
{ 
    Console.WriteLine(attribute.Description); 
} 
4

użyłbym metodę rozszerzenia dla ICustomAttributeProvider zapewnić silnie wpisane GetCustomAttributes która zwraca silnie wpisane przeliczalny. Jedynym wykorzystanie LINQ byłoby wywołanie FirstOrDefault i OfType

public static void Main() { 
    Assembly assembly = Assembly.GetExecutingAssembly(); 
    var descriptionAttribute = assembly 
     .GetCustomAttributes<AssemblyDescriptionAttribute>(inherit: false) 
     .FirstOrDefault(); 

    if (descriptionAttribute != null) { 
     Console.WriteLine(descriptionAttribute.Description); 
    } 

    Console.ReadKey(); 
} 

public static IEnumerable<T> GetCustomAttributes<T>(this ICustomAttributeProvider provider, bool inherit) where T : Attribute { 
    return provider.GetCustomAttributes(typeof(T), inherit).OfType<T>(); 
} 
4
var attribute = Assembly.GetExecutingAssembly() 
        .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) 
        .Cast<AssemblyDescriptionAttribute>().FirstOrDefault(); 
if (attribute != null) 
{ 
    Console.WriteLine(attribute.Description); 
} 
+1

+1 dla '()'. – abatishchev

1

chciałbym zrobić coś takiego:

public static class AssemblyExtensions 
{ 
    public static string GetDescription(this Assembly assembly) 
    { 
     var attribute = assembly.GetCustomAttributes(typeof (AssemblyDescriptionAttribute), false) 
      .Select(a => a as AssemblyDescriptionAttribute).FirstOrDefault(); 

     if (attribute == null) 
     { 
      return String.Empty; 
     } 

     return attribute.Description; 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var assembly = Assembly.GetExecutingAssembly(); 
     Console.WriteLine(assembly.GetDescription()); 
     Console.ReadKey(); 
    } 
} 
0

tu proszę - to łatwo skrapla się do dwóch linii kodu - - a jeśli jest zbyt duży, można go zrzucić na metodę rozszerzenia:

public static string GetAssemblyDescription(this Assembly assembly) 
{ 
    return assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) 
     .OfType<AssemblyDescriptionAttribute>().SingleOrDefault()?.Description; 
} 

Następnie po prostu użyć metody rozszerzenie takiego:

Console.WriteLine(typeof(Program).Assembly.GetAssemblyDescription()); 
1

Po @ AB Kolan odpowiedzi, to może być jeszcze prostsze:

var description = Assembly 
      .GetExecutingAssembly() 
      .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) 
      .OfType<AssemblyDescriptionAttribute>() 
      .FirstOrDefault()? 
      .Description ?? ""; 
0

Jeśli jesteś zainteresowany tylko w bieżącym procesie wykonującego (versus zespół, jak na oryginalny post), to jest to prosta wkładka ..

Console.WriteLine(Process.GetCurrentProcess().MainModule.FileVersionInfo.Comments); 
Powiązane problemy