Fix BaseEventState.MemberwiseClone() to pass Parent to Activator.CreateInstance (#3361)

* Fix BaseEventState.MemberwiseClone() to pass Parent to Activator.CreateInstance

Co-authored-by: romanett <7413710+romanett@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: romanett <7413710+romanett@users.noreply.github.com>
This commit is contained in:
Copilot
2025-12-04 15:34:51 +01:00
committed by GitHub
co-authored by romanett
parent 30f4fa26ab
commit b8ed3cc782
2 changed files with 56 additions and 1 deletions
@@ -83,7 +83,7 @@ namespace Opc.Ua
/// </returns>
public new object MemberwiseClone()
{
var clone = (BaseEventState)Activator.CreateInstance(GetType());
var clone = (BaseEventState)Activator.CreateInstance(GetType(), Parent);
return CloneChildren(clone);
}
}
@@ -139,4 +139,59 @@ namespace Opc.Ua.Core.Tests.Stack.State
return CreateDefaultNodeStateType(systemType) is NodeState;
}
}
/// <summary>
/// Tests for BaseEventState.MemberwiseClone.
/// </summary>
[TestFixture]
[Category("BaseEventState")]
[SetCulture("en-us")]
[SetUICulture("en-us")]
[Parallelizable]
public class BaseEventStateTests
{
/// <summary>
/// Verify that MemberwiseClone works correctly for BaseEventState.
/// </summary>
[Test]
public void MemberwiseCloneBaseEventStateSucceeds()
{
var parent = new BaseObjectState(null);
var eventState = new BaseEventState(parent);
var clone = (BaseEventState)eventState.MemberwiseClone();
Assert.That(clone, Is.Not.Null);
Assert.That(clone.Parent, Is.SameAs(parent));
}
/// <summary>
/// Verify that MemberwiseClone works correctly for BaseEventState with null parent.
/// </summary>
[Test]
public void MemberwiseCloneBaseEventStateWithNullParentSucceeds()
{
var eventState = new BaseEventState(null);
var clone = (BaseEventState)eventState.MemberwiseClone();
Assert.That(clone, Is.Not.Null);
Assert.That(clone.Parent, Is.Null);
}
/// <summary>
/// Verify that Clone works correctly for derived event types.
/// </summary>
[Test]
public void CloneNonExclusiveLimitAlarmStateSucceeds()
{
var parent = new BaseObjectState(null);
var alarmState = new NonExclusiveLimitAlarmState(parent);
var clone = (NonExclusiveLimitAlarmState)alarmState.Clone();
Assert.That(clone, Is.Not.Null);
Assert.That(clone.Parent, Is.SameAs(parent));
}
}
}