Skip to content

Commit

Permalink
Remove the defunct DiagnosticUtility/Fx classes from DataContractSeri…
Browse files Browse the repository at this point in the history
…alization (#82324)

- Fx.Assert just delegated to Debug.Assert: replace all the call sites with Debug.Assert/Fail.
- DiagnosticUtility.DebugAssert just delegated to Debug.Assert: replace all the call sites with Debug.Assert/Fail.
- DiagnosticUtility.ExceptionUtility.ThrowHelperError didn't throw anything and just returned its argument: removed all uses of it
- DiagnosticUtility.ExceptionUtility.ThrowHelperCallback didn't throw anything and just returned its argument: removed all uses of it.
- DiagnosticUtility.ExceptionUtility.ThrowHelperFatal didn't throw anything and just wrapped its arguments in a new Exception that was then returned: just changed the call sites to create that exception.
- DiagnosticUtility.ExceptionUtility.ThrowHelperArgument{Null} didn't throw anything and just returned new instances of Argument{Null}Exception: just changed the call sites to create that exception.
- Replaced some argument validation with helpers like ThrowIfNegative.
- Fx.IsFatal: moved to ExceptionUtility class
  • Loading branch information
stephentoub authored Mar 14, 2023
1 parent a78f9bd commit 93ab46b
Show file tree
Hide file tree
Showing 69 changed files with 818 additions and 1,101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,6 @@
<data name="AttributedTypesCannotInheritFromNonAttributedSerializableTypes" xml:space="preserve">
<value>Type '{0}' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type '{1}' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.</value>
</data>
<data name="QuotaMustBePositive" xml:space="preserve">
<value>Quota must be a positive value.</value>
</data>
<data name="QuotaIsReadOnly" xml:space="preserve">
<value>The '{0}' quota is readonly.</value>
</data>
Expand Down Expand Up @@ -543,9 +540,6 @@
<data name="UnknownConstantType" xml:space="preserve">
<value>Internal Error: Unrecognized constant type {0}.</value>
</data>
<data name="ValueMustBeNonNegative" xml:space="preserve">
<value>The value of this argument must be non-negative.</value>
</data>
<data name="ValueTypeCannotBeNull" xml:space="preserve">
<value>ValueType '{0}' cannot be null.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<Compile Include="$(RuntimeSerializationSources)\CollectionDataContract.cs" />
<Compile Include="$(RuntimeSerializationSources)\DataContract.cs" />
<Compile Include="$(RuntimeSerializationSources)\DateTimeOffsetAdapter.cs" />
<Compile Include="$(RuntimeSerializationSources)\DiagnosticUtility.cs" />
<Compile Include="$(RuntimeSerializationSources)\DictionaryGlobals.cs" />
<Compile Include="$(RuntimeSerializationSources)\DataContractResolver.cs" />
<Compile Include="$(RuntimeSerializationSources)\DataContractSerializer.cs" />
Expand All @@ -35,6 +34,7 @@
<Compile Include="$(RuntimeSerializationSources)\DataContractSurrogateCaller.cs" />
<Compile Include="$(RuntimeSerializationSources)\DataMember.cs" />
<Compile Include="$(RuntimeSerializationSources)\EnumDataContract.cs" />
<Compile Include="$(RuntimeSerializationSources)\ExceptionUtility.cs" />
<Compile Include="$(RuntimeSerializationSources)\ExportOptions.cs" />
<Compile Include="$(RuntimeSerializationSources)\ExtensionDataObject.cs" />
<Compile Include="$(RuntimeSerializationSources)\ExtensionDataReader.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static Getter CreateGetter(MemberInfo memberInfo)
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType!), memberInfo.Name)));
throw new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType!), memberInfo.Name));
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ public static Setter CreateSetter(MemberInfo memberInfo)
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.NoSetMethodForProperty, propInfo.DeclaringType, propInfo.Name)));
throw new InvalidOperationException(SR.Format(SR.NoSetMethodForProperty, propInfo.DeclaringType, propInfo.Name));
}
}
else if (memberInfo is FieldInfo fieldInfo)
Expand All @@ -152,7 +152,7 @@ public static Setter CreateSetter(MemberInfo memberInfo)
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType!), memberInfo.Name)));
throw new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType!), memberInfo.Name));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void ReadId(XmlReaderDelegator reader)
Id = reader.ReadContentAsString();
if (string.IsNullOrEmpty(Id))
{
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.InvalidXsIdDefinition, Id)));
throw XmlObjectSerializer.CreateSerializationException(SR.Format(SR.InvalidXsIdDefinition, Id));
}
}

Expand All @@ -114,7 +114,7 @@ private void ReadRef(XmlReaderDelegator reader)
Ref = reader.ReadContentAsString();
if (string.IsNullOrEmpty(Ref))
{
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.InvalidXsRefDefinition, Ref)));
throw XmlObjectSerializer.CreateSerializationException(SR.Format(SR.InvalidXsRefDefinition, Ref));
}
}

Expand All @@ -127,7 +127,7 @@ private void ReadArraySize(XmlReaderDelegator reader)
{
ArraySZSize = reader.ReadContentAsInt();
if (ArraySZSize < 0)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.InvalidSizeDefinition, ArraySZSize)));
throw XmlObjectSerializer.CreateSerializationException(SR.Format(SR.InvalidSizeDefinition, ArraySZSize));
}

private void ReadXsiType(XmlReaderDelegator reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,10 @@ internal bool RequiresMemberAccessForRead(SecurityException? securityException)
{
if (securityException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractTypeNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType)),
securityException));
securityException);
}
return true;
}
Expand All @@ -421,11 +420,10 @@ internal bool RequiresMemberAccessForRead(SecurityException? securityException)
{
if (securityException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustIXmlSerialzableNoPublicConstructor,
DataContract.GetClrTypeFullName(UnderlyingType)),
securityException));
securityException);
}
return true;
}
Expand All @@ -434,11 +432,10 @@ internal bool RequiresMemberAccessForRead(SecurityException? securityException)
{
if (securityException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustNonAttributedSerializableTypeNoPublicConstructor,
DataContract.GetClrTypeFullName(UnderlyingType)),
securityException));
securityException);
}
return true;
}
Expand All @@ -447,12 +444,11 @@ internal bool RequiresMemberAccessForRead(SecurityException? securityException)
{
if (securityException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractOnDeserializingNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType),
OnDeserializing!.Name),
securityException));
securityException);
}
return true;
}
Expand All @@ -461,12 +457,11 @@ internal bool RequiresMemberAccessForRead(SecurityException? securityException)
{
if (securityException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractOnDeserializedNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType),
OnDeserialized!.Name),
securityException));
securityException);
}
return true;
}
Expand All @@ -481,21 +476,19 @@ internal bool RequiresMemberAccessForRead(SecurityException? securityException)
{
if (Members[i].MemberInfo is FieldInfo)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractFieldSetNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType),
Members[i].MemberInfo.Name),
securityException));
securityException);
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractPropertySetNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType),
Members[i].MemberInfo.Name),
securityException));
securityException);
}
}
return true;
Expand All @@ -519,11 +512,10 @@ internal bool RequiresMemberAccessForWrite(SecurityException? securityException)
{
if (securityException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractTypeNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType)),
securityException));
securityException);
}
return true;
}
Expand All @@ -535,12 +527,11 @@ internal bool RequiresMemberAccessForWrite(SecurityException? securityException)
{
if (securityException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractOnSerializingNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType),
OnSerializing!.Name),
securityException));
securityException);
}
return true;
}
Expand All @@ -549,12 +540,11 @@ internal bool RequiresMemberAccessForWrite(SecurityException? securityException)
{
if (securityException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractOnSerializedNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType),
OnSerialized!.Name),
securityException));
securityException);
}
return true;
}
Expand All @@ -569,21 +559,19 @@ internal bool RequiresMemberAccessForWrite(SecurityException? securityException)
{
if (Members[i].MemberInfo is FieldInfo)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractFieldGetNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType),
Members[i].MemberInfo.Name),
securityException));
securityException);
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new SecurityException(SR.Format(
throw new SecurityException(SR.Format(
SR.PartialTrustDataContractPropertyGetNotPublic,
DataContract.GetClrTypeFullName(UnderlyingType),
Members[i].MemberInfo.Name),
securityException));
securityException);
}
}
return true;
Expand Down Expand Up @@ -646,7 +634,7 @@ internal ClassDataContractCriticalHelper([DynamicallyAccessedMembers(DataContrac
if (IsISerializable)
{
if (HasDataContract)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.ISerializableCannotHaveDataContract, DataContract.GetClrTypeFullName(type))));
throw new InvalidDataContractException(SR.Format(SR.ISerializableCannotHaveDataContract, DataContract.GetClrTypeFullName(type)));
if (baseType != null && !(baseType.IsSerializable && Globals.TypeOfISerializable.IsAssignableFrom(baseType)))
baseType = null;
}
Expand All @@ -665,9 +653,8 @@ internal ClassDataContractCriticalHelper([DynamicallyAccessedMembers(DataContrac

if (BaseClassContract != null && BaseClassContract.IsNonAttributedType && !_isNonAttributedType)
{
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError
(new InvalidDataContractException(SR.Format(SR.AttributedTypesCannotInheritFromNonAttributedSerializableTypes,
DataContract.GetClrTypeFullName(type), DataContract.GetClrTypeFullName(baseType))));
throw new InvalidDataContractException(SR.Format(SR.AttributedTypesCannotInheritFromNonAttributedSerializableTypes,
DataContract.GetClrTypeFullName(type), DataContract.GetClrTypeFullName(baseType)));
}
}
else
Expand All @@ -678,7 +665,7 @@ internal ClassDataContractCriticalHelper([DynamicallyAccessedMembers(DataContrac
_hasExtensionData = (Globals.TypeOfIExtensibleDataObject.IsAssignableFrom(type));
if (_hasExtensionData && !HasDataContract && !IsNonAttributedType)
{
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.OnlyDataContractTypesCanHaveExtensionData, DataContract.GetClrTypeFullName(type))));
throw new InvalidDataContractException(SR.Format(SR.OnlyDataContractTypesCanHaveExtensionData, DataContract.GetClrTypeFullName(type)));
}

if (IsISerializable)
Expand Down Expand Up @@ -1252,7 +1239,7 @@ internal override DataContractDictionary? KnownDataContracts

ConstructorInfo? ctor = UnderlyingType.GetConstructor(Globals.ScanAllMembers, SerInfoCtorArgs);
if (ctor == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.SerializationInfo_ConstructorNotFound, DataContract.GetClrTypeFullName(UnderlyingType))));
throw XmlObjectSerializer.CreateSerializationException(SR.Format(SR.SerializationInfo_ConstructorNotFound, DataContract.GetClrTypeFullName(UnderlyingType)));

return ctor;
}
Expand All @@ -1269,7 +1256,7 @@ internal override DataContractDictionary? KnownDataContracts

ConstructorInfo? ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, Type.EmptyTypes);
if (ctor == null)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.NonAttributedSerializableTypesMustHaveDefaultConstructor, DataContract.GetClrTypeFullName(type))));
throw new InvalidDataContractException(SR.Format(SR.NonAttributedSerializableTypesMustHaveDefaultConstructor, DataContract.GetClrTypeFullName(type)));

return ctor;
}
Expand Down
Loading

0 comments on commit 93ab46b

Please sign in to comment.