From bcb5b1d28ae0ac1a7ee35954560516d187039460 Mon Sep 17 00:00:00 2001 From: AJIOB Date: Sat, 3 Sep 2022 19:01:49 +0300 Subject: [PATCH] Optimizing Is/As calls --- TitaniumAS.Opc.Client/Da/OpcDaGroup.cs | 21 +++++++++++++++++---- TitaniumAS.Opc.Client/Da/OpcDaServer.cs | 13 +++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/TitaniumAS.Opc.Client/Da/OpcDaGroup.cs b/TitaniumAS.Opc.Client/Da/OpcDaGroup.cs index 5616924..e575158 100644 --- a/TitaniumAS.Opc.Client/Da/OpcDaGroup.cs +++ b/TitaniumAS.Opc.Client/Da/OpcDaGroup.cs @@ -470,9 +470,9 @@ public TimeSpan KeepAlive } set { - if (!Is()) + if (!Is(out var v)) return; - _keepAlive = As().SetKeepAlive(value); + _keepAlive = v.SetKeepAlive(value); } } @@ -918,6 +918,19 @@ public bool Is() where T : ComWrapper return As() != null; } + /// + /// Determines whether this instance is of specified COM wrapper type. + /// + /// The COM wrapper type. + /// Casted to type value + /// true if this instance is of specified COM wrapper type; otherwise, false. + public bool Is(out T value) where T : ComWrapper + { + value = As(); + + return value != null; + } + private OpcDaItemResult[] CreateItemResults(IList itemDefinitions, OPCITEMDEF[] pItemArray, OPCITEMRESULT[] opcDaItemResults, HRESULT[] ppErrors, bool setGroup) { @@ -1047,9 +1060,9 @@ private HRESULT[] SetClientHandles(int[] serverHandles, int[] clientHandles) private TimeSpan RefreshKeepAlive() { - if (!Is()) + if (!Is(out var v)) return TimeSpan.Zero; - return As().GetKeepAlive(); + return v.GetKeepAlive(); } // Protected implementation of Dispose pattern. diff --git a/TitaniumAS.Opc.Client/Da/OpcDaServer.cs b/TitaniumAS.Opc.Client/Da/OpcDaServer.cs index 1adbeeb..bba6552 100644 --- a/TitaniumAS.Opc.Client/Da/OpcDaServer.cs +++ b/TitaniumAS.Opc.Client/Da/OpcDaServer.cs @@ -491,6 +491,19 @@ public bool Is() where T : ComWrapper return As() != null; } + /// + /// Determines whether this instance is of specified COM wrapper type. + /// + /// The COM wrapper type. + /// Casted to type value + /// true if this instance is of specified COM wrapper type; otherwise, false. + public bool Is(out T value) where T : ComWrapper + { + value = As(); + + return value != null; + } + private void RemoveAllGroups(bool rpcFailed = false) { foreach (OpcDaGroup opcDaGroup in _groups.ToArray())