English 中文(简体)
• 简化C#中的集会描述方式?
原标题:Simplified way to get assembly description in C#?

我正在通过“NET2.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();
}

简单地了解集会描述,这有许多法典,我希望知道,这样做是否比较简单。 NET 3.5 + 采用LINQ或lambda表达方式?

最佳回答

确实有点。 你们可以这样说:

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

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

[EDIT将议会改为国际监管机构,参看Simon Svensson的答复]

如果你需要这样一部法典,则在ICustomAttribute上采用一种推广方法。 提供人:

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

如Yuriy所解释的,自Net 4.5以来,框架中已有一种推广方法:

var descriptionAttribute = 
    assembly.GetCustomAttribute<AssemblyDescriptionAttribute>();
问题回答
var attribute = Assembly.GetExecutingAssembly()
                    .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false)
                    .Cast<AssemblyDescriptionAttribute>().FirstOrDefault();
if (attribute != null)
{
    Console.WriteLine(attribute.Description);
}

在本案中,你不需要LINQ或lambda的表述。

自www.NET 4.5以来,你可以享受以下信息:https://learn.microsoft.com/en-us/dotnet/api/system.reflection.customattributeextensions.gettomattribute?view=netframework-4.5#System_Reflection_CustomAttributeExtensions_1_System_Reflection_Assembly_" rel=“nofollow noreer”>extension”>extensionet/

var descriptionAttribute = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>();

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

在“@ab-kolan”答复之后,可以比较简单:

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

如果你仅对目前的执行过程(相对于原来的员额的组装)感兴趣,那么它就是一个简单的一行。

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

虽然这部法律已经较为简明,但您<>可。 • 利用略微的LINQ来清理它。

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

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

我愿这样做:

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

在这里,你可以很容易地听从两条法典,如果这太大,你可以把它放在一种延伸方法上:

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

然后,你才采用这样的推广方法:

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

If you re referring to the Description in the Assembly Information: Project s Assembly Information Window

[项目――和项目;申请――和;议会信息]

I would recommend using the AssemblyInfo Class, you can see an example here:

当我使用时,我有一个<代码>。 类型:, 采用思考方式,并做到:

using Microsoft.VisualBasic.ApplicationServices;

AssemblyInfo info = new AssemblyInfo(type.Assembly);
string description = info.Description;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签