зеркало из https://github.com/dotnet/winforms.git
removed unnecessary casts that I had missed before (#12585)
removed unnecessary casts added a baseline test for the future changes
This commit is contained in:
Родитель
a1405f6bb3
Коммит
4673159e5d
|
@ -92,32 +92,30 @@ public unsafe partial class DataObject :
|
|||
internal IDataObject? OriginalIDataObject => _innerData.OriginalIDataObject;
|
||||
|
||||
#region IDataObject
|
||||
public virtual object? GetData(string format, bool autoConvert) =>
|
||||
((IDataObject)_innerData).GetData(format, autoConvert);
|
||||
public virtual object? GetData(string format, bool autoConvert) => _innerData.GetData(format, autoConvert);
|
||||
|
||||
public virtual object? GetData(string format) => GetData(format, autoConvert: true);
|
||||
|
||||
public virtual object? GetData(Type format) => format is null ? null : GetData(format.FullName!);
|
||||
|
||||
public virtual bool GetDataPresent(string format, bool autoConvert) =>
|
||||
((IDataObject)_innerData).GetDataPresent(format, autoConvert);
|
||||
public virtual bool GetDataPresent(string format, bool autoConvert) => _innerData.GetDataPresent(format, autoConvert);
|
||||
|
||||
public virtual bool GetDataPresent(string format) => GetDataPresent(format, autoConvert: true);
|
||||
|
||||
public virtual bool GetDataPresent(Type format) => format is not null && GetDataPresent(format.FullName!);
|
||||
|
||||
public virtual string[] GetFormats(bool autoConvert) => ((IDataObject)_innerData).GetFormats(autoConvert);
|
||||
public virtual string[] GetFormats(bool autoConvert) => _innerData.GetFormats(autoConvert);
|
||||
|
||||
public virtual string[] GetFormats() => GetFormats(autoConvert: true);
|
||||
|
||||
public virtual void SetData(string format, bool autoConvert, object? data)
|
||||
=> ((IDataObject)_innerData).SetData(format, autoConvert, data);
|
||||
public virtual void SetData(string format, bool autoConvert, object? data) =>
|
||||
_innerData.SetData(format, autoConvert, data);
|
||||
|
||||
public virtual void SetData(string format, object? data) => ((IDataObject)_innerData).SetData(format, data);
|
||||
public virtual void SetData(string format, object? data) => _innerData.SetData(format, data);
|
||||
|
||||
public virtual void SetData(Type format, object? data) => ((IDataObject)_innerData).SetData(format, data);
|
||||
public virtual void SetData(Type format, object? data) => _innerData.SetData(format, data);
|
||||
|
||||
public virtual void SetData(object? data) => ((IDataObject)_innerData).SetData(data);
|
||||
public virtual void SetData(object? data) => _innerData.SetData(data);
|
||||
#endregion
|
||||
|
||||
public virtual bool ContainsAudio() => GetDataPresent(DataFormats.WaveAudioConstant, autoConvert: false);
|
||||
|
@ -150,15 +148,15 @@ public unsafe partial class DataObject :
|
|||
|
||||
public virtual Image? GetImage() => GetData(DataFormats.Bitmap, autoConvert: true) as Image;
|
||||
|
||||
public virtual string GetText() => GetText(TextDataFormat.UnicodeText);
|
||||
|
||||
public virtual string GetText(TextDataFormat format)
|
||||
{
|
||||
// Valid values are 0x0 to 0x4
|
||||
SourceGenerated.EnumValidator.Validate(format, nameof(format));
|
||||
return GetData(ConvertToDataFormats(format), false) is string text ? text : string.Empty;
|
||||
return GetData(ConvertToDataFormats(format), autoConvert: false) is string text ? text : string.Empty;
|
||||
}
|
||||
|
||||
public virtual string GetText() => GetText(TextDataFormat.UnicodeText);
|
||||
|
||||
public virtual void SetAudio(byte[] audioBytes) => SetAudio(new MemoryStream(audioBytes.OrThrowIfNull()));
|
||||
|
||||
public virtual void SetAudio(Stream audioStream) =>
|
||||
|
|
|
@ -18,18 +18,6 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
|
||||
public void Dispose() => _stream.Dispose();
|
||||
|
||||
private object? RoundTripObject(object value)
|
||||
{
|
||||
Utilities.WriteObjectToStream(_stream, value, restrictSerialization: false);
|
||||
return ReadObjectFromStream();
|
||||
}
|
||||
|
||||
private object? RoundTripObject_RestrictedFormat(object value)
|
||||
{
|
||||
Utilities.WriteObjectToStream(_stream, value, restrictSerialization: true);
|
||||
return ReadObjectFromStream(restrictDeserialization: true);
|
||||
}
|
||||
|
||||
private void WriteObjectToStream(object value, bool restrictSerialization = false) =>
|
||||
Utilities.WriteObjectToStream(_stream, value, restrictSerialization);
|
||||
|
||||
|
@ -39,6 +27,22 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
return Utilities.ReadObjectFromStream(_stream, restrictDeserialization);
|
||||
}
|
||||
|
||||
private object? RoundTripObject(object value)
|
||||
{
|
||||
// This is equivalent to SetData/GetData methods with unbounded formats,
|
||||
// and works with the BinaryFormat AppContext switches.
|
||||
WriteObjectToStream(value);
|
||||
return ReadObjectFromStream();
|
||||
}
|
||||
|
||||
private object? RoundTripObject_RestrictedFormat(object value)
|
||||
{
|
||||
// This is equivalent to SetData/GetData methods using registered OLE formats and thus the BitmapBinder,
|
||||
// and works with the BF AppCompat switches.
|
||||
WriteObjectToStream(value, restrictSerialization: true);
|
||||
return ReadObjectFromStream(restrictDeserialization: true);
|
||||
}
|
||||
|
||||
// Primitive types as defined by the NRBF spec.
|
||||
// https://learn.microsoft.com/dotnet/api/system.formats.nrbf.primitivetyperecord
|
||||
public static TheoryData<object> PrimitiveObjects_TheoryData =>
|
||||
|
@ -54,17 +58,17 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
(float)9.0,
|
||||
10.0,
|
||||
'a',
|
||||
true
|
||||
true,
|
||||
"string",
|
||||
DateTime.Now,
|
||||
TimeSpan.FromHours(1),
|
||||
decimal.MaxValue
|
||||
];
|
||||
|
||||
public static TheoryData<object> KnownObjects_TheoryData =>
|
||||
[
|
||||
"string",
|
||||
DateTime.Now,
|
||||
TimeSpan.FromHours(1),
|
||||
-(nint)11,
|
||||
(nuint)12,
|
||||
decimal.MaxValue,
|
||||
new PointF(1, 2),
|
||||
new RectangleF(1, 2, 3, 4),
|
||||
new Point(-1, int.MaxValue),
|
||||
|
@ -167,7 +171,9 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
[
|
||||
new List<object>(),
|
||||
new List<nint>(),
|
||||
new List<(int, int)>()
|
||||
new List<(int, int)>(),
|
||||
new List<nint> { nint.MinValue, nint.MaxValue },
|
||||
new List<nuint> { nuint.MinValue, nuint.MaxValue }
|
||||
];
|
||||
|
||||
[Theory]
|
||||
|
@ -289,7 +295,10 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
[MemberData(nameof(Lists_UnsupportedTestData))]
|
||||
public void RoundTrip_Unsupported(IList value)
|
||||
{
|
||||
((Action)(() => WriteObjectToStream(value))).Should().Throw<NotSupportedException>();
|
||||
Action writer = () => WriteObjectToStream(value);
|
||||
Action reader = () => ReadObjectFromStream();
|
||||
|
||||
writer.Should().Throw<NotSupportedException>();
|
||||
|
||||
using (BinaryFormatterScope scope = new(enable: true))
|
||||
{
|
||||
|
@ -297,16 +306,45 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
ReadObjectFromStream().Should().BeEquivalentTo(value);
|
||||
}
|
||||
|
||||
((Action)(() => ReadObjectFromStream())).Should().Throw<NotSupportedException>();
|
||||
reader.Should().Throw<NotSupportedException>();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Lists_UnsupportedTestData))]
|
||||
public void RoundTrip_RestrictedFormat_Unsupported(IList value)
|
||||
{
|
||||
((Action)(() => WriteObjectToStream(value, restrictSerialization: true))).Should().Throw<NotSupportedException>();
|
||||
Action writer = () => WriteObjectToStream(value, restrictSerialization: true);
|
||||
writer.Should().Throw<NotSupportedException>();
|
||||
|
||||
using BinaryFormatterScope scope = new(enable: true);
|
||||
((Action)(() => WriteObjectToStream(value, restrictSerialization: true))).Should().Throw<SerializationException>();
|
||||
writer.Should().Throw<SerializationException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RoundTrip_OffsetArray()
|
||||
{
|
||||
Array value = Array.CreateInstance(typeof(uint), lengths: [2, 3], lowerBounds: [1, 2]);
|
||||
value.SetValue(101u, 1, 2);
|
||||
value.SetValue(102u, 1, 3);
|
||||
value.SetValue(103u, 1, 4);
|
||||
value.SetValue(201u, 2, 2);
|
||||
value.SetValue(202u, 2, 3);
|
||||
value.SetValue(203u, 2, 4);
|
||||
|
||||
// Can read offset array with the BinaryFormatter.
|
||||
using BinaryFormatterScope scope = new(enable: true);
|
||||
var result = RoundTripObject(value).Should().BeOfType<uint[,]>().Subject;
|
||||
|
||||
result.Rank.Should().Be(2);
|
||||
result.GetLength(0).Should().Be(2);
|
||||
result.GetLength(1).Should().Be(3);
|
||||
result.GetLowerBound(0).Should().Be(1);
|
||||
result.GetLowerBound(1).Should().Be(2);
|
||||
result.GetValue(1, 2).Should().Be(101u);
|
||||
result.GetValue(1, 3).Should().Be(102u);
|
||||
result.GetValue(1, 4).Should().Be(103u);
|
||||
result.GetValue(2, 2).Should().Be(201u);
|
||||
result.GetValue(2, 3).Should().Be(202u);
|
||||
result.GetValue(2, 4).Should().Be(203u);
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче