【问题标题】:Reading assembly version information and custom attributes in CoreCLR在 CoreCLR 中读取程序集版本信息和自定义属性
【发布时间】:2015-07-19 04:45:36
【问题描述】:

我想知道如何在新的 CoreClR 中读取程序集版本信息,或者使用代码当前正在执行的程序集的名称,或者显式给出程序集的名称,或者最好是某个程序集中的类型(可以静态引用)?

我有如下代码(目前我真的很关心汇编版本)

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;


public class AssemblyInfo
{
    private readonly Assembly assembly;

    public AssemblyInfo(Type type)
    {
        assembly = Assembly.GetAssembly(type);
    }

    public string Title
    {
        get { return CustomAttributes<AssemblyTitleAttribute>().Title; }
    }

    public Version AssemblyVersion
    {
        get { return assembly.GetName().Version; }
    }

    public string FileVersion
    {
        get { return FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion; }
    }

    private T CustomAttributes<T>() where T: Attribute
    {
        var customAttributes = assembly.GetCustomAttributes(typeof(T), false);
        if(customAttributes.Length > 0)
        {
            return (T)customAttributes[0];
        }

        throw new InvalidOperationException();
    }
}

有诸如

之类的用法
var info = new AssemblyInfo(typeof(SomeClassInSomeAssembly));

正如对 Ramin 的其他不错的回答所评论的那样,如果这可行,我想知道这里是否还有其他事情发生。我通过引用Assembly 已经收到一条错误消息,而 VS 2015 RC 不建议添加任何内容。错误信息基本上是Error CS0246 The type or namespace name 'Assembly' could not be found (are you missing a using directive or an assembly reference?) CoreClrConsoleApp1.DNX Core 5.0\CoreClrConsoleApp1\src\CoreClrConsoleApp1

我的 project.json 是

"frameworks": {
"dnx451": { },
"dnxcore50": {
  "dependencies": {
    "System.Console": "4.0.0-beta-22816",
    "System.Collections": "4.0.10-beta-22816",
    "System.Linq": "4.0.0-beta-22816",
    "System.Threading": "4.0.10-beta-22816",
    "Microsoft.CSharp": "4.0.0-beta-22816",
    "System.Runtime.InteropServices": "4.0.20-beta-23019",
    "System.Reflection": "4.0.10-beta-23019",
    "System.Diagnostics.FileVersionInfo": "4.0.0-beta-22816",
    "netfx-Reflector": "1.0.0.10"

Assembly 被识别。除了从object 继承的两种方法之外,还有一种方法Assembly.Load。我可以使用它来加载程序集,从而获得我想要的版本信息。或者我会,但是当我尝试运行应用程序时,它会崩溃并显示消息 System.InvalidOperationException: Failed to resolve the following dependencies for target framework 'DNXCore,Version=v5.0'。这很奇怪,因为我已经运行了dnu restore,我得到了成功,但仍然dnx . run 不起作用。我已经尝试过dnvm install -r coreclr latest(同时使用x86 和x64)并且还使用dnvm use 进行了检查,选择了一个框架。

自定义参数代码可以是(在我的脑海中编写):

private string CustomAttributes<T>() where T : Attribute
{
    var customAttribute = assembly.CustomAttributes.Where(a => a.AttributeType == typeof(T)).FirstOrDefault();
    if(customAttribute != null) { return (string)customAttribute.ConstructorArguments[0].Value; }

   throw new InvalidOperationException();
}

【问题讨论】:

    标签: c# .net .net-assembly coreclr


    【解决方案1】:
    Assembly.GetAssembly(type).GetName().Version
    

    或者

    Assembly.GetAssembly(typeof(assemblyName)).GetName().Version
    

    或者,如果你想在运行时加载程序集

    Assembly.LoadFile(dllPath).GetName().Version
    

    【讨论】:

    • 我想我应该更具体一些(唉,孩子们在闹闹),但是当我尝试引用 Assembly 时,我收到一条错误消息 Error CS0246 The type or namespace name 'Assembly' could not be found (are you missing a using directive or an assembly reference?) CoreClrConsoleApp1.DNX Core 5.0\CoreClrConsoleApp1\src\CoreClrConsoleApp1。还没找到牛是什么,VS也不建议加什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2011-05-17
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多