-
Notifications
You must be signed in to change notification settings - Fork 61
/
AssemblyCommon.razor
77 lines (70 loc) · 2.99 KB
/
AssemblyCommon.razor
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
@page "/Assembly"
@using System.IO;
@inject IFileReaderService fileReaderService
<h1>Hello, .NET standard assemblies!</h1>
This demo reads a .net Standard assembly and takes a look inside.
<br />
<br />
<strong>Note:</strong> This demo requires the full <a href="https://www.nuget.org/packages/NETStandard.Library/">.net standard dll</a>
to be loaded, so this demo only runs if the linker does not remove the netstandard.dll.
<br />
<br />
<input type="file" @ref=inputElement accept=".dll,.exe" />
<button @onclick=ReadFile class="btn btn-primary">Read assembly</button>
<br />
<br />
<textarea style="max-width: 100%;" cols="60" rows="20">@Output</textarea>
@code {
ElementReference inputElement;
string Output { get; set; }
public async Task ReadFile()
{
// This tricks the linker into keeping netstandard. Using the LinkerConfig.xml is currently not enough.
string HackToTrickLinkerIntoKeepingDotNetStandard = typeof(System.Web.HttpUtility).FullName.Substring(0,0);
Output = string.Empty;
this.StateHasChanged();
var nl = Environment.NewLine;
foreach (var file in await fileReaderService.CreateReference(inputElement).EnumerateFilesAsync())
{
var fileInfo = await file.ReadFileInfoAsync();
Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Name)}: {fileInfo.Name}{nl}";
Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Size)}: {fileInfo.Size}{nl}";
Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Type)}: {fileInfo.Type}{nl}";
Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.LastModifiedDate)}: {fileInfo.LastModifiedDate?.ToString() ?? "(N/A)"}{nl}";
Output += $"Reading file...";
this.StateHasChanged();
System.Reflection.Assembly assembly;
using (var fs = await file.CreateMemoryStreamAsync(4096))
{
Output += $"Done reading file {fileInfo.Name}.{nl}";
Output += $"Loading assembly...{nl}";
this.StateHasChanged();
try
{
assembly = System.Reflection.Assembly.Load(fs.ToArray());
Output += $"Assembly loaded.{nl}";
Output += $"Assembly FullName:{assembly.FullName}{nl}";
this.StateHasChanged();
}
catch (Exception e)
{
Output += $"Assembly load failed: {e}";
this.StateHasChanged();
return;
}
}
Output += $"Loading assembly contents...{nl}";
try
{
Output += $"Public Types:{nl}";
Output += string.Join(nl, assembly.GetExportedTypes().Select(x => x.FullName));
}
catch (Exception e)
{
Output += $"Assembly loading of types failed: {e}";
this.StateHasChanged();
return;
}
}
}
}