Document of c-modernization-kit (calc dotnet wrapper) 1.0.0

Loading...
Searching...
No Matches
ModuleInitializer.cs
Go to the documentation of this file.
1#pragma warning disable 1587
18#pragma warning restore 1587
19
20 using System;
21 using System.IO;
22 using System.Reflection;
23 using System.Runtime.CompilerServices;
24
25 namespace CalcApp
26{
30 internal static class ModuleInitializer
31 {
35 [ModuleInitializer]
36 public static void Initialize()
37 {
38 AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
39 }
40
45 private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
46 {
47 var assemblyName = new AssemblyName(args.Name);
48 var dllName = assemblyName.Name + ".dll";
49
50 // 環境変数名とパス区切り文字をプラットフォームに応じて決定
51 string libraryPathEnv;
52 char pathSeparator;
53
54 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
55 {
56 libraryPathEnv = "PATH";
57 pathSeparator = ';';
58 }
59 else
60 {
61 libraryPathEnv = "LD_LIBRARY_PATH";
62 pathSeparator = ':';
63 }
64
65 // 環境変数を取得
66 var libraryPath = Environment.GetEnvironmentVariable(libraryPathEnv);
67 if (string.IsNullOrEmpty(libraryPath))
68 {
69 return null;
70 }
71
72 // 各パスを探索
73 var paths = libraryPath.Split(pathSeparator, StringSplitOptions.RemoveEmptyEntries);
74 foreach (var path in paths)
75 {
76 var assemblyPath = Path.Combine(path.Trim(), dllName);
77 if (File.Exists(assemblyPath))
78 {
79 return Assembly.LoadFrom(assemblyPath);
80 }
81 }
82
83 return null;
84 }
85 }
86}
CalcApp
Definition ModuleInitializer.cs:26