From 654dc9bd3917e3d6a8337bbdba41e7fd0b6b4d33 Mon Sep 17 00:00:00 2001 From: Blaise Taylor Date: Wed, 4 Feb 2026 10:55:11 -0500 Subject: [PATCH] AB#16 Fixing Code Quality Warnings - part 3. --- .../ComponentModel/Design/Helpers.cs | 4 +- .../CollectionMarkupSerializer.cs | 12 +- .../MarkupExtensionSerializer.cs | 25 ++-- .../Serialization/WorkflowMarkupSerializer.cs | 136 +++++++++--------- .../SynchronizationHandlesTypeConverter.cs | 6 +- .../CodeTypeReferenceSerializerTest.cs | 18 +-- .../TypeExtensionSerializerTest.cs | 4 +- ...SynchronizationHandlesTypeConverterTest.cs | 2 +- 8 files changed, 98 insertions(+), 109 deletions(-) diff --git a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Design/Helpers.cs b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Design/Helpers.cs index a81bb11..be75625 100644 --- a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Design/Helpers.cs +++ b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Design/Helpers.cs @@ -10,10 +10,9 @@ namespace LogicBuilder.Workflow.ComponentModel.Design { internal static class Helpers { - [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal static XmlWriter CreateXmlWriter(object output) { - XmlWriterSettings settings = new XmlWriterSettings + XmlWriterSettings settings = new() { Indent = true, IndentChars = ("\t"), @@ -32,7 +31,6 @@ internal static XmlWriter CreateXmlWriter(object output) } } - [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal static DesignerSerializationVisibility GetSerializationVisibility(MemberInfo memberInfo) { diff --git a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/CollectionMarkupSerializer.cs b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/CollectionMarkupSerializer.cs index 5b081c2..23a6b10 100644 --- a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/CollectionMarkupSerializer.cs +++ b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/CollectionMarkupSerializer.cs @@ -18,16 +18,14 @@ protected internal override IList GetChildren(WorkflowMarkupSerializationManager if (!IsValidCollectionType(obj.GetType())) throw new Exception(SR.GetString(SR.Error_SerializerTypeRequirement, obj.GetType().FullName, typeof(ICollection).FullName, typeof(ICollection<>).FullName)); - IEnumerable enumerable = obj as IEnumerable; - ArrayList arrayList = new ArrayList(); - foreach (object containedObj in enumerable) - arrayList.Add(containedObj); + IEnumerable enumerable = obj as IEnumerable ?? Enumerable.Empty(); + ArrayList arrayList = [.. enumerable]; return arrayList; } protected internal override PropertyInfo[] GetProperties(WorkflowMarkupSerializationManager serializationManager, object obj) { - return new PropertyInfo[] { }; + return []; } protected internal override bool ShouldSerializeValue(WorkflowMarkupSerializationManager serializationManager, object value) @@ -52,7 +50,7 @@ protected internal override void ClearChildren(WorkflowMarkupSerializationManage throw new Exception(SR.GetString(SR.Error_SerializerTypeRequirement, obj.GetType().FullName, typeof(ICollection).FullName, typeof(ICollection<>).FullName)); if (obj is ICollection) /*Updating from collection == null - appears to be a bug e.g. List of T passes IsValidCollectionType and implements System.Collections.ICollection.*/ - obj.GetType().InvokeMember("Clear", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, obj, new object[] { }, CultureInfo.InvariantCulture); + obj.GetType().InvokeMember("Clear", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, obj, [], CultureInfo.InvariantCulture); } protected internal override void AddChild(WorkflowMarkupSerializationManager serializationManager, object parentObj, object childObj) @@ -63,7 +61,7 @@ protected internal override void AddChild(WorkflowMarkupSerializationManager ser if (!IsValidCollectionType(parentObj.GetType())) throw new Exception(SR.GetString(SR.Error_SerializerTypeRequirement, parentObj.GetType().FullName, typeof(ICollection).FullName, typeof(ICollection<>).FullName)); - parentObj.GetType().InvokeMember("Add", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, parentObj, new object[] { childObj }, CultureInfo.InvariantCulture); + parentObj.GetType().InvokeMember("Add", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, parentObj, [childObj], CultureInfo.InvariantCulture); } internal static bool IsValidCollectionType(Type collectionType) diff --git a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/MarkupExtensionSerializer.cs b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/MarkupExtensionSerializer.cs index 0d55dba..7b857f6 100644 --- a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/MarkupExtensionSerializer.cs +++ b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/MarkupExtensionSerializer.cs @@ -53,8 +53,7 @@ protected internal sealed override string SerializeToString(WorkflowMarkupSerial int i = 0; foreach (object argValue in instanceDescriptor.Arguments) { - if (constructorArguments == null) - constructorArguments = new Dictionary(); + constructorArguments ??= []; // if (argValue == null) continue; @@ -70,7 +69,7 @@ protected internal sealed override string SerializeToString(WorkflowMarkupSerial else if (argValue is System.Type) { Type argType = argValue as Type; - if (argType.Assembly != null) + if (argType?.Assembly != null) { XmlQualifiedName typeQualifiedName = serializationManager.GetXmlQualifiedName(argType, out _); writer.WriteQualifiedName(XmlConvert.EncodeName(typeQualifiedName.Name), typeQualifiedName.Namespace); @@ -91,14 +90,16 @@ protected internal sealed override string SerializeToString(WorkflowMarkupSerial } } - List properties = new List(); - properties.AddRange(GetProperties(serializationManager, value)); - properties.AddRange(serializationManager.GetExtendedProperties(value)); + List properties = + [ + .. GetProperties(serializationManager, value), + .. serializationManager.GetExtendedProperties(value), + ]; foreach (PropertyInfo serializableProperty in properties) { if (Helpers.GetSerializationVisibility(serializableProperty) != DesignerSerializationVisibility.Hidden && serializableProperty.CanRead && serializableProperty.GetValue(value, null) != null) { - if (!(serializationManager.GetSerializer(serializableProperty.PropertyType, typeof(WorkflowMarkupSerializer)) is WorkflowMarkupSerializer propSerializer)) + if (serializationManager.GetSerializer(serializableProperty.PropertyType, typeof(WorkflowMarkupSerializer)) is not WorkflowMarkupSerializer propSerializer) { serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNotAvailable, serializableProperty.PropertyType.FullName))); continue; @@ -142,13 +143,13 @@ protected internal sealed override string SerializeToString(WorkflowMarkupSerial } else { - serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNoSerializeLogic, new object[] { serializableProperty.Name, value.GetType().FullName }))); + serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNoSerializeLogic, [serializableProperty.Name, value.GetType().FullName]))); } } } catch { - serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNoSerializeLogic, new object[] { serializableProperty.Name, value.GetType().FullName }))); + serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNoSerializeLogic, [serializableProperty.Name, value.GetType().FullName]))); continue; } finally @@ -165,9 +166,9 @@ protected internal sealed override string SerializeToString(WorkflowMarkupSerial protected virtual InstanceDescriptor GetInstanceDescriptor(WorkflowMarkupSerializationManager serializationManager, object value) { - return !(value is MarkupExtension markupExtension) + return value is not MarkupExtension markupExtension ? throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(MarkupExtension).FullName), "value") - : new InstanceDescriptor(markupExtension.GetType().GetConstructor(new Type[0]), null); + : new InstanceDescriptor(markupExtension.GetType().GetConstructor([]), null); } // more escaped characters can be consider here, hence a seperate fn instead of string.Replace @@ -176,7 +177,7 @@ private string CreateEscapedValue(string value) if (value == null) throw new ArgumentNullException("value"); - StringBuilder sb = new StringBuilder(64); + StringBuilder sb = new(64); int length = value.Length; for (int i = 0; i < length; i++) { diff --git a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/WorkflowMarkupSerializer.cs b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/WorkflowMarkupSerializer.cs index b187d12..536d544 100644 --- a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/WorkflowMarkupSerializer.cs +++ b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/Serialization/WorkflowMarkupSerializer.cs @@ -27,7 +27,7 @@ public object Deserialize(XmlReader reader) if (reader == null) throw new ArgumentNullException("reader"); - DesignerSerializationManager designerSerializationManager = new DesignerSerializationManager(); + DesignerSerializationManager designerSerializationManager = new(); using (designerSerializationManager.CreateSession()) { return Deserialize(designerSerializationManager, reader); @@ -94,7 +94,7 @@ public void Serialize(XmlWriter writer, object obj) if (writer == null) throw new ArgumentNullException("writer"); - DesignerSerializationManager designerSerializationManager = new DesignerSerializationManager(); + DesignerSerializationManager designerSerializationManager = new(); using (designerSerializationManager.CreateSession()) { Serialize(designerSerializationManager, writer, obj); @@ -111,7 +111,7 @@ public void Serialize(IDesignerSerializationManager serializationManager, XmlWri throw new ArgumentNullException("writer"); WorkflowMarkupSerializationManager markupSerializationManager = serializationManager as WorkflowMarkupSerializationManager ?? new WorkflowMarkupSerializationManager(serializationManager); - StringWriter xomlStringWriter = new StringWriter(CultureInfo.InvariantCulture); + StringWriter xomlStringWriter = new(CultureInfo.InvariantCulture); XmlWriter xmlWriter = Helpers.CreateXmlWriter(xomlStringWriter); markupSerializationManager.WorkflowMarkupStack.Push(xmlWriter); markupSerializationManager.WorkflowMarkupStack.Push(xomlStringWriter); @@ -154,7 +154,7 @@ internal object DeserializeObject(WorkflowMarkupSerializationManager serializati // Lets ignore the Definition tags if nobody is interested // string decodedName = XmlConvert.DecodeName(reader.LocalName); - XmlQualifiedName xmlQualifiedName = new XmlQualifiedName(decodedName, reader.LookupNamespace(reader.Prefix)); + XmlQualifiedName xmlQualifiedName = new(decodedName, reader.LookupNamespace(reader.Prefix)); if (xmlQualifiedName.Namespace.Equals(StandardXomlKeys.Definitions_XmlNs, StringComparison.Ordinal) && !IsMarkupExtension(xmlQualifiedName) && !ExtendedPropertyInfo.IsExtendedProperty(serializationManager, xmlQualifiedName)) @@ -206,7 +206,7 @@ private void DeserializeContents(WorkflowMarkupSerializationManager serializatio return; // get the serializer - if (!(serializationManager.GetSerializer(obj.GetType(), typeof(WorkflowMarkupSerializer)) is WorkflowMarkupSerializer serializer)) + if (serializationManager.GetSerializer(obj.GetType(), typeof(WorkflowMarkupSerializer)) is not WorkflowMarkupSerializer serializer) { serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerNotAvailable, obj.GetType().FullName), reader)); return; @@ -225,8 +225,8 @@ private void DeserializeContents(WorkflowMarkupSerializationManager serializatio bool isEmptyElement = reader.IsEmptyElement; //string elementNamespace = reader.NamespaceURI; - List props = new List(); - List events = new List(); + List props = []; + List events = []; // Add the extended properties for primitive types if (obj.GetType().IsPrimitive || obj.GetType() == typeof(string) || obj.GetType() == typeof(decimal) || obj.GetType().IsEnum || obj.GetType() == typeof(DateTime) || obj.GetType() == typeof(TimeSpan) || @@ -258,7 +258,7 @@ private void DeserializeContents(WorkflowMarkupSerializationManager serializatio continue; // - XmlQualifiedName xmlQualifiedName = new XmlQualifiedName(reader.LocalName, reader.LookupNamespace(reader.Prefix)); + XmlQualifiedName xmlQualifiedName = new(reader.LocalName, reader.LookupNamespace(reader.Prefix)); if (xmlQualifiedName.Namespace.Equals(StandardXomlKeys.Definitions_XmlNs, StringComparison.Ordinal) && !IsMarkupExtension(xmlQualifiedName) && !ExtendedPropertyInfo.IsExtendedProperty(serializationManager, props, xmlQualifiedName) && @@ -287,7 +287,7 @@ private void DeserializeContents(WorkflowMarkupSerializationManager serializatio } else { - serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerNoMemberFound, new object[] { propName, obj.GetType().FullName }), reader)); + serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerNoMemberFound, [propName, obj.GetType().FullName]), reader)); } } } @@ -313,14 +313,14 @@ private void DeserializeContents(WorkflowMarkupSerializationManager serializatio return; } - using (ContentProperty contentProperty = new ContentProperty(serializationManager, serializer, obj)) + using (ContentProperty contentProperty = new(serializationManager, serializer, obj)) { - List contents = new List(); + List contents = []; if (!isEmptyElement) { reader.MoveToElement(); int initialDepth = reader.Depth; - XmlQualifiedName extendedPropertyQualifiedName = new XmlQualifiedName(reader.LocalName, reader.LookupNamespace(reader.Prefix)); + XmlQualifiedName extendedPropertyQualifiedName = new(reader.LocalName, reader.LookupNamespace(reader.Prefix)); do { // Extended property should be deserialized, this is required for primitive types which have extended property as children @@ -350,7 +350,7 @@ private void DeserializeContents(WorkflowMarkupSerializationManager serializatio if (reader.NodeType == XmlNodeType.Element) { //We should only support A.B syntax for compound properties, all others are treated as content - XmlQualifiedName xmlQualifiedName = new XmlQualifiedName(reader.LocalName, reader.LookupNamespace(reader.Prefix)); + XmlQualifiedName xmlQualifiedName = new(reader.LocalName, reader.LookupNamespace(reader.Prefix)); int index = reader.LocalName.IndexOf('.'); if (index > 0 || ExtendedPropertyInfo.IsExtendedProperty(serializationManager, xmlQualifiedName)) { @@ -507,11 +507,11 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization return; } //Need the SortedDictionary to keep serialization repeatable. - SortedDictionary allProperties = new SortedDictionary(); - ArrayList complexProperties = new ArrayList(); + SortedDictionary allProperties = []; + ArrayList complexProperties = []; - List properties = new List(); - List events = new List(); + List properties = []; + List events = []; // Serialize the extended properties for primitive types also if (obj.GetType().IsPrimitive || obj.GetType() == typeof(string) || obj.GetType() == typeof(decimal) || @@ -547,7 +547,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization else if (obj.GetType() == typeof(string)) { string attribValue = obj as string; - attribValue = attribValue.Replace('\0', ' '); + attribValue = attribValue?.Replace('\0', ' ') ?? ""; if (!(attribValue.StartsWith("{", StringComparison.Ordinal) && attribValue.EndsWith("}", StringComparison.Ordinal))) writer.WriteValue(attribValue); else @@ -606,7 +606,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization } } - using (ContentProperty contentProperty = new ContentProperty(serializationManager, serializer, obj)) + using (ContentProperty contentProperty = new(serializationManager, serializer, obj)) { foreach (object propertyObj in allProperties.Values) { @@ -616,10 +616,8 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization try { - if (propertyObj is PropertyInfo) + if (propertyObj is PropertyInfo property) { - PropertyInfo property = propertyObj as PropertyInfo; - // If the property has parameters we can not serialize it , we just move on. ParameterInfo[] indexParameters = property.GetIndexParameters(); if (indexParameters != null && indexParameters.Length > 0) @@ -639,7 +637,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization while (e is TargetInvocationException && e.InnerException != null) e = e.InnerException; - serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerPropertyGetFailed, new object[] { propertyName, obj.GetType().FullName, e.Message }))); + serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerPropertyGetFailed, [propertyName, obj.GetType().FullName, e.Message]))); continue; } @@ -660,7 +658,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization if (attributes.Length > 0) { DefaultValueAttribute defaultValueAttr = attributes[0] as DefaultValueAttribute; - if (defaultValueAttr.Value == null) + if (defaultValueAttr?.Value == null) propertyValue = null; } } @@ -682,7 +680,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization } if (propValueSerializer == null) { - serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNotAvailableForSerialize, propertyValueType.FullName))); + serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNotAvailableForSerialize, propertyValueType?.FullName ?? ""))); serializationManager.Context.Pop(); continue; } @@ -726,7 +724,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization } catch (Exception e) { - serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNoSerializeLogic, new object[] { propertyName, obj.GetType().FullName }), e)); + serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNoSerializeLogic, [propertyName, obj.GetType().FullName]), e)); } finally { @@ -756,9 +754,8 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization try { - if (propertyObj is PropertyInfo) + if (propertyObj is PropertyInfo property) { - PropertyInfo property = propertyObj as PropertyInfo; propertyName = property.Name; propertyValue = property.CanRead ? property.GetValue(obj, null) : null; ownerType = obj.GetType(); @@ -770,7 +767,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization while (e is TargetInvocationException && e.InnerException != null) e = e.InnerException; - serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerPropertyGetFailed, propertyName, ownerType.FullName, e.Message))); + serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerPropertyGetFailed, propertyName, ownerType?.FullName ?? "", e.Message))); continue; } @@ -806,7 +803,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization { contents = GetMarkupExtensionFromValue(contents); - if (!(serializationManager.GetSerializer(contents.GetType(), typeof(WorkflowMarkupSerializer)) is WorkflowMarkupSerializer propValueSerializer)) + if (serializationManager.GetSerializer(contents.GetType(), typeof(WorkflowMarkupSerializer)) is not WorkflowMarkupSerializer propValueSerializer) { serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNotAvailableForSerialize, contents.GetType()))); } @@ -836,7 +833,7 @@ internal void SerializeContents(WorkflowMarkupSerializationManager serialization { if (contentProperty.Property == null) { - IEnumerable enumerableContents = contents as IEnumerable; + IEnumerable enumerableContents = contents as IEnumerable ?? Array.Empty(); foreach (object childObj in enumerableContents) { if (childObj == null) @@ -942,7 +939,7 @@ protected internal virtual bool ShouldSerializeValue(WorkflowMarkupSerialization if (attributes.Length > 0) { DefaultValueAttribute defaultValueAttr = attributes[0] as DefaultValueAttribute; - if (defaultValueAttr.Value is IConvertible && value is IConvertible && object.Equals(Convert.ChangeType(defaultValueAttr.Value, property.PropertyType, CultureInfo.InvariantCulture), Convert.ChangeType(value, property.PropertyType, CultureInfo.InvariantCulture))) + if (defaultValueAttr?.Value is IConvertible && value is IConvertible && object.Equals(Convert.ChangeType(defaultValueAttr.Value, property.PropertyType, CultureInfo.InvariantCulture), Convert.ChangeType(value, property.PropertyType, CultureInfo.InvariantCulture))) return false; } } @@ -1001,8 +998,8 @@ private object InternalDeserializeFromString(WorkflowMarkupSerializationManager if (value == null) throw new ArgumentNullException("value"); - object propVal = null; - if (!(serializationManager.WorkflowMarkupStack[typeof(XmlReader)] is XmlReader reader)) + object propVal; + if (serializationManager.WorkflowMarkupStack[typeof(XmlReader)] is not XmlReader reader) { Debug.Assert(false, "XmlReader not available."); return null; @@ -1053,7 +1050,7 @@ private object InternalDeserializeFromString(WorkflowMarkupSerializationManager if (propVal != null) { Type type = propVal as Type; - if (type.IsPrimitive || type.IsEnum || type == typeof(System.String)) + if (type?.IsPrimitive == true || type?.IsEnum == true || type == typeof(System.String)) return type; } return value; @@ -1068,7 +1065,7 @@ private object InternalDeserializeFromString(WorkflowMarkupSerializationManager } else { - throw CreateSerializationError(SR.GetString(SR.Error_SerializerPrimitivePropertyNoLogic, new object[] { "", value.Trim(), "" }), reader); + throw CreateSerializationError(SR.GetString(SR.Error_SerializerPrimitivePropertyNoLogic, ["", value.Trim(), ""]), reader); } } return propVal; @@ -1101,7 +1098,7 @@ protected internal virtual void AddChild(WorkflowMarkupSerializationManager seri if (childObj == null) throw new ArgumentNullException("childObj"); - throw new Exception(SR.GetString(SR.Error_SerializerNoChildNotion, new object[] { parentObject.GetType().FullName })); + throw new Exception(SR.GetString(SR.Error_SerializerNoChildNotion, [parentObject.GetType().FullName])); } protected virtual object CreateInstance(WorkflowMarkupSerializationManager serializationManager, Type type) @@ -1121,7 +1118,7 @@ protected internal virtual PropertyInfo[] GetProperties(WorkflowMarkupSerializat if (serializationManager == null) throw new ArgumentNullException("serializationManager"); - List properties = new List(); + List properties = []; object[] attributes = obj.GetType().GetCustomAttributes(typeof(RuntimeNamePropertyAttribute), true); string name = null; @@ -1137,7 +1134,7 @@ protected internal virtual PropertyInfo[] GetProperties(WorkflowMarkupSerializat if (visibility != DesignerSerializationVisibility.Content && (!property.CanWrite || property.GetSetMethod() == null)) { // work around for CodeObject which are ICollection needs to be serialized. - if (!(obj is CodeObject) || !typeof(ICollection).IsAssignableFrom(property.PropertyType)) + if (obj is not CodeObject || !typeof(ICollection).IsAssignableFrom(property.PropertyType)) continue; } @@ -1147,7 +1144,7 @@ protected internal virtual PropertyInfo[] GetProperties(WorkflowMarkupSerializat properties.Add(new ExtendedPropertyInfo(property, OnGetRuntimeNameValue, OnSetRuntimeNameValue, OnGetRuntimeQualifiedName)); } - return properties.ToArray(); + return [.. properties]; } protected internal virtual EventInfo[] GetEvents(WorkflowMarkupSerializationManager serializationManager, object obj) @@ -1157,7 +1154,7 @@ protected internal virtual EventInfo[] GetEvents(WorkflowMarkupSerializationMana if (serializationManager == null) throw new ArgumentNullException("serializationManager"); - List events = new List(); + List events = []; foreach (EventInfo evt in obj.GetType().GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy)) { if (Helpers.GetSerializationVisibility(evt) == DesignerSerializationVisibility.Hidden) @@ -1166,12 +1163,12 @@ protected internal virtual EventInfo[] GetEvents(WorkflowMarkupSerializationMana events.Add(evt); } - return events.ToArray(); + return [.. events]; } internal virtual ExtendedPropertyInfo[] GetExtendedProperties(WorkflowMarkupSerializationManager manager, object extendee) { - return new ExtendedPropertyInfo[0]; + return []; } private object OnGetRuntimeNameValue(ExtendedPropertyInfo extendedProperty, object extendee) { @@ -1219,7 +1216,7 @@ internal static WorkflowMarkupSerializationException CreateSerializationError(st { string errorMsg = message; if (string.IsNullOrEmpty(errorMsg)) - errorMsg = e.Message; + errorMsg = e?.Message ?? string.Empty; if (reader is IXmlLineInfo xmlLineInfo) return new WorkflowMarkupSerializationException(errorMsg, xmlLineInfo.LineNumber, xmlLineInfo.LinePosition); @@ -1261,7 +1258,7 @@ private object CreateInstance(WorkflowMarkupSerializationManager serializationMa } catch (Exception e) { - serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerTypeNotResolvedWithInnerError, new object[] { GetClrFullName(serializationManager, xmlQualifiedName), e.Message }), e, reader)); + serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerTypeNotResolvedWithInnerError, [GetClrFullName(serializationManager, xmlQualifiedName), e.Message]), e, reader)); return null; } if (type == null && !xmlQualifiedName.Name.EndsWith("Extension", StringComparison.Ordinal)) @@ -1273,14 +1270,14 @@ private object CreateInstance(WorkflowMarkupSerializationManager serializationMa } catch (Exception e) { - serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerTypeNotResolvedWithInnerError, new object[] { GetClrFullName(serializationManager, xmlQualifiedName), e.Message }), e, reader)); + serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerTypeNotResolvedWithInnerError, [GetClrFullName(serializationManager, xmlQualifiedName), e.Message]), e, reader)); return null; } } if (type == null) { - serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerTypeNotResolved, new object[] { GetClrFullName(serializationManager, xmlQualifiedName) }), reader)); + serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerTypeNotResolved, [GetClrFullName(serializationManager, xmlQualifiedName)]), reader)); return null; } @@ -1317,7 +1314,7 @@ private object CreateInstance(WorkflowMarkupSerializationManager serializationMa else { // get the serializer - if (!(serializationManager.GetSerializer(type, typeof(WorkflowMarkupSerializer)) is WorkflowMarkupSerializer serializer)) + if (serializationManager.GetSerializer(type, typeof(WorkflowMarkupSerializer)) is not WorkflowMarkupSerializer serializer) { serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerNotAvailable, type.FullName), reader)); return null; @@ -1355,8 +1352,7 @@ private void DeserializeCompoundProperty(WorkflowMarkupSerializationManager seri //Deserialize compound properties if (isReadOnly) { - object propValue = null; - propValue = property.CanRead ? property.GetValue(obj, null) : null; + object propValue = property.CanRead ? property.GetValue(obj, null) : null; if (propValue != null) DeserializeContents(serializationManager, propValue, reader); @@ -1408,7 +1404,7 @@ private void DeserializeCompoundProperty(WorkflowMarkupSerializationManager seri } catch { - serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerComplexPropertySetFailed, new object[] { propertyName, propertyName, obj.GetType().Name }))); + serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerComplexPropertySetFailed, [propertyName, propertyName, obj.GetType().Name]))); } } } @@ -1434,7 +1430,7 @@ private void DeserializeSimpleProperty(WorkflowMarkupSerializationManager serial if (isReadOnly && !typeof(ICollection).IsAssignableFrom(propertyType)) { - serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerPrimitivePropertyReadOnly, new object[] { property.Name, property.Name, obj.GetType().FullName }), reader)); + serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerPrimitivePropertyReadOnly, [property.Name, property.Name, obj.GetType().FullName]), reader)); return; } @@ -1444,7 +1440,7 @@ private void DeserializeSimpleProperty(WorkflowMarkupSerializationManager serial private void DeserializeSimpleMember(WorkflowMarkupSerializationManager serializationManager, Type memberType, XmlReader reader, object obj, string value) { //Get the serializer for the member type - if (!(serializationManager.GetSerializer(memberType, typeof(WorkflowMarkupSerializer)) is WorkflowMarkupSerializer memberSerializer)) + if (serializationManager.GetSerializer(memberType, typeof(WorkflowMarkupSerializer)) is not WorkflowMarkupSerializer memberSerializer) { serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerNotAvailable, memberType.FullName), reader)); return; @@ -1477,7 +1473,7 @@ private void DeserializeSimpleMember(WorkflowMarkupSerializationManager serializ { while (e is TargetInvocationException && e.InnerException != null) e = e.InnerException; - serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerMemberSetFailed, new object[] { reader.LocalName, reader.Value, reader.LocalName, obj.GetType().FullName, e.Message }), e, reader)); + serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerMemberSetFailed, [reader.LocalName, reader.Value, reader.LocalName, obj.GetType().FullName, e.Message]), e, reader)); } } } @@ -1485,7 +1481,7 @@ private void DeserializeSimpleMember(WorkflowMarkupSerializationManager serializ { while (e is TargetInvocationException && e.InnerException != null) e = e.InnerException; - serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerMemberSetFailed, new object[] { reader.LocalName, reader.Value, reader.LocalName, obj.GetType().FullName, e.Message }), e, reader)); + serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerMemberSetFailed, [reader.LocalName, reader.Value, reader.LocalName, obj.GetType().FullName, e.Message]), e, reader)); } } #endregion @@ -1639,6 +1635,9 @@ internal object DeserializeFromCompactFormat(WorkflowMarkupSerializationManager typename = typename.Substring(typeIndex + 1); } + if (reader == null) + throw new ArgumentNullException(nameof(reader)); + Type type = serializationManager.GetType(new XmlQualifiedName(typename, reader.LookupNamespace(prefix))); if (type == null && !typename.EndsWith("Extension", StringComparison.Ordinal)) { @@ -1653,7 +1652,7 @@ internal object DeserializeFromCompactFormat(WorkflowMarkupSerializationManager // Break apart the argument string. object obj = null; - Dictionary namedArgs = new Dictionary(); + Dictionary namedArgs = []; ArrayList argTokens; try { @@ -1667,7 +1666,7 @@ internal object DeserializeFromCompactFormat(WorkflowMarkupSerializationManager if (argTokens != null) { // Process the positional arugments and find the correct constructor to call. - ArrayList positionalArgs = new ArrayList(); + ArrayList positionalArgs = []; bool firstEqual = true; for (int i = 0; i < argTokens.Count; i++) { @@ -1731,12 +1730,12 @@ internal object DeserializeFromCompactFormat(WorkflowMarkupSerializationManager if (namedArgs.Count > 0) { - if (!(serializationManager.GetSerializer(obj.GetType(), typeof(WorkflowMarkupSerializer)) is WorkflowMarkupSerializer serializer)) + if (serializationManager.GetSerializer(obj.GetType(), typeof(WorkflowMarkupSerializer)) is not WorkflowMarkupSerializer serializer) { serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerNotAvailable, obj.GetType().FullName), reader)); return obj; } - List properties = new List(); + List properties = []; try { properties.AddRange(serializer.GetProperties(serializationManager, obj)); @@ -1771,7 +1770,7 @@ internal object DeserializeFromCompactFormat(WorkflowMarkupSerializationManager } else { - serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerPrimitivePropertyNoLogic, new object[] { argName, argName, obj.GetType().FullName }), reader)); + serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerPrimitivePropertyNoLogic, [argName, argName, obj.GetType().FullName]), reader)); } } } @@ -2032,7 +2031,7 @@ public ContentProperty(WorkflowMarkupSerializationManager serializationManager, !IsMarkupExtension(this.contentProperty.PropertyType) && this.contentProperty.CanWrite) { - if (!(serializationManager.GetSerializer(this.contentProperty.PropertyType, typeof(WorkflowMarkupSerializer)) is WorkflowMarkupSerializer serializer)) + if (serializationManager.GetSerializer(this.contentProperty.PropertyType, typeof(WorkflowMarkupSerializer)) is not WorkflowMarkupSerializer serializer) { serializationManager.ReportError(CreateSerializationError(SR.GetString(SR.Error_SerializerNotAvailable, this.contentProperty.PropertyType.FullName), reader)); return; @@ -2226,18 +2225,11 @@ private PropertyInfo GetContentProperty(WorkflowMarkupSerializationManager seria } } - private struct ContentInfo + private struct ContentInfo(object content, int lineNumber, int linePosition) { - public int LineNumber; - public int LinePosition; - public object Content; - - public ContentInfo(object content, int lineNumber, int linePosition) - { - this.Content = content; - this.LineNumber = lineNumber; - this.LinePosition = linePosition; - } + public int LineNumber = lineNumber; + public int LinePosition = linePosition; + public object Content = content; } #endregion diff --git a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/SynchronizationHandlesTypeConverter.cs b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/SynchronizationHandlesTypeConverter.cs index 671ad10..b33551a 100644 --- a/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/SynchronizationHandlesTypeConverter.cs +++ b/LogicBuilder.Workflow.ComponentModel.Serialization/ComponentModel/SynchronizationHandlesTypeConverter.cs @@ -58,9 +58,9 @@ internal static string Stringify(ICollection synchronizationHandles) internal static ICollection UnStringify(string stringifiedValue) { - ICollection synchronizationHandles = new List(); - stringifiedValue = stringifiedValue.Replace("\\,", ">"); - foreach (string handle in stringifiedValue.Split(new char[] { ',', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) + ICollection synchronizationHandles = []; + stringifiedValue = stringifiedValue?.Replace("\\,", ">") ?? ""; + foreach (string handle in stringifiedValue.Split([',', '\r', '\n'], StringSplitOptions.RemoveEmptyEntries)) { string realHandle = handle.Trim().Replace('>', ','); if (realHandle != string.Empty && !synchronizationHandles.Contains(realHandle)) diff --git a/Workflow.ComponentModel.Serialization.Tests/ComponentModel/Serialization/CodeTypeReferenceSerializerTest.cs b/Workflow.ComponentModel.Serialization.Tests/ComponentModel/Serialization/CodeTypeReferenceSerializerTest.cs index ba17797..6023fe8 100644 --- a/Workflow.ComponentModel.Serialization.Tests/ComponentModel/Serialization/CodeTypeReferenceSerializerTest.cs +++ b/Workflow.ComponentModel.Serialization.Tests/ComponentModel/Serialization/CodeTypeReferenceSerializerTest.cs @@ -243,7 +243,7 @@ public void DeserializeFromString_ReturnsCodeTypeReference_ForSimpleType() // Assert Assert.NotNull(result); - Assert.Contains("System.String", result.BaseType); + Assert.Contains("System.String", result?.BaseType); } [Fact] @@ -258,8 +258,8 @@ public void DeserializeFromString_SetsQualifiedNameInUserData() // Assert Assert.NotNull(result); - Assert.True(result.UserData.Contains(CodeTypeReferenceSerializer.QualifiedName)); - Assert.Equal(typeof(string).AssemblyQualifiedName, result.UserData[CodeTypeReferenceSerializer.QualifiedName]); + Assert.True(result?.UserData.Contains(CodeTypeReferenceSerializer.QualifiedName)); + Assert.Equal(typeof(string).AssemblyQualifiedName, result?.UserData[CodeTypeReferenceSerializer.QualifiedName]); } [Fact] @@ -274,7 +274,7 @@ public void DeserializeFromString_HandlesUnknownType_Gracefully() // Assert Assert.NotNull(result); - Assert.Equal("UnknownType.DoesNotExist", result.UserData[CodeTypeReferenceSerializer.QualifiedName]); + Assert.Equal("UnknownType.DoesNotExist", result?.UserData[CodeTypeReferenceSerializer.QualifiedName]); } [Fact] @@ -289,7 +289,7 @@ public void DeserializeFromString_HandlesArrayType() // Assert Assert.NotNull(result); - Assert.Contains("System.String[]", result.UserData[CodeTypeReferenceSerializer.QualifiedName]!.ToString()); + Assert.Contains("System.String[]", result?.UserData[CodeTypeReferenceSerializer.QualifiedName]!.ToString()); } [Fact] @@ -304,7 +304,7 @@ public void DeserializeFromString_HandlesGenericType() // Assert Assert.NotNull(result); - Assert.True(result.UserData.Contains(CodeTypeReferenceSerializer.QualifiedName)); + Assert.True(result?.UserData.Contains(CodeTypeReferenceSerializer.QualifiedName)); } #endregion @@ -324,7 +324,7 @@ public void RoundTrip_SerializeAndDeserialize_SimpleType() // Assert Assert.NotNull(deserialized); - Assert.Equal("System.Int32", deserialized.BaseType); + Assert.Equal("System.Int32", deserialized?.BaseType); } [Fact] @@ -340,7 +340,7 @@ public void RoundTrip_SerializeAndDeserialize_GenericType() // Assert Assert.NotNull(deserialized); - Assert.Contains("List", deserialized.BaseType); + Assert.Contains("List", deserialized?.BaseType); } [Fact] @@ -356,7 +356,7 @@ public void RoundTrip_SerializeAndDeserialize_ArrayType() // Assert Assert.NotNull(deserialized); - Assert.Contains("Double[]", deserialized.UserData[CodeTypeReferenceSerializer.QualifiedName]!.ToString()); + Assert.Contains("Double[]", deserialized?.UserData[CodeTypeReferenceSerializer.QualifiedName]!.ToString()); } #endregion diff --git a/Workflow.ComponentModel.Serialization.Tests/ComponentModel/Serialization/TypeExtensionSerializerTest.cs b/Workflow.ComponentModel.Serialization.Tests/ComponentModel/Serialization/TypeExtensionSerializerTest.cs index e39b7db..eef06c0 100644 --- a/Workflow.ComponentModel.Serialization.Tests/ComponentModel/Serialization/TypeExtensionSerializerTest.cs +++ b/Workflow.ComponentModel.Serialization.Tests/ComponentModel/Serialization/TypeExtensionSerializerTest.cs @@ -54,7 +54,7 @@ public void GetInstanceDescriptor_ReturnsInstanceDescriptor_WithTypeConstructor( Assert.IsType(result); var ctor = result.MemberInfo as ConstructorInfo; Assert.NotNull(ctor); - Assert.Equal([typeof(Type)], Array.ConvertAll(ctor.GetParameters(), p => p.ParameterType)); + Assert.Equal([typeof(Type)], Array.ConvertAll(ctor?.GetParameters() ?? [], p => p.ParameterType)); Assert.Single(result.Arguments); Assert.Equal(type, result.Arguments.OfType().First()); } @@ -77,7 +77,7 @@ public void GetInstanceDescriptor_ReturnsInstanceDescriptor_WithStringConstructo Assert.IsType(result); var ctor = result.MemberInfo as ConstructorInfo; Assert.NotNull(ctor); - Assert.Equal([typeof(string)], Array.ConvertAll(ctor.GetParameters(), p => p.ParameterType)); + Assert.Equal([typeof(string)], Array.ConvertAll(ctor?.GetParameters() ?? [], p => p.ParameterType)); Assert.Single(result.Arguments); Assert.Equal(typeName, result.Arguments.OfType().First()); } diff --git a/Workflow.ComponentModel.Serialization.Tests/ComponentModel/SynchronizationHandlesTypeConverterTest.cs b/Workflow.ComponentModel.Serialization.Tests/ComponentModel/SynchronizationHandlesTypeConverterTest.cs index 4be47c5..d1b88f0 100644 --- a/Workflow.ComponentModel.Serialization.Tests/ComponentModel/SynchronizationHandlesTypeConverterTest.cs +++ b/Workflow.ComponentModel.Serialization.Tests/ComponentModel/SynchronizationHandlesTypeConverterTest.cs @@ -168,7 +168,7 @@ public void ConvertFrom_ReturnsCollectionOfHandles_WhenValueIsCommaSeparatedStri string input = "handle1, handle2, handle3"; // Act - var result = _converter.ConvertFrom(null, CultureInfo.InvariantCulture, input) as ICollection; + var result = _converter.ConvertFrom(null, CultureInfo.InvariantCulture, input) as ICollection ?? []; // Assert Assert.NotNull(result);