2012-04-18 18 views
12

Ben .NET 2.0 kitap aracılığıyla okuma ve uygulamaları montaj açıklamasına alır bu örnek kod karşıdan karşıya geldi:C# 'da montaj açıklamasını almanın basit yolu nedir?

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(); 
} 

Bu sadece montaj açıklamasına almak için kod oldukça fazla ve ben olmadığını bilmek istiyorum .NET 3.5 + 'da LINQ veya lambda ifadeleri kullanarak bunu yapmanın daha basit bir yolu var mı?

+7

.. basit bir liner var ben bu kod '()' için –

cevap

27

Gerçekten yok.

[DÜZENLEME ICustomAttributeProvider için Meclis değişti
var descriptionAttribute = assembly 
     .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) 
     .OfType<AssemblyDescriptionAttribute>() 
     .FirstOrDefault(); 

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

krş: Sen böyle biraz 'daha akıcı' yapabilir) Simon Svensson tarafından

cevap Ve bu kod tür çok gerekiyorsa, ICustomAttributeProvider üzerinde bir uzantısı yöntemi yapmak: Bu kod biraz, sen olabilir kaldıraç zaten nispeten özlü iken

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

bir dokunuş temizlemek için LINQ.

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

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

Ben kuvvetle kuvvetle enumerable yazılan döndürür GetCustomAttributes daktilo temin etmek ICustomAttributeProvider için bir uzantısı yöntemi kullanmak. o iki satır kod kolayca yoğunlaşır -

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(); 
    } 
} 
4
var attribute = Assembly.GetExecutingAssembly() 
        .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) 
        .Cast<AssemblyDescriptionAttribute>().FirstOrDefault(); 
if (attribute != null) 
{ 
    Console.WriteLine(attribute.Description); 
} 
+1

1 yeterince iyi olduğunu düşünüyorum. – abatishchev

1

çağrısı olurdu - ve eğer çok büyükse, bunu bir uzantı yöntemine atabilirsiniz:

Sonra aynen bu şekilde uzatma yöntemi kullanın: ab-kolan cevap @ takiben

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

bakalım: - Sadece linq kullanımı Böyle bir şey yapacağını FirstOrDefault ve 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>(); 
} 
1

, hatta daha fazla olabilir basit:

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

geçerli yürütülmesi sürecinde sadece ilgileniyorsanız (versus orijinal yayına göre montaj), o zaman

Console.WriteLine(Process.GetCurrentProcess().MainModule.FileVersionInfo.Comments); 
İlgili konular