Skip to content

Commit 8a23435

Browse files
authored
Merge pull request #21 from fbayon/feature/netcore2
Fixed assembly load and remove console.writeline as log
2 parents c6aa49a + b81df5a commit 8a23435

File tree

2 files changed

+52
-43
lines changed

2 files changed

+52
-43
lines changed

source/loaders/cs_loader/netcore/source/Loader.cs

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,41 @@ namespace CSLoader
1616
{
1717
public class Loader
1818
{
19+
[System.Diagnostics.Conditional("DEBUG_FILE")]
20+
private static void Log(string text){
1921

22+
var message= $"{DateTime.Now.Ticks}: {text}\n";
23+
System.IO.File.AppendAllText("log.txt", message);
24+
}
25+
26+
private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
27+
{
28+
Assembly asm = null;
29+
Log("MyResolveEventHandler" + paths.Count.ToString());
30+
foreach (var path in paths)
31+
{
32+
try
33+
{
34+
var p =path + "\\" + args.Name + ".dll";
35+
Log(p);
36+
37+
asm = Assembly.LoadFile(p);
38+
if (asm != null)
39+
{
40+
return asm;
41+
}else{
42+
Log("NULL");
43+
}
44+
}
45+
catch (Exception ex)
46+
{
47+
Log("EX! " + ex.Message);
48+
49+
}
50+
}
51+
52+
return asm;
53+
}
2054
public static void Main(string[] args)
2155
{
2256

@@ -26,7 +60,9 @@ public static void Main(string[] args)
2660

2761
static Loader()
2862
{
29-
AssemblyLoadContext.Default.Resolving += Context_Resolving;
63+
Log("start");
64+
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
65+
AppDomain.CurrentDomain.TypeResolve+= new ResolveEventHandler(MyResolveEventHandler);
3066
Init();
3167
}
3268

@@ -74,7 +110,6 @@ public static ReflectFunction[] GetFunctionsInternal()
74110
{
75111
return loader.Functions();
76112
}
77-
78113
public static void GetFunctions(ref int count, IntPtr p)
79114
{
80115
var f = loader.Functions();
@@ -189,11 +224,14 @@ public bool LoadFromSourceFunctions(string[] source)
189224

190225
string assemblyName = Path.GetRandomFileName();
191226

192-
MetadataReference[] references = new MetadataReference[]
193-
{
194-
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
195-
MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location)
196-
};
227+
MetadataReference[] references;
228+
229+
var mainPath = Path.GetDirectoryName( typeof(object).GetTypeInfo().Assembly.Location) +"/";
230+
var assemblyFiles = System.IO.Directory.GetFiles(mainPath,"*.dll");
231+
232+
assemblyFiles = assemblyFiles.Concat(System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory,"*.dll")).Distinct().ToArray();
233+
234+
references = assemblyFiles.Select(x=> MetadataReference.CreateFromFile(x)).ToArray();
197235

198236
CSharpCompilation compilation = CSharpCompilation.Create(
199237
assemblyName,
@@ -213,15 +251,13 @@ public bool LoadFromSourceFunctions(string[] source)
213251

214252
foreach (Diagnostic diagnostic in failures)
215253
{
216-
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
254+
Log($"{diagnostic.Id}: {diagnostic.GetMessage()}");
217255
}
218256
}
219257
else
220258
{
221259
ms.Seek(0, SeekOrigin.Begin);
222-
223-
AssemblyLoadContext context = AssemblyLoadContext.Default;
224-
assembly = context.LoadFromStream(ms);
260+
assembly = Assembly.Load(ms.ToArray());
225261
}
226262
}
227263

@@ -240,7 +276,6 @@ public bool LoadFromSourceFunctions(string[] source)
240276

241277
public static bool LoadFromAssembly(string assemblyFile)
242278
{
243-
AssemblyLoadContext context = AssemblyLoadContext.Default;
244279
Assembly asm = null;
245280

246281
string path = System.IO.Path.GetDirectoryName(assemblyFile);
@@ -252,7 +287,7 @@ public static bool LoadFromAssembly(string assemblyFile)
252287

253288
try
254289
{
255-
asm = context.LoadFromAssemblyPath(assemblyFile);
290+
asm =Assembly.LoadFile(assemblyFile);
256291
}
257292
catch (Exception)
258293
{
@@ -261,7 +296,7 @@ public static bool LoadFromAssembly(string assemblyFile)
261296
{
262297
try
263298
{
264-
asm = context.LoadFromAssemblyName(new AssemblyName(System.IO.Path.GetFileNameWithoutExtension(assemblyFile)));
299+
asm = Assembly.Load(new AssemblyName(System.IO.Path.GetFileNameWithoutExtension(assemblyFile)));
265300
}
266301
catch (Exception)
267302
{
@@ -283,30 +318,6 @@ public static bool LoadFromAssembly(string assemblyFile)
283318
return false;
284319
}
285320

286-
private static Assembly Context_Resolving(AssemblyLoadContext context, AssemblyName name)
287-
{
288-
Assembly asm = null;
289-
290-
foreach (var path in paths)
291-
{
292-
try
293-
{
294-
asm = context.LoadFromAssemblyPath(path + "\\" + name.Name + ".dll");
295-
296-
if (asm != null)
297-
{
298-
return asm;
299-
}
300-
}
301-
catch (Exception ex)
302-
{
303-
Console.Error.WriteLine(ex.Message);
304-
}
305-
}
306-
307-
return asm;
308-
}
309-
310321
public static bool LoadFromAssemblyC([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string assemblyFile)
311322
{
312323
return LoadFromAssembly(assemblyFile);
@@ -377,7 +388,7 @@ public void LoadFunctions(Assembly assembly)
377388
}
378389
catch (Exception ex)
379390
{
380-
Console.Error.WriteLine(ex.Message);
391+
Log(ex.Message);
381392
}
382393

383394
return null;

source/loaders/cs_loader/netcore/source/project.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
<VersionPrefix>1.0.0</VersionPrefix>
77
<TargetFramework>netcoreapp2.2</TargetFramework>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9-
<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion>
10-
<AssetTargetFallback>$(AssetTargetFallback);dnxcore50;</AssetTargetFallback>
9+
1110
</PropertyGroup>
1211

1312
<ItemGroup>
14-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="1.3.0" />
15-
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
13+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.2.1" />
1614
</ItemGroup>
1715

1816
</Project>

0 commit comments

Comments
 (0)