Skip to content

Commit c326025

Browse files
authored
Merge f5fc478 into df1251e
2 parents df1251e + f5fc478 commit c326025

File tree

4 files changed

+126
-25
lines changed

4 files changed

+126
-25
lines changed

src/ExtendedXmlSerializer/ReflectionModel/AssemblyLoader.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
using System.Collections.Immutable;
1+
using System;
2+
using System.Collections.Immutable;
23
using System.IO;
34
using System.Reflection;
45

56
namespace ExtendedXmlSerializer.ReflectionModel
67
{
78
sealed class AssemblyLoader : IAssemblyLoader
89
{
9-
readonly ImmutableArray<Assembly> _loaded;
1010
public static AssemblyLoader Default { get; } = new AssemblyLoader();
1111

12-
AssemblyLoader() : this(AssemblyProvider.Default.Get()
13-
.ToImmutableArray()) {}
12+
AssemblyLoader() : this(AssemblyPath.Default.Get, AssemblyProvider.Default.Get().ToImmutableArray()) {}
1413

15-
AssemblyLoader(ImmutableArray<Assembly> loaded) => _loaded = loaded;
14+
AssemblyLoader(Func<string, string> path, ImmutableArray<Assembly> loaded)
15+
{
16+
_path = path;
17+
_loaded = loaded;
18+
}
19+
20+
readonly Func<string, string> _path;
21+
readonly ImmutableArray<Assembly> _loaded;
1622

1723
public Assembly Get(string parameter)
1824
{
@@ -22,18 +28,25 @@ public Assembly Get(string parameter)
2228
}
2329
catch (FileNotFoundException)
2430
{
25-
var length = _loaded.Length;
26-
for (var i = 0; i < length; i++)
31+
try
2732
{
28-
var assembly = _loaded[i];
29-
if (assembly.GetName()
30-
.Name == parameter)
33+
return Assembly.LoadFile(_path(parameter));
34+
}
35+
catch
36+
{
37+
var length = _loaded.Length;
38+
for (var i = 0; i < length; i++)
3139
{
32-
return assembly;
40+
var assembly = _loaded[i];
41+
if (assembly.GetName()
42+
.Name == parameter)
43+
{
44+
return assembly;
45+
}
3346
}
34-
}
3547

36-
throw;
48+
throw;
49+
}
3750
}
3851
}
3952
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using ExtendedXmlSerializer.Core.Sources;
2+
using System;
3+
using System.Runtime.InteropServices;
4+
5+
namespace ExtendedXmlSerializer.ReflectionModel
6+
{
7+
/// <summary>
8+
/// Attribution: https://stackoverflow.com/a/6131116/10340424
9+
/// </summary>
10+
sealed class AssemblyPath : IParameterizedSource<string, string>
11+
{
12+
public static AssemblyPath Default { get; } = new AssemblyPath();
13+
14+
AssemblyPath() {}
15+
16+
public string Get(string parameter)
17+
{
18+
if (parameter == null)
19+
{
20+
throw new ArgumentNullException(nameof(parameter));
21+
}
22+
23+
var finalName = parameter;
24+
var aInfo = new AssemblyInfo {cchBuf = 1024}; // should be fine...
25+
aInfo.currentAssemblyPath = new string('\0', aInfo.cchBuf);
26+
27+
var hr = CreateAssemblyCache(out var ac, 0);
28+
if (hr >= 0)
29+
{
30+
hr = ac.QueryAssemblyInfo(0, finalName, ref aInfo);
31+
if (hr < 0)
32+
{
33+
return null;
34+
}
35+
}
36+
37+
return aInfo.currentAssemblyPath;
38+
}
39+
40+
[DllImport("fusion.dll")]
41+
static extern int CreateAssemblyCache(out IAssemblyCache ppAsmCache, int reserved);
42+
43+
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
44+
interface IAssemblyCache
45+
{
46+
void Reserved0();
47+
48+
[PreserveSig]
49+
int QueryAssemblyInfo(int flags, [MarshalAs(UnmanagedType.LPWStr)] string assemblyName,
50+
ref AssemblyInfo assemblyInfo);
51+
}
52+
53+
[StructLayout(LayoutKind.Sequential)]
54+
struct AssemblyInfo
55+
{
56+
readonly int cbAssemblyInfo;
57+
readonly int assemblyFlags;
58+
readonly long assemblySizeInKB;
59+
[MarshalAs(UnmanagedType.LPWStr)] public string currentAssemblyPath;
60+
public int cchBuf; // size of path buf.
61+
}
62+
}
63+
}

test/ExtendedXmlSerializer.Tests.ReportedIssues/ExtendedXmlSerializer.Tests.ReportedIssues.csproj

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@
2929
<PackageReference Include="xunit" Version="2.4.1" />
3030
</ItemGroup>
3131

32-
<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp2.1' ">
33-
<Reference Include="WindowsBase" />
34-
<Reference Include="System.Data">
35-
<Private>True</Private>
36-
</Reference>
37-
</ItemGroup>
38-
39-
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
40-
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
41-
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
42-
</ItemGroup>
43-
4432
<ItemGroup Condition=" '$(TargetFramework)' != 'net471' ">
4533
<ProjectReference Include="..\..\src\ExtendedXmlSerializer\ExtendedXmlSerializer.csproj" />
4634
</ItemGroup>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using ExtendedXmlSerializer.Configuration;
2+
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
3+
using FluentAssertions;
4+
using System.Drawing;
5+
using Xunit;
6+
7+
namespace ExtendedXmlSerializer.Tests.ReportedIssues
8+
{
9+
public sealed class Issue366Tests
10+
{
11+
[Fact]
12+
void Verify()
13+
{
14+
var instance = new Model();
15+
new ConfigurationContainer().Create()
16+
.ForTesting()
17+
.Cycle(instance)
18+
.Should()
19+
.BeEquivalentTo(instance);
20+
}
21+
22+
public class Model
23+
{
24+
public Model()
25+
{
26+
NestedModel = new NestedModel();
27+
}
28+
29+
public NestedModel NestedModel { get; set; }
30+
}
31+
32+
public class NestedModel
33+
{
34+
public Size Size { get; set; }
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)