Skip to content

Improve PaddingConverter CreateInstance implementation#2679

Merged
RussKie merged 2 commits intodotnet:masterfrom
weltkante:PaddingTypeConverter
Jan 17, 2020
Merged

Improve PaddingConverter CreateInstance implementation#2679
RussKie merged 2 commits intodotnet:masterfrom
weltkante:PaddingTypeConverter

Conversation

@weltkante
Copy link
Contributor

@weltkante weltkante commented Jan 9, 2020

Fixes #2677

Proposed changes

  • The context argument may be null for callers other than the PropertyGrid, this should not lead to an exception, in particular since GetCreateInstanceSupported returns true for a null context. Also every other implementation of CreateInstance I could find in WinForms and System.Drawing supports a null context, so PaddingConverter is an outlier.

Regression?

  • No

Risk

  • Callers which depend on an exception being thrown in a previously unsupported scenario will no longer see an exception

Before

  • PaddingConverter.CreateInstance(null, properties) throws an exception even though PaddingConverter.GetCreateInstanceSupported(null) returns true

After

  • PaddingConverter.CreateInstance(null, properties) creates an instance based on the given properties like any other TypeConverter defined in WinForms and System.Drawing

Test methodology

  • Tests have not been updated yet, this is WIP for discussion
Microsoft Reviewers: Open in CodeFlow

@weltkante weltkante requested a review from a team as a code owner January 9, 2020 18:55
@RussKie RussKie added api-ready-for-review (2) API is ready for formal API review; applied by the issue owner design-discussion Ongoing discussion about design without consensus labels Jan 10, 2020
@RussKie
Copy link
Contributor

RussKie commented Jan 10, 2020

I think this is a sensible proposal.
Looks like all other converters do the right thing and use context if it's not null. None of them throw.
Though it appears we have few more possible NREs related to use of context in MdiWindowListItemConverter and LanguageCultureInfoConverter.

On a side note, a number of UITypeEditors appear to like to live dangerously and use context without checks for null.

@RussKie
Copy link
Contributor

RussKie commented Jan 10, 2020

I'm curious, is this reachable at all?

            catch (NullReferenceException nullRef)
            {
                throw new ArgumentException(SR.PropertyValueInvalidEntry, nameof(propertyValues), nullRef);
            }

@RussKie RussKie added api-approved (4) API was approved in API review, it can be implemented and removed api-ready-for-review (2) API is ready for formal API review; applied by the issue owner design-discussion Ongoing discussion about design without consensus labels Jan 10, 2020
@weltkante
Copy link
Contributor Author

I'm curious, is this reachable at all?

You get this instead of a typecast exception when a value is null; also values are null for missing keys when the caller uses a Hashtable to store the properties.

@weltkante weltkante changed the title [WIP] Improve PaddingConverter CreateInstance implementation Improve PaddingConverter CreateInstance implementation Jan 17, 2020
@weltkante
Copy link
Contributor Author

Updated the tests. I think the existing tests (with updated expectations) already cover the modified code paths sufficiently.

@codecov
Copy link

codecov bot commented Jan 17, 2020

Codecov Report

Merging #2679 into master will increase coverage by 0.563%.
The diff coverage is 100%.

@@                 Coverage Diff                 @@
##              master       #2679         +/-   ##
===================================================
+ Coverage   57.59523%   58.15824%   +0.56301%     
===================================================
  Files           1235        1240          +5     
  Lines         418368      423008       +4640     
  Branches       38816       38852         +36     
===================================================
+ Hits          240960      246014       +5054     
+ Misses        172035      171620        -415     
- Partials        5373        5374          +1
Flag Coverage Δ
#Debug 58.15824% <100%> (+0.563%) ⬆️
#production 31.49243% <ø> (+0.17304%) ⬆️
#test 98.92984% <100%> (-0.00364%) ⬇️

@RussKie RussKie merged commit 8fe7c9b into dotnet:master Jan 17, 2020
@ghost ghost added this to the 5.0 milestone Jan 17, 2020
@RussKie
Copy link
Contributor

RussKie commented Jan 17, 2020

Thank you

@weltkante weltkante deleted the PaddingTypeConverter branch April 29, 2020 17:54
@ghost ghost locked as resolved and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api-approved (4) API was approved in API review, it can be implemented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve Padding TypeConverter CreateInstance implementation

2 participants