Skip to content

Commit

Permalink
Load assemblies from memory, update test to dump fields and item prop…
Browse files Browse the repository at this point in the history
…erties
  • Loading branch information
MikeHillberg committed Oct 27, 2020
1 parent 9450aa3 commit 8aaa1c7
Show file tree
Hide file tree
Showing 13 changed files with 489 additions and 37 deletions.
80 changes: 79 additions & 1 deletion MiddleweightReflection.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,108 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiddleweightReflection", "MiddleweightReflection\MiddleweightReflection.csproj", "{ACAEDCBA-8C46-4135-8577-30D37B86AE01}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiddleweightReflection", "MiddleweightReflection\MiddleweightReflection.csproj", "{ACAEDCBA-8C46-4135-8577-30D37B86AE01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "Test\UnitTests.csproj", "{C5148951-A297-462F-BD72-0B3AA43F7215}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestSampleAssembly", "Test\UnitTestSampleAssembly\UnitTestSampleAssembly.csproj", "{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestWinRTComponent", "Test\UnitTestWinRTComponent\UnitTestWinRTComponent.csproj", "{95C612FE-E84C-4923-9AB3-CD305B4F5C84}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|ARM = Debug|ARM
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|ARM = Release|ARM
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|ARM.ActiveCfg = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|ARM.Build.0 = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|ARM64.Build.0 = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|x64.ActiveCfg = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|x64.Build.0 = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|x86.ActiveCfg = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Debug|x86.Build.0 = Debug|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|Any CPU.Build.0 = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|ARM.ActiveCfg = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|ARM.Build.0 = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|ARM64.ActiveCfg = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|ARM64.Build.0 = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|x64.ActiveCfg = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|x64.Build.0 = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|x86.ActiveCfg = Release|Any CPU
{ACAEDCBA-8C46-4135-8577-30D37B86AE01}.Release|x86.Build.0 = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|ARM.ActiveCfg = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|ARM.Build.0 = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|ARM64.Build.0 = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|x64.ActiveCfg = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|x64.Build.0 = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|x86.ActiveCfg = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Debug|x86.Build.0 = Debug|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|Any CPU.Build.0 = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|ARM.ActiveCfg = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|ARM.Build.0 = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|ARM64.ActiveCfg = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|ARM64.Build.0 = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|x64.ActiveCfg = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|x64.Build.0 = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|x86.ActiveCfg = Release|Any CPU
{C5148951-A297-462F-BD72-0B3AA43F7215}.Release|x86.Build.0 = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|ARM.ActiveCfg = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|ARM.Build.0 = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|ARM64.Build.0 = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|x64.ActiveCfg = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|x64.Build.0 = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|x86.ActiveCfg = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Debug|x86.Build.0 = Debug|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|Any CPU.Build.0 = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|ARM.ActiveCfg = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|ARM.Build.0 = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|ARM64.ActiveCfg = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|ARM64.Build.0 = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|x64.ActiveCfg = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|x64.Build.0 = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|x86.ActiveCfg = Release|Any CPU
{65E7C87B-7E7B-4EEF-A6AA-5B8CD2F9D265}.Release|x86.Build.0 = Release|Any CPU
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|ARM.ActiveCfg = Debug|ARM
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|ARM.Build.0 = Debug|ARM
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|ARM64.ActiveCfg = Debug|ARM64
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|ARM64.Build.0 = Debug|ARM64
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|x64.ActiveCfg = Debug|x64
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|x64.Build.0 = Debug|x64
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|x86.ActiveCfg = Debug|x86
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Debug|x86.Build.0 = Debug|x86
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|Any CPU.Build.0 = Release|Any CPU
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|ARM.ActiveCfg = Release|ARM
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|ARM.Build.0 = Release|ARM
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|ARM64.ActiveCfg = Release|ARM64
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|ARM64.Build.0 = Release|ARM64
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|x64.ActiveCfg = Release|x64
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|x64.Build.0 = Release|x64
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|x86.ActiveCfg = Release|x86
{95C612FE-E84C-4923-9AB3-CD305B4F5C84}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
60 changes: 44 additions & 16 deletions MiddleweightReflection/MrLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ public MrLoadContext()
LoadFromAssemblyName("mscorlib", implicitLoad: true);
}

/// <summary>
/// A MRLoadContext into which can be loaded assemblies. If useWinRTProjections is set,
/// some types in a WinMD will be converted, for example IVector to IList.
/// </summary>
/// <param name="useWinRTProjections"></param>
public MrLoadContext(bool useWinRTProjections)
{
if(useWinRTProjections)
{
MetadataReaderOptions = MetadataReaderOptions.ApplyWindowsRuntimeProjections;
}

LoadFromAssemblyName("mscorlib", implicitLoad: true);
}

/// <summary>
/// Call this after loading assemblies, after which the LoadedAssemblies property will become non-null
/// </summary>
Expand All @@ -48,21 +63,6 @@ public void FinishLoading()

}

/// <summary>
/// A MRLoadContext into which can be loaded assemblies. If useWinRTProjections is set,
/// some types in a WinMD will be converted, for example IVector to IList.
/// </summary>
/// <param name="useWinRTProjections"></param>
public MrLoadContext(bool useWinRTProjections = true)
{
if(!useWinRTProjections)
{
MetadataReaderOptions = MetadataReaderOptions.None;
}

LoadFromAssemblyName("mscorlib", implicitLoad: true);
}

public delegate string AssemblyPathFromNameCallback(string assemblyName);
public AssemblyPathFromNameCallback AssemblyPathFromName { get; set; }
//public delegate string AssemblyPathFromNameCallback(string assemblyName);
Expand Down Expand Up @@ -188,6 +188,24 @@ public MrAssembly LoadAssemblyFromPath(string path)
return LoadFromReader(reader, name, path, implicitLoad: false);
}

/// <summary>
/// Load an assembly from memory. If already loaded, return that.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public MrAssembly LoadAssemblyFromBytes(byte[] buffer)
{
var reader = CreateReaderFromBytes(buffer);
var name = reader.GetString(reader.GetAssemblyDefinition().Name);

if (_loadedAssemblies.TryGetValue(name, out var assembly))
{
return assembly;
}

return LoadFromReader(reader, name, null, implicitLoad: false);
}

/// <summary>
/// Load given the assembly name. If already loaded, return that.
/// </summary>
Expand Down Expand Up @@ -326,13 +344,23 @@ void CreateReaderFromAssemblyName(string requestedName, out MetadataReader reade
}

/// <summary>
/// Create the System.Reflection.Metadata MetadataReader
/// Create the System.Reflection.Metadata MetadataReader give the path to an assembly
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
unsafe MetadataReader CreateReaderFromPath(string path)
{
var buffer = File.ReadAllBytes(path);
return CreateReaderFromBytes(buffer);
}

/// <summary>
/// Create the System.Reflection.Metadata MetadataReader given an in-memory assembly
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
unsafe MetadataReader CreateReaderFromBytes(byte[] buffer)
{
var pinnedHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
var headers = new PEHeaders(new MemoryStream(buffer));
var startOffset = headers.MetadataStartOffset;
Expand Down
56 changes: 56 additions & 0 deletions Test/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Test/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,16 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="ExpectdProjectedWinRT" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ExpectdProjectedWinRT.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="ExpectedOutput" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ExpectedOutput.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="ExpectedUnprojectedWinRT" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ExpectedUnprojectedWinRT.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="UnitTestWinRTComponent" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\UnitTestWinRTComponent.winmd;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
33 changes: 33 additions & 0 deletions Test/Resources/ExpectdProjectedWinRT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

class <Module>
NotPublic

public class UnitTestWinRTComponent.WinRTComponent : System.Object
AutoLayout, AnsiClass, Class, Public, Sealed, BeforeFieldInit
[MarshalingBehaviorAttribute]
[ThreadingAttribute]
[VersionAttribute(16777216)]
[ActivatableAttribute(16777216)]
UnitTestWinRTComponent.IWinRTComponentClass
UnitTestWinRTComponent.WinRTComponent()
System.Collections.Generic.IList<System.String> StringList { get; set; }

class UnitTestWinRTComponent.<WinRT>WinRTComponent : System.Object
AutoLayout, AnsiClass, Class, Sealed, Import, WindowsRuntime, BeforeFieldInit
[MarshalingBehaviorAttribute]
[ThreadingAttribute]
[VersionAttribute(16777216)]
[CompilerGeneratedAttribute]
[ActivatableAttribute(16777216)]
UnitTestWinRTComponent.IWinRTComponentClass
Windows.Foundation.IStringable
UnitTestWinRTComponent.<WinRT>WinRTComponent()
System.Collections.Generic.IList<System.String> StringList { get; set; }

interface UnitTestWinRTComponent.IWinRTComponentClass
AutoLayout, AnsiClass, Class, ClassSemanticsMask, Abstract, WindowsRuntime
[CompilerGeneratedAttribute]
[GuidAttribute(595258099, 43547, 21940, 123, 11, 53, 220, 245, 117, 190, 210)]
[VersionAttribute(16777216)]
[ExclusiveToAttribute(UnitTestWinRTComponent.WinRTComponent)]
System.Collections.Generic.IList<System.String> StringList { get; set; }
33 changes: 33 additions & 0 deletions Test/Resources/ExpectedUnprojectedWinRT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

class <Module>
NotPublic

class UnitTestWinRTComponent.<CLR>WinRTComponent : System.Object
AutoLayout, AnsiClass, Class, Sealed, SpecialName, BeforeFieldInit
[MarshalingBehaviorAttribute]
[ThreadingAttribute]
[VersionAttribute(16777216)]
[ActivatableAttribute(16777216)]
UnitTestWinRTComponent.IWinRTComponentClass
UnitTestWinRTComponent.<CLR>WinRTComponent()
System.Collections.Generic.IList<System.String> StringList { get; set; }

public class UnitTestWinRTComponent.WinRTComponent : System.Object
AutoLayout, AnsiClass, Class, Public, Sealed, WindowsRuntime, BeforeFieldInit
[MarshalingBehaviorAttribute]
[ThreadingAttribute]
[VersionAttribute(16777216)]
[CompilerGeneratedAttribute]
[ActivatableAttribute(16777216)]
UnitTestWinRTComponent.IWinRTComponentClass
Windows.Foundation.IStringable
UnitTestWinRTComponent.WinRTComponent()
Windows.Foundation.Collections.IVector<System.String> StringList { get; set; }

interface UnitTestWinRTComponent.IWinRTComponentClass
AutoLayout, AnsiClass, Class, ClassSemanticsMask, Abstract, WindowsRuntime
[CompilerGeneratedAttribute]
[GuidAttribute(595258099, 43547, 21940, 123, 11, 53, 220, 245, 117, 190, 210)]
[VersionAttribute(16777216)]
[ExclusiveToAttribute(UnitTestWinRTComponent.WinRTComponent)]
Windows.Foundation.Collections.IVector<System.String> StringList { get; set; }
Binary file added Test/Resources/UnitTestWinRTComponent.winmd
Binary file not shown.
Loading

0 comments on commit 8aaa1c7

Please sign in to comment.