2012-04-18 8 views
12

मैं एक .NET 2.0 पुस्तक के माध्यम से पढ़ रहे हैं और यह नमूना कोड जो अनुप्रयोगों विधानसभा वर्णन हो जाता है भर में आया था:सी # में असेंबली विवरण प्राप्त करने के लिए सरलीकृत तरीका?

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

मुझे लगता है कि इस कोड को काफी अच्छा –

उत्तर

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 उपयोग FirstOrDefault और 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 के लिए '()' है। – abatishchev

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); 
संबंधित मुद्दे