c# - Reading assembly version information and custom attributes in CoreCLR -
i wonder how read assembly version information in new coreclr either name of assembly code executing or explicitly giviging name of assembly or preferably type in assembly (can statically referenced)?
i have code following (currently care assembly version)
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 { { return customattributes<assemblytitleattribute>().title; } } public version assemblyversion { { return assembly.getname().version; } } public string fileversion { { return fileversioninfo.getversioninfo(assembly.location).fileversion; } } private t customattributes<t>() t: attribute { var customattributes = assembly.getcustomattributes(typeof(t), false); if(customattributes.length > 0) { return (t)customattributes[0]; } throw new invalidoperationexception(); } }
with usage such
var info = new assemblyinfo(typeof(someclassinsomeassembly));
<edit: commented ramin's otherwise nice answer, if should work, wonder if there's else going on here. error message referencing assembly
, vs 2015 rc doesn't suggest adding anything. error messages error cs0246 type or namespace name 'assembly' not found (are missing using directive or assembly reference?) coreclrconsoleapp1.dnx core 5.0\coreclrconsoleapp1\src\coreclrconsoleapp1
.
my project.json is
"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"
<edit: setup when created new project, assembly
recognized. there 1 method, assembly.load
, besides 2 inherited object
. can use load assembly , consequently version information desire. or would, when try run application, crashes message system.invalidoperationexception: failed resolve following dependencies target framework 'dnxcore,version=v5.0'. strange, i've ran dnu restore
, success, still dnx . run
won't work. tried dnvm install -r coreclr latest
(with both x86 , x64) , checked dnvm use
framework selected.
the custom argument code (composing on top of head):
private string customattributes<t>() 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(); }
assembly.getassembly(type).getname().version
or
assembly.getassembly(typeof(assemblyname)).getname().version
or, if want load assembly @ run time
assembly.loadfile(dllpath).getname().version
Comments
Post a Comment