diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FileNameEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FileNameEditor.cs
index ba9202bf67c..e6e75c10d09 100644
--- a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FileNameEditor.cs
+++ b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FileNameEditor.cs
@@ -2,49 +2,45 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
using System.ComponentModel;
-using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Design;
namespace System.Windows.Forms.Design
{
/// >
- /// Provides an
- /// editor for filenames.
+ /// Provides an editor for filenames.
///
[CLSCompliant(false)]
public class FileNameEditor : UITypeEditor
{
- private OpenFileDialog openFileDialog;
+ private OpenFileDialog _openFileDialog;
///
- /// Edits the given object value using the editor style provided by
- /// GetEditorStyle. A service provider is provided so that any
- /// required editing services can be obtained.
+ /// Edits the given object value using the editor style provided by GetEditorStyle.
+ /// A service provider is provided so that any required editing services can be obtained.
///
- [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
- Debug.Assert(provider != null, "No service provider; we cannot edit the value");
if (provider != null)
{
- IWindowsFormsEditorService edSvc =
- (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
-
- Debug.Assert(edSvc != null, "No editor service; we cannot edit the value");
- if (edSvc != null)
+ if (provider.GetService(typeof(IWindowsFormsEditorService)) is IWindowsFormsEditorService edSvc)
{
- if (openFileDialog == null)
+ if (_openFileDialog == null)
{
- openFileDialog = new OpenFileDialog();
- InitializeDialog(openFileDialog);
+ _openFileDialog = new OpenFileDialog();
+ InitializeDialog(_openFileDialog);
}
- if (value is string) openFileDialog.FileName = (string)value;
+ if (value is string stringValue)
+ {
+ _openFileDialog.FileName = stringValue;
+ }
- if (openFileDialog.ShowDialog() == DialogResult.OK) value = openFileDialog.FileName;
+ if (_openFileDialog.ShowDialog() == DialogResult.OK)
+ {
+ return _openFileDialog.FileName;
+ }
}
}
@@ -52,24 +48,26 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide
}
/// >
- /// Gets the editing style of the Edit method. If the method
- /// is not supported, this will return None.
+ /// Gets the editing style of the Edit method.
///
- [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
- // everything in this assembly is full trust.
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
///
- /// Initializes the open file dialog when it is created. This gives you
- /// an opportunity to configure the dialog as you please. The default
- /// implementation provides a generic file filter and title.
+ /// Initializes the open file dialog when it is created. This gives you an opportunity to
+ /// configure the dialog as you please. The default implementation provides a generic file
+ /// filter and title.
///
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
protected virtual void InitializeDialog(OpenFileDialog openFileDialog)
{
+ if (openFileDialog == null)
+ {
+ throw new ArgumentNullException(nameof(openFileDialog));
+ }
+
openFileDialog.Filter = SR.GenericFileFilter;
openFileDialog.Title = SR.GenericOpenFile;
}
diff --git a/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/FileNameEditorTests.cs b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/FileNameEditorTests.cs
new file mode 100644
index 00000000000..90a245d1b92
--- /dev/null
+++ b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/FileNameEditorTests.cs
@@ -0,0 +1,73 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Collections.Generic;
+using System.Drawing.Design;
+using System.Windows.Forms;
+using System.Windows.Forms.Design;
+using Moq;
+using Xunit;
+
+namespace System.Windows.Forms.Design.Tests
+{
+ public class FileNameEditorTests
+ {
+ public static IEnumerable