我想知道如何在新的CoreClR中读取汇编版本信息,或者使用当前正在执行代码的程序集的名称,或者显式地查看程序集的名称,或者最好是某个程序集中的类型(可以静态引用)?< / p>
我的代码如下(目前我非常关心汇编版本)
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));
&LT;编辑:
正如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"
&lt;编辑:我的设置就像我创建新项目时一样,Assembly
被识别。除了继承自Assembly.Load
的两个方法之外,还有一种方法object
。我可以使用它来加载程序集,从而获得我想要的版本信息。或者我愿意,但是当我尝试运行应用程序时,它会崩溃并显示消息 System.InvalidOperationException:无法解析目标框架的以下依赖关系&#39; DNXCore,Version = v5.0&#39; 。这很奇怪,因为我已经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();
}
答案 0 :(得分:2)
Assembly.GetAssembly(type).GetName().Version
或
Assembly.GetAssembly(typeof(assemblyName)).GetName().Version
或者,如果要在运行时加载程序集
Assembly.LoadFile(dllPath).GetName().Version