2012-04-18 15 views
12

我是通过一个.NET 2.0读书和整个它获取应用程序组件描述此示例代码来:在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(); 
} 

这是一个相当大量的代码只是获得装配说明,我想知道有一种使用LINQ或lambda表达式在.NET 3.5+中执行此操作的简单方法?

+7

我觉得这个代码是不够好 – 2012-04-18 05:54:56

回答

27

没有,真的。你可以把它有点“更流畅”是这样的:

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

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

[编辑变化大会ICustomAttributeProvider,比照。由西蒙·斯文森回答)

如果你需要这样的代码很多,就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

尽管此代码已经是比较简洁的,你可能杠杆一点点的LINQ来清理它的触摸。

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

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

我会用一个扩展方法ICustomAttributeProvider提供一个强类型GetCustomAttributes它返回一个强类型的枚举。唯一的LINQ用法是调用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'()'。 – abatishchev 2012-11-08 19:17:10

1

我会做这样的事情:

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

在这里你去 - 这很容易凝结成两行代码 - - 如果这太大,可以将其转储为扩展方法:

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

然后你只需要使用扩展方法是这样的:

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

继@ AB-kolan答案,也可能是更简单:

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

如果你只在当前执行的进程有兴趣(与装配按照原帖),那么它是一个简单的衬垫..

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