Skip to content

Commit 4e65694

Browse files
author
Fraser Greenroyd
committed
Add try catch for Activator.CreateInstance
1 parent 7c19df5 commit 4e65694

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Settings_Engine/Query/GetSettings.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,30 @@ public static object GetSettings(Type type)
4141
if (!Global.BHoMSettings.TryGetValue(type, out settings))
4242
{
4343
BH.Engine.Base.Compute.RecordWarning($"Could not find settings of type {type} loaded in memory. Returning a default instance of the settings object instead.");
44-
return Activator.CreateInstance(type);
44+
ISettings obj = null;
45+
try
46+
{
47+
var activator = Activator.CreateInstance(type);
48+
49+
try
50+
{
51+
obj = activator as ISettings;
52+
}
53+
catch(Exception ex)
54+
{
55+
BH.Engine.Base.Compute.RecordError(ex, $"Could not cast object of type {activator.GetType()} to an ISettings object. Settings not saved in memory.");
56+
return activator;
57+
}
58+
}
59+
catch(Exception ex)
60+
{
61+
BH.Engine.Base.Compute.RecordError(ex, $"Could not create instance of object of type {type}.");
62+
return null;
63+
}
64+
65+
Global.BHoMSettings[type] = obj;
66+
67+
return obj;
4568
}
4669

4770
return settings;

0 commit comments

Comments
 (0)