Skip to content

Commit fa07758

Browse files
committed
fix e duplicate issue with R25
1 parent 23ba9f3 commit fa07758

File tree

2 files changed

+82
-70
lines changed

2 files changed

+82
-70
lines changed

CadAddinManager/Command/AddInManagerCommand.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ public class AddInManagerFaceLess : ICadCommand
2626
[Autodesk.AutoCAD.Runtime.CommandMethod("AddInManagerFaceLess",CommandFlags.Session)]
2727
public override void Execute()
2828
{
29-
AddinManagerBase.Instance.ExecuteCommand(true);
29+
try
30+
{
31+
AddinManagerBase.Instance.ExecuteCommand(true);
32+
}
33+
catch (Exception e)
34+
{
35+
MessageBox.Show(e.ToString());
36+
Trace.WriteLine(e.Message);
37+
}
3038
}
3139
}
3240

CadAddinManager/Command/AddinManagerBase.cs

Lines changed: 73 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -36,74 +36,75 @@ public string ActiveTempFolder
3636
set => _activeTempFolder = value;
3737
}
3838

39-
40-
#if A25
41-
public void RunActiveCommand()
42-
{
43-
AssemLoader assemLoader = new AssemLoader();
44-
var filePath = _activeCmd.FilePath;
45-
if (!File.Exists(filePath))
46-
{
47-
MessageBox.Show("File not found: " + filePath, DefaultSetting.AppName, MessageBoxButton.OK,
48-
MessageBoxImage.Error);
49-
return;
50-
}
51-
var alc = new AssemblyLoadContext(filePath);
52-
Stream stream = null;
53-
try
54-
{
55-
56-
57-
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
58-
Assembly assembly = alc.LoadFromStream(stream);
59-
//var assembly = assemLoader.LoadAddinsToTempFolder(filePath, false);
60-
WeakReference alcWeakRef = new WeakReference(alc, trackResurrection: true);
61-
Type[] types = assembly.GetTypes();
62-
foreach (Type type in types)
63-
{
64-
List<MethodInfo> methodInfos = type.GetMethods().ToList();
65-
foreach (MethodInfo methodInfo in methodInfos)
66-
{
67-
CommandMethodAttribute commandAtt = (CommandMethodAttribute)methodInfo
68-
.GetCustomAttributes(typeof(CommandMethodAttribute), false)
69-
.FirstOrDefault();
70-
string fullName = string.Join(".", methodInfo.DeclaringType.Name, methodInfo.Name);
71-
if (commandAtt != null && fullName == Instance.ActiveCmdItem.FullClassName)
72-
{
73-
Invoke(methodInfo);
74-
alc.Unload();
75-
}
76-
}
77-
}
78-
int counter = 0;
79-
for (counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++)
80-
{
81-
alc = null;
82-
GC.Collect();
83-
GC.WaitForPendingFinalizers();
84-
}
85-
stream.Close();
86-
}
87-
catch (Exception e)
88-
{
89-
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(e.ToString());
90-
alc?.Unload();
91-
WeakReference alcWeakRef = new WeakReference(alc, trackResurrection: true);
92-
for (int counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++)
93-
{
94-
alc = null;
95-
GC.Collect();
96-
GC.WaitForPendingFinalizers();
97-
}
98-
stream?.Close();
99-
}
100-
// finally
101-
// {
102-
// assemLoader.UnhookAssemblyResolve();
103-
// assemLoader.CopyGeneratedFilesBack();
104-
// }
105-
}
106-
#else
39+
//
40+
// #if A25
41+
// public void RunActiveCommand()
42+
// {
43+
// var filePath = _activeCmd.FilePath;
44+
// if (!File.Exists(filePath))
45+
// {
46+
// MessageBox.Show("File not found: " + filePath, DefaultSetting.AppName, MessageBoxButton.OK,
47+
// MessageBoxImage.Error);
48+
// return;
49+
// }
50+
// var alc = new AssemblyLoadContext(filePath);
51+
// Stream stream = null;
52+
// try
53+
// {
54+
//
55+
//
56+
// stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
57+
// Assembly assembly = alc.LoadFromStream(stream);
58+
// //var assembly = assemLoader.LoadAddinsToTempFolder(filePath, false);
59+
// WeakReference alcWeakRef = new WeakReference(alc, trackResurrection: true);
60+
// Type[] types = assembly.GetTypes();
61+
// foreach (Type type in types)
62+
// {
63+
// List<MethodInfo> methodInfos = type.GetMethods().ToList();
64+
// foreach (MethodInfo methodInfo in methodInfos)
65+
// {
66+
// CommandMethodAttribute commandAtt = (CommandMethodAttribute)methodInfo
67+
// .GetCustomAttributes(typeof(CommandMethodAttribute), false)
68+
// .FirstOrDefault();
69+
// string fullName = string.Join(".", methodInfo.DeclaringType.Name, methodInfo.Name);
70+
// if (commandAtt != null && fullName == Instance.ActiveCmdItem.FullClassName)
71+
// {
72+
// // clone the assembly to
73+
// // DO HERE
74+
// Invoke(methodInfo);
75+
// alc.Unload();
76+
// }
77+
// }
78+
// }
79+
// int counter = 0;
80+
// for (counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++)
81+
// {
82+
// alc = null;
83+
// GC.Collect();
84+
// GC.WaitForPendingFinalizers();
85+
// }
86+
// stream.Close();
87+
// }
88+
// catch (Exception e)
89+
// {
90+
// //Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(e.ToString());
91+
// alc?.Unload();
92+
// WeakReference alcWeakRef = new WeakReference(alc, trackResurrection: true);
93+
// for (int counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++)
94+
// {
95+
// alc = null;
96+
// GC.Collect();
97+
// GC.WaitForPendingFinalizers();
98+
// }
99+
// stream?.Close();
100+
// }
101+
// // finally
102+
// // {
103+
// // assemLoader.UnhookAssemblyResolve();
104+
// // assemLoader.CopyGeneratedFilesBack();
105+
// // }
106+
// }
107+
// #else
107108
public void RunActiveCommand()
108109
{
109110
AssemLoader assemLoader = new AssemLoader();
@@ -150,7 +151,6 @@ public void RunActiveCommand()
150151
assemLoader.CopyGeneratedFilesBack();
151152
}
152153
}
153-
#endif
154154

155155

156156
void Invoke(MethodInfo methodInfo)
@@ -176,6 +176,10 @@ void Invoke(MethodInfo methodInfo)
176176
{
177177
doc.Editor.WriteMessage(e.Message);
178178
}
179+
catch (Exception e)
180+
{
181+
Console.WriteLine(e.StackTrace);
182+
}
179183
}
180184
}
181185

0 commit comments

Comments
 (0)