From 1158ab2082d91fd1aec5be1fff83bb816d880bea Mon Sep 17 00:00:00 2001 From: Blaise Taylor Date: Fri, 30 Jan 2026 15:00:29 -0500 Subject: [PATCH] AB#4 Fixing recommendations from Code Quality. --- .../DesignerSerializationManager.cs | 144 +++++++----------- .../SR.cs | 18 +-- 2 files changed, 66 insertions(+), 96 deletions(-) diff --git a/LogicBuilder.ComponentModel.Design.Serialization/DesignerSerializationManager.cs b/LogicBuilder.ComponentModel.Design.Serialization/DesignerSerializationManager.cs index 9e1e8c9..f48fd2b 100644 --- a/LogicBuilder.ComponentModel.Design.Serialization/DesignerSerializationManager.cs +++ b/LogicBuilder.ComponentModel.Design.Serialization/DesignerSerializationManager.cs @@ -226,13 +226,11 @@ public IContainer Container { get { - if (this.container == null) + if (this.container == null && this.GetService(typeof(IDesignerHost)) is IDesignerHost designerHost) { - if (this.GetService(typeof(IDesignerHost)) is IDesignerHost designerHost) - { - this.container = designerHost.Container; - } + this.container = designerHost.Container; } + return this.container; } set @@ -250,10 +248,7 @@ public IList Errors get { this.CheckSession(); - if (this.errorList == null) - { - this.errorList = new ArrayList(); - } + this.errorList ??= []; return this.errorList; } } @@ -352,7 +347,7 @@ PropertyDescriptorCollection IDesignerSerializationManager.Properties PropertyDescriptor[] array; if (obj == null) { - array = new PropertyDescriptor[0]; + array = []; } else { @@ -375,7 +370,7 @@ internal ArrayList SerializationProviders { if (this.designerSerializationProviders == null) { - return new ArrayList(); + return []; } return this.designerSerializationProviders.Clone() as ArrayList; } @@ -447,24 +442,20 @@ protected virtual object CreateInstance(Type type, ICollection arguments, string obj = null; } } - if ((obj == null & addToContainer) && typeof(IComponent).IsAssignableFrom(type) && (array == null || array.Length == 0 || (array.Length == 1 && array[0] == this.Container))) + if ((obj == null & addToContainer) + && typeof(IComponent).IsAssignableFrom(type) + && (array == null || array.Length == 0 || (array.Length == 1 && array[0] == this.Container)) + && this.GetService(typeof(IDesignerHost)) is IDesignerHost designerHost && designerHost.Container == this.Container) { - if (this.GetService(typeof(IDesignerHost)) is IDesignerHost designerHost && designerHost.Container == this.Container) + bool flag = false; + if (!this.PreserveNames && name != null && this.Container.Components[name] != null) { - bool flag = false; - if (!this.PreserveNames && name != null && this.Container.Components[name] != null) - { - flag = true; - } - if (name == null | flag) - { - obj = designerHost.CreateComponent(type); - } - else - { - obj = designerHost.CreateComponent(type, name); - } + flag = true; } + + obj = (name == null | flag) + ? designerHost.CreateComponent(type) + : designerHost.CreateComponent(type, name); } if (obj == null) { @@ -509,6 +500,8 @@ protected virtual object CreateInstance(Type type, ICollection arguments, string } catch (InvalidCastException) { + // Type conversion failed - this constructor parameter doesn't match. + // Fall through to set flag2 = false and break. } } flag2 = false; @@ -532,7 +525,7 @@ protected virtual object CreateInstance(Type type, ICollection arguments, string } catch (MissingMethodException) { - StringBuilder stringBuilder = new StringBuilder(); + StringBuilder stringBuilder = new(); object[] array4 = array; for (int l = 0; l < array4.Length; l++) { @@ -550,11 +543,11 @@ protected virtual object CreateInstance(Type type, ICollection arguments, string stringBuilder.Append("null"); } } - throw new SerializationException(SR.GetString("SerializationManagerNoMatchingCtor", new object[] - { + throw new SerializationException(SR.GetString("SerializationManagerNoMatchingCtor", + [ type.FullName, stringBuilder.ToString() - })) + ])) { HelpLink = "SerializationManagerNoMatchingCtor" }; @@ -641,10 +634,7 @@ public object GetSerializer(Type objectType, Type serializerType) } if (obj != null && this.session != null) { - if (this.serializers == null) - { - this.serializers = new Hashtable(); - } + this.serializers ??= []; this.serializers[objectType] = obj; } } @@ -662,10 +652,7 @@ public object GetSerializer(Type objectType, Type serializerType) ((IDesignerSerializationManager)this).AddSerializationProvider(designerSerializationProvider); } } - if (this.defaultProviderTable == null) - { - this.defaultProviderTable = new Hashtable(); - } + this.defaultProviderTable ??= []; this.defaultProviderTable[serializerType] = type; } if (this.designerSerializationProviders != null) @@ -712,15 +699,12 @@ protected virtual object GetService(Type serviceType) protected virtual Type GetType(string typeName) { Type type = this.GetRuntimeType(typeName); - if (type != null) + if (type != null && this.GetService(typeof(TypeDescriptionProviderService)) is TypeDescriptionProviderService typeDescriptionProviderService) { - if (this.GetService(typeof(TypeDescriptionProviderService)) is TypeDescriptionProviderService typeDescriptionProviderService) + TypeDescriptionProvider typeDescriptionProvider = typeDescriptionProviderService.GetProvider(type); + if (!typeDescriptionProvider.IsSupportedType(type)) { - TypeDescriptionProvider typeDescriptionProvider = typeDescriptionProviderService.GetProvider(type); - if (!typeDescriptionProvider.IsSupportedType(type)) - { - type = null; - } + type = null; } } return type; @@ -736,16 +720,10 @@ public Type GetRuntimeType(string typeName) this.typeResolver = (this.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService); this.searchedTypeResolver = true; } - Type type; - if (this.typeResolver == null) - { - type = Type.GetType(typeName); - } - else - { - type = this.typeResolver.GetType(typeName); - } - return type; + + return this.typeResolver == null + ? Type.GetType(typeName) + : this.typeResolver.GetType(typeName); } /// Raises the event. @@ -800,16 +778,13 @@ private PropertyDescriptor WrapProperty(PropertyDescriptor property, object owne } /// Adds a custom serialization provider to the serialization manager. - /// The serialization provider to add. - void IDesignerSerializationManager.AddSerializationProvider(IDesignerSerializationProvider provider) + /// The serialization provider to add. + void IDesignerSerializationManager.AddSerializationProvider(IDesignerSerializationProvider serializationProvider) { - if (this.designerSerializationProviders == null) - { - this.designerSerializationProviders = new ArrayList(); - } - if (!this.designerSerializationProviders.Contains(provider)) + this.designerSerializationProviders ??= []; + if (!this.designerSerializationProviders.Contains(serializationProvider)) { - this.designerSerializationProviders.Add(provider); + this.designerSerializationProviders.Add(serializationProvider); } } @@ -824,20 +799,20 @@ object IDesignerSerializationManager.CreateInstance(Type type, ICollection argum this.CheckSession(); if (name != null && this.instancesByName != null && this.instancesByName.ContainsKey(name)) { - throw new SerializationException(SR.GetString("SerializationManagerDuplicateComponentDecl", new object[] - { + throw new SerializationException(SR.GetString("SerializationManagerDuplicateComponentDecl", + [ name - })) + ])) { HelpLink = "SerializationManagerDuplicateComponentDecl" }; } object obj = this.CreateInstance(type, arguments, name, addToContainer); - if (name != null && (!(obj is IComponent) || !this.RecycleInstances)) + if (name != null && (obj is not IComponent || !this.RecycleInstances)) { if (this.instancesByName == null) { - this.instancesByName = new Hashtable(); + this.instancesByName = []; this.namesByInstance = new Hashtable(new DesignerSerializationManager.ReferenceComparer()); } this.instancesByName[name] = obj; @@ -870,7 +845,7 @@ object IDesignerSerializationManager.GetInstance(string name) } if (obj == null) { - ResolveNameEventArgs resolveNameEventArgs = new ResolveNameEventArgs(name); + ResolveNameEventArgs resolveNameEventArgs = new(name); this.OnResolveName(resolveNameEventArgs); obj = resolveNameEventArgs.Value; } @@ -900,14 +875,9 @@ string IDesignerSerializationManager.GetName(object value) ISite site = component.Site; if (site != null) { - if (site is INestedSite nestedSite) - { - text = nestedSite.FullName; - } - else - { - text = site.Name; - } + text = site is INestedSite nestedSite + ? nestedSite.FullName + : site.Name; } } return text; @@ -947,10 +917,10 @@ Type IDesignerSerializationManager.GetType(string typeName) } /// Removes a previously added serialization provider. - /// The to remove. - void IDesignerSerializationManager.RemoveSerializationProvider(IDesignerSerializationProvider provider) + /// The to remove. + void IDesignerSerializationManager.RemoveSerializationProvider(IDesignerSerializationProvider serializationProvider) { - this.designerSerializationProviders?.Remove(provider); + this.designerSerializationProviders?.Remove(serializationProvider); } /// Used to report a recoverable error in serialization. @@ -984,23 +954,23 @@ void IDesignerSerializationManager.SetName(object instance, string name) } if (this.instancesByName == null) { - this.instancesByName = new Hashtable(); + this.instancesByName = []; this.namesByInstance = new Hashtable(new DesignerSerializationManager.ReferenceComparer()); } if (this.instancesByName[name] != null) { - throw new ArgumentException(SR.GetString("SerializationManagerNameInUse", new object[] - { + throw new ArgumentException(SR.GetString("SerializationManagerNameInUse", + [ name - })); + ])); } if (this.namesByInstance[instance] != null) { - throw new ArgumentException(SR.GetString("SerializationManagerObjectHasName", new object[] - { + throw new ArgumentException(SR.GetString("SerializationManagerObjectHasName", + [ name, (string)this.namesByInstance[instance] - })); + ])); } this.instancesByName[name] = instance; this.namesByInstance[instance] = name; diff --git a/LogicBuilder.ComponentModel.Design.Serialization/SR.cs b/LogicBuilder.ComponentModel.Design.Serialization/SR.cs index 797cdab..a43732a 100644 --- a/LogicBuilder.ComponentModel.Design.Serialization/SR.cs +++ b/LogicBuilder.ComponentModel.Design.Serialization/SR.cs @@ -1827,32 +1827,32 @@ private static LogicBuilder.ComponentModel.Design.Serialization.SR GetLoader() public static object GetObject(string name) { - LogicBuilder.ComponentModel.Design.Serialization.SR loader = GetLoader(); - if (loader == null) + LogicBuilder.ComponentModel.Design.Serialization.SR loaderLocal = GetLoader(); + if (loaderLocal == null) { return null; } - return loader.resources.GetObject(name, Culture); + return loaderLocal.resources.GetObject(name, Culture); } public static string GetString(string name) { - LogicBuilder.ComponentModel.Design.Serialization.SR loader = GetLoader(); - if (loader == null) + LogicBuilder.ComponentModel.Design.Serialization.SR loaderLocal = GetLoader(); + if (loaderLocal == null) { return null; } - return loader.resources.GetString(name, Culture); + return loaderLocal.resources.GetString(name, Culture); } public static string GetString(string name, params object[] args) { - LogicBuilder.ComponentModel.Design.Serialization.SR loader = GetLoader(); - if (loader == null) + LogicBuilder.ComponentModel.Design.Serialization.SR loaderLocal = GetLoader(); + if (loaderLocal == null) { return null; } - string format = loader.resources.GetString(name, Culture); + string format = loaderLocal.resources.GetString(name, Culture); if ((args == null) || (args.Length <= 0)) { return format;