Skip to content

Commit 8d17a34

Browse files
refactor: potential nullreferenceexception in basesettings constructor (#1212)
The constructor of `BaseSettings<T>` uses `Activator.CreateInstance(typeof(T))` to initialize the `Component` property. If `T` does not have a parameterless constructor or if `T` is an interface or abstract class, this will throw an exception at runtime, potentially causing a crash. Affected files: BaseSettings.cs Signed-off-by: BachDEV <1437214+bachdev@users.noreply.github.com> Co-authored-by: Sam <sam@spiritreader.eu>
1 parent 26cf8a3 commit 8d17a34

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

AutoDarkModeLib/ComponentSettings/BaseSettings.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919

2020
namespace AutoDarkModeLib.ComponentSettings;
2121

22-
public class BaseSettings<T> : ISwitchComponentSettings<T> where T : class
22+
public class BaseSettings<T> : ISwitchComponentSettings<T> where T : new()
2323
{
2424
public virtual bool Enabled { get; set; }
2525
public T? Component { get; set; }
2626
public BaseSettings()
27-
{
28-
if (typeof(T) == typeof(object)) Component = null;
29-
else Component = (T)Activator.CreateInstance(typeof(T));
27+
{
28+
Component = new T();
3029
}
3130
}

0 commit comments

Comments
 (0)