2012-04-18 5 views
12

Tôi đã đọc qua một cuốn sách .NET 2.0 và đã xem qua mẫu mã này mà được mô tả ứng dụng lắp ráp:Cách đơn giản hóa để có được mô tả lắp ráp trong 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(); 
} 

Đó là khá nhiều mã để chỉ đơn giản nhận được mô tả lắp ráp và tôi muốn biết nếu có một cách đơn giản hơn để làm điều này trong .NET 3.5+ sử dụng LINQ hoặc lambda biểu thức?

+7

tôi nghĩ rằng mã này là đủ tốt –

Trả lời

27

Thực sự không có. Bạn có thể làm cho nó 'thông thạo hơn' như thế này:

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

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

[EDIT đã thay đổi Lắp ráp thành ICustomAttributeProvider, cf. trả lời bởi Simon Svensson)

Và nếu bạn cần loại mã này rất nhiều, thực hiện một phương pháp khuyến nông trên 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

Trong khi mã này đã tương đối ngắn gọn, bạn thể đòn bẩy một chút của LINQ để làm sạch nó một liên lạc.

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

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

Tôi sẽ sử dụng một phương pháp mở rộng cho ICustomAttributeProvider để cung cấp một mạnh mẽ gõ GetCustomAttributes mà trả về một mạnh mẽ gõ đếm được. Việc sử dụng LINQ duy nhất là cuộc gọi đến FirstOrDefaultOfType

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 cho '()'. – abatishchev

1

tôi sẽ làm một cái gì đó như thế này:

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

Ở đây bạn đi - nó ngưng tụ một cách dễ dàng để hai dòng mã - - và nếu đó là quá lớn, bạn có thể đổ nó vào một phương pháp mở rộng:

Sau đó, bạn chỉ cần sử dụng các phương pháp khuyến nông như thế này:

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

Sau @ ab-kolan câu trả lời, nó có thể thậm chí đơn giản hơn:

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

Nếu bạn chỉ quan tâm trong quá trình thực hiện hiện nay (so với lắp ráp theo các bài bản gốc), sau đó nó là một câu lót đơn giản ..

Console.WriteLine(Process.GetCurrentProcess().MainModule.FileVersionInfo.Comments);