-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
141 lines (123 loc) · 5.37 KB
/
Copy pathProgram.cs
File metadata and controls
141 lines (123 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//Sample: prints information about the specified assembly using MetadataLoadContext
using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
namespace MetadataLoadContextSample
{
class Program
{
static int Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: dotnet MetadataLoadContextSample.dll <assembly path>");
return 0;
}
string inputFile = args[0];
try
{
//get the array of runtime assemblies
//this will allow us to at least inspect types depending only on BCL
string[] runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll");
//create the list of assembly paths consisting of runtime assemblies and the input file
var paths = new List<string>(runtimeAssemblies);
paths.Add(inputFile);
//create MetadataLoadContext that can resolve assemblies using the created list
var resolver = new PathAssemblyResolver(paths);
var mlc = new MetadataLoadContext(resolver);
using (mlc)
{
//load assembly into MetadataLoadContext
Assembly assembly = mlc.LoadFromAssemblyPath(inputFile);
AssemblyName name = assembly.GetName();
//print assembly attribute information
Console.WriteLine(name.Name + " has following attributes: ");
foreach (CustomAttributeData attr in assembly.GetCustomAttributesData())
{
try
{
Console.WriteLine(attr.AttributeType);
}
catch (FileNotFoundException ex)
{
//we are missing the required dependency assembly
Console.WriteLine("Error: " + ex.Message);
}
}
Console.WriteLine();
//print assembly type information
Console.WriteLine(name.Name + " contains following types: ");
foreach (TypeInfo t in assembly.GetTypes())
{
try
{
Type baseType = t.BaseType;
if (t.IsClass)
{
Console.Write("class ");
}
else if (t.IsValueType)
{
if (String.Equals(baseType?.FullName, "System.Enum", StringComparison.InvariantCulture))
{
Console.Write("enum ");
}
else
{
Console.Write("struct ");
}
}
else if (t.IsInterface)
{
Console.Write("interface ");
}
Console.Write(t.FullName);
if (t.IsClass && !String.Equals(baseType.FullName, "System.Object", StringComparison.InvariantCulture))
{
Console.Write(" : " + baseType.FullName);
}
Console.WriteLine();
}
catch (System.IO.FileNotFoundException ex)
{
//we are missing the required dependency assembly
Console.WriteLine("Error: " + ex.Message);
}
}
}
return 0;
}
catch (IOException ex)
{
Console.WriteLine("I/O error occured when trying to load assembly: ");
Console.WriteLine(ex.ToString());
return 1;
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("Access denied when trying to load assembly: ");
Console.WriteLine(ex.ToString());
return 1;
}
}
}
}
/*
C:\PROJECTS_2017\MetadataLoadContextSample\MetadataLoadContextSample\bin\Debug\netcoreapp2.1>dotnet MetadataLoadContextSample.dll "C:\PROJECTS_2017\CoreTest\NetCoreTest\bin\Debug\netcoreapp2.1\NetCoreTest.dll"
NetCoreTest has following attributes:
System.Runtime.CompilerServices.CompilationRelaxationsAttribute
System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
System.Diagnostics.DebuggableAttribute
System.Runtime.Versioning.TargetFrameworkAttribute
System.Reflection.AssemblyCompanyAttribute
System.Reflection.AssemblyConfigurationAttribute
System.Reflection.AssemblyFileVersionAttribute
System.Reflection.AssemblyInformationalVersionAttribute
System.Reflection.AssemblyProductAttribute
System.Reflection.AssemblyTitleAttribute
NetCoreTest contains following types:
struct TestStruct
class Program
*/