Skip to content

[MilCodeGen] Optimize generated ValueSerializers code style and usings #10648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@
// Please see MilCodeGen.html for more information.
//

using MS.Internal;
using MS.Internal.KnownBoxes;
using MS.Internal.Collections;
using MS.Utility;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.Windows.Media.Composition;
using System.Windows.Media;
using System.Windows.Markup;
using System.Windows.Media.Converters;

using ConverterHelper = System.Windows.Markup.TypeConverterHelper;

namespace System.Windows.Media.Converters
{
Expand All @@ -45,49 +36,30 @@ public override bool CanConvertFromString(string value, IValueSerializerContext
public override bool CanConvertToString(object value, IValueSerializerContext context)
{
// When invoked by the serialization engine we can convert to string only for some instances
if (!(value is Brush))
{
return false;
}

Brush instance = (Brush) value;

return instance.CanSerializeToString();
return value is Brush brush && brush.CanSerializeToString();
}

/// <summary>
/// Converts a string into a Brush.
/// </summary>
public override object ConvertFromString(string value, IValueSerializerContext context)
{
if (value != null)
{
return Brush.Parse(value, context );
}
else
{
return base.ConvertFromString( value, context );
}
return value is not null ? Brush.Parse(value, context) : base.ConvertFromString(value, context);
}

/// <summary>
/// Converts the value into a string.
/// </summary>
public override string ConvertToString(object value, IValueSerializerContext context)
{
if (value is Brush instance)
// When invoked by the serialization engine we can convert to string only for some instances
if (value is not Brush brush || !brush.CanSerializeToString())
{
// When invoked by the serialization engine we can convert to string only for some instances
if (!instance.CanSerializeToString())
{
// Let base throw an exception.
return base.ConvertToString(value, context);
}

return instance.ConvertToString(null, System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS);
// Let base throw an exception.
return base.ConvertToString(value, context);
}

return base.ConvertToString(value, context);
return brush.ConvertToString(null, ConverterHelper.InvariantEnglishUS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@
// Please see MilCodeGen.html for more information.
//

using MS.Internal;
using MS.Internal.KnownBoxes;
using MS.Internal.Collections;
using MS.Utility;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.Windows.Media.Composition;
using System.Windows.Media;
using System.Windows.Markup;
using System.Windows.Media.Converters;

using ConverterHelper = System.Windows.Markup.TypeConverterHelper;

namespace System.Windows.Media.Converters
{
Expand All @@ -45,49 +36,30 @@ public override bool CanConvertFromString(string value, IValueSerializerContext
public override bool CanConvertToString(object value, IValueSerializerContext context)
{
// When invoked by the serialization engine we can convert to string only for some instances
if (!(value is CacheMode))
{
return false;
}

CacheMode instance = (CacheMode) value;

return instance.CanSerializeToString();
return value is CacheMode cacheMode && cacheMode.CanSerializeToString();
}

/// <summary>
/// Converts a string into a CacheMode.
/// </summary>
public override object ConvertFromString(string value, IValueSerializerContext context)
{
if (value != null)
{
return CacheMode.Parse(value );
}
else
{
return base.ConvertFromString( value, context );
}
return value is not null ? CacheMode.Parse(value) : base.ConvertFromString(value, context);
}

/// <summary>
/// Converts the value into a string.
/// </summary>
public override string ConvertToString(object value, IValueSerializerContext context)
{
if (value is CacheMode instance)
// When invoked by the serialization engine we can convert to string only for some instances
if (value is not CacheMode cacheMode || !cacheMode.CanSerializeToString())
{
// When invoked by the serialization engine we can convert to string only for some instances
if (!instance.CanSerializeToString())
{
// Let base throw an exception.
return base.ConvertToString(value, context);
}

return instance.ConvertToString(null, System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS);
// Let base throw an exception.
return base.ConvertToString(value, context);
}

return base.ConvertToString(value, context);
return cacheMode.ConvertToString(null, ConverterHelper.InvariantEnglishUS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@
// Please see MilCodeGen.html for more information.
//

using MS.Internal;
using MS.Internal.KnownBoxes;
using MS.Internal.Collections;
using MS.Utility;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.Windows.Media.Composition;
using System.Windows.Media;
using System.Windows.Markup;
using System.Windows.Media.Converters;

using ConverterHelper = System.Windows.Markup.TypeConverterHelper;

namespace System.Windows.Media.Converters
{
Expand All @@ -45,41 +36,29 @@ public override bool CanConvertFromString(string value, IValueSerializerContext
public override bool CanConvertToString(object value, IValueSerializerContext context)
{
// Validate the input type
if (!(value is DoubleCollection))
{
return false;
}

return true;
return value is DoubleCollection;
}

/// <summary>
/// Converts a string into a DoubleCollection.
/// </summary>
public override object ConvertFromString(string value, IValueSerializerContext context)
{
if (value != null)
{
return DoubleCollection.Parse(value );
}
else
{
return base.ConvertFromString( value, context );
}
return value is not null ? DoubleCollection.Parse(value) : base.ConvertFromString(value, context);
}

/// <summary>
/// Converts the value into a string.
/// </summary>
public override string ConvertToString(object value, IValueSerializerContext context)
{
if (value is DoubleCollection instance)
if (value is not DoubleCollection doubleCollection)
{

return instance.ConvertToString(null, System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS);
// Let base throw an exception.
return base.ConvertToString(value, context);
}

return base.ConvertToString(value, context);
return doubleCollection.ConvertToString(null, ConverterHelper.InvariantEnglishUS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@
// Please see MilCodeGen.html for more information.
//

using MS.Internal;
using MS.Internal.KnownBoxes;
using MS.Internal.Collections;
using MS.Utility;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.Windows.Media.Composition;
using System.Windows.Media;
using System.Windows.Markup;
using System.Windows.Media.Converters;

using ConverterHelper = System.Windows.Markup.TypeConverterHelper;

namespace System.Windows.Media.Converters
{
Expand All @@ -45,49 +36,30 @@ public override bool CanConvertFromString(string value, IValueSerializerContext
public override bool CanConvertToString(object value, IValueSerializerContext context)
{
// When invoked by the serialization engine we can convert to string only for some instances
if (!(value is Geometry))
{
return false;
}

Geometry instance = (Geometry) value;

return instance.CanSerializeToString();
return value is Geometry geometry && geometry.CanSerializeToString();
}

/// <summary>
/// Converts a string into a Geometry.
/// </summary>
public override object ConvertFromString(string value, IValueSerializerContext context)
{
if (value != null)
{
return Geometry.Parse(value );
}
else
{
return base.ConvertFromString( value, context );
}
return value is not null ? Geometry.Parse(value) : base.ConvertFromString(value, context);
}

/// <summary>
/// Converts the value into a string.
/// </summary>
public override string ConvertToString(object value, IValueSerializerContext context)
{
if (value is Geometry instance)
// When invoked by the serialization engine we can convert to string only for some instances
if (value is not Geometry geometry || !geometry.CanSerializeToString())
{
// When invoked by the serialization engine we can convert to string only for some instances
if (!instance.CanSerializeToString())
{
// Let base throw an exception.
return base.ConvertToString(value, context);
}

return instance.ConvertToString(null, System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS);
// Let base throw an exception.
return base.ConvertToString(value, context);
}

return base.ConvertToString(value, context);
return geometry.ConvertToString(null, ConverterHelper.InvariantEnglishUS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@
// Please see MilCodeGen.html for more information.
//

using MS.Internal;
using MS.Internal.KnownBoxes;
using MS.Internal.Collections;
using MS.Utility;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.Windows.Media.Composition;
using System.Windows.Media;
using System.Windows.Markup;
using System.Windows.Media.Converters;

using ConverterHelper = System.Windows.Markup.TypeConverterHelper;

namespace System.Windows.Media.Converters
{
Expand All @@ -45,41 +36,29 @@ public override bool CanConvertFromString(string value, IValueSerializerContext
public override bool CanConvertToString(object value, IValueSerializerContext context)
{
// Validate the input type
if (!(value is Int32Collection))
{
return false;
}

return true;
return value is Int32Collection;
}

/// <summary>
/// Converts a string into a Int32Collection.
/// </summary>
public override object ConvertFromString(string value, IValueSerializerContext context)
{
if (value != null)
{
return Int32Collection.Parse(value );
}
else
{
return base.ConvertFromString( value, context );
}
return value is not null ? Int32Collection.Parse(value) : base.ConvertFromString(value, context);
}

/// <summary>
/// Converts the value into a string.
/// </summary>
public override string ConvertToString(object value, IValueSerializerContext context)
{
if (value is Int32Collection instance)
if (value is not Int32Collection int32Collection)
{

return instance.ConvertToString(null, System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS);
// Let base throw an exception.
return base.ConvertToString(value, context);
}

return base.ConvertToString(value, context);
return int32Collection.ConvertToString(null, ConverterHelper.InvariantEnglishUS);
}
}
}
Loading