|
8 | 8 | using CadAddinManager.Model; |
9 | 9 | using CadAddinManager.View; |
10 | 10 | using CadAddinManager.ViewModel; |
| 11 | +using RevitAddinManager.Model; |
11 | 12 | using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application; |
12 | 13 | using Exception = System.Exception; |
13 | 14 | using MessageBox = System.Windows.MessageBox; |
@@ -39,7 +40,74 @@ public string ActiveTempFolder |
39 | 40 | } |
40 | 41 |
|
41 | 42 |
|
| 43 | +#if A25 |
42 | 44 | public void RunActiveCommand() |
| 45 | + { |
| 46 | + AssemLoader assemLoader = new AssemLoader(); |
| 47 | + var filePath = _activeCmd.FilePath; |
| 48 | + if (!File.Exists(filePath)) |
| 49 | + { |
| 50 | + MessageBox.Show("File not found: " + filePath, DefaultSetting.AppName, MessageBoxButton.OK, |
| 51 | + MessageBoxImage.Error); |
| 52 | + return; |
| 53 | + } |
| 54 | + var alc = new AssemblyLoadContext(filePath); |
| 55 | + Stream stream = null; |
| 56 | + try |
| 57 | + { |
| 58 | + |
| 59 | + |
| 60 | + stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); |
| 61 | + Assembly assembly = alc.LoadFromStream(stream); |
| 62 | + //var assembly = assemLoader.LoadAddinsToTempFolder(filePath, false); |
| 63 | + WeakReference alcWeakRef = new WeakReference(alc, trackResurrection: true); |
| 64 | + Type[] types = assembly.GetTypes(); |
| 65 | + foreach (Type type in types) |
| 66 | + { |
| 67 | + List<MethodInfo> methodInfos = type.GetMethods().ToList(); |
| 68 | + foreach (MethodInfo methodInfo in methodInfos) |
| 69 | + { |
| 70 | + CommandMethodAttribute commandAtt = (CommandMethodAttribute)methodInfo |
| 71 | + .GetCustomAttributes(typeof(CommandMethodAttribute), false) |
| 72 | + .FirstOrDefault(); |
| 73 | + string fullName = string.Join(".", methodInfo.DeclaringType.Name, methodInfo.Name); |
| 74 | + if (commandAtt != null && fullName == Instance.ActiveCmdItem.FullClassName) |
| 75 | + { |
| 76 | + Invoke(methodInfo); |
| 77 | + alc.Unload(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + int counter = 0; |
| 82 | + for (counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++) |
| 83 | + { |
| 84 | + alc = null; |
| 85 | + GC.Collect(); |
| 86 | + GC.WaitForPendingFinalizers(); |
| 87 | + } |
| 88 | + stream.Close(); |
| 89 | + } |
| 90 | + catch (Exception e) |
| 91 | + { |
| 92 | + Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(e.ToString()); |
| 93 | + alc?.Unload(); |
| 94 | + WeakReference alcWeakRef = new WeakReference(alc, trackResurrection: true); |
| 95 | + for (int counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++) |
| 96 | + { |
| 97 | + alc = null; |
| 98 | + GC.Collect(); |
| 99 | + GC.WaitForPendingFinalizers(); |
| 100 | + } |
| 101 | + stream?.Close(); |
| 102 | + } |
| 103 | + // finally |
| 104 | + // { |
| 105 | + // assemLoader.UnhookAssemblyResolve(); |
| 106 | + // assemLoader.CopyGeneratedFilesBack(); |
| 107 | + // } |
| 108 | + } |
| 109 | +#else |
| 110 | + public void RunActiveCommand() |
43 | 111 | { |
44 | 112 | AssemLoader assemLoader = new AssemLoader(); |
45 | 113 | var filePath = _activeCmd.FilePath; |
@@ -85,6 +153,8 @@ public void RunActiveCommand() |
85 | 153 | assemLoader.CopyGeneratedFilesBack(); |
86 | 154 | } |
87 | 155 | } |
| 156 | +#endif |
| 157 | + |
88 | 158 |
|
89 | 159 | void Invoke(MethodInfo methodInfo) |
90 | 160 | { |
|
0 commit comments