Skip to content
Open
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 @@ -486,7 +486,8 @@ private void GenerateType(DataContract dataContract, ContractCodeDomInfo contrac
if (containingContractCodeDomInfo.ReferencedTypeExists)
return null;

CodeTypeDeclaration containingType = containingContractCodeDomInfo.TypeDeclaration!; // Nested types by definition have containing types.
Debug.Assert(containingContractCodeDomInfo.TypeDeclaration != null, "Nested types have containing types by definition - types with declaration");
CodeTypeDeclaration containingType = containingContractCodeDomInfo.TypeDeclaration;
if (TypeContainsNestedType(containingType, nestedTypeName))
{
for (int i = 1; ; i++)
Expand All @@ -502,7 +503,8 @@ private void GenerateType(DataContract dataContract, ContractCodeDomInfo contrac

CodeTypeDeclaration type = CreateTypeDeclaration(nestedTypeName, dataContract);
containingType.Members.Add(type);
contractCodeDomInfo.TypeReference = new CodeTypeReference(containingContractCodeDomInfo.TypeReference!.BaseType + "+" + nestedTypeName); // Again, nested types by definition have containing types.
Debug.Assert(containingContractCodeDomInfo.TypeReference != null, "Nested types have containing types by definition - types with reference");
contractCodeDomInfo.TypeReference = new CodeTypeReference(containingContractCodeDomInfo.TypeReference.BaseType + "+" + nestedTypeName);

if (GenerateInternalTypes)
type.TypeAttributes = TypeAttributes.NestedAssembly;
Expand All @@ -518,8 +520,9 @@ private static CodeTypeDeclaration CreateTypeDeclaration(string typeName, DataCo
CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration(typeof(GeneratedCodeAttribute).FullName!);

AssemblyName assemblyName = Assembly.GetExecutingAssembly().GetName();
generatedCodeAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Name!)));
generatedCodeAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Version?.ToString()!)));
Debug.Assert(assemblyName.Name != null, $"Current executing assembly name is not expected to be null in {nameof(CodeExporter)}.{nameof(CreateTypeDeclaration)} scenario");
generatedCodeAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Name)));
generatedCodeAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Version?.ToString())));

// System.Diagnostics.DebuggerStepThroughAttribute not allowed on enums
// ensure that the attribute is only generated on types that are not enums
Expand Down Expand Up @@ -607,6 +610,7 @@ private static CodeTypeDeclaration CreateTypeDeclaration(string typeName, DataCo
if (!TryGetReferencedDictionaryType(collectionContract, out typeReference))
{
// ItemContract - aka BaseContract - is never null for CollectionDataContract
Debug.Assert(collectionContract.BaseContract != null, "BaseContract should not be null for CollectionDataContract");
DataContract itemContract = collectionContract.BaseContract!;
if (collectionContract.IsDictionaryLike(out _, out _, out _))
{
Expand All @@ -629,6 +633,7 @@ private static CodeTypeDeclaration CreateTypeDeclaration(string typeName, DataCo
private static bool HasDefaultCollectionNames(DataContract collectionContract)
{
Debug.Assert(collectionContract.Is(DataContractType.CollectionDataContract));
Debug.Assert(collectionContract.BaseContract != null, "BaseContract should not be null for CollectionDataContract");

// ItemContract - aka BaseContract - is never null for CollectionDataContract
DataContract itemContract = collectionContract.BaseContract!;
Expand All @@ -647,6 +652,7 @@ private static bool HasDefaultCollectionNames(DataContract collectionContract)
private bool TryGetReferencedDictionaryType(DataContract collectionContract, [NotNullWhen(true)] out CodeTypeReference? typeReference)
{
Debug.Assert(collectionContract.Is(DataContractType.CollectionDataContract));
Debug.Assert(collectionContract.BaseContract != null, "BaseContract should not be null for CollectionDataContract");

// Check if it is a dictionary and use referenced dictionary type if present
if (collectionContract.IsDictionaryLike(out _, out _, out _)
Expand Down Expand Up @@ -819,7 +825,8 @@ private void ExportClassDataContract(DataContract classDataContract, ContractCod
{
ContractCodeDomInfo baseContractCodeDomInfo = GetContractCodeDomInfo(classDataContract.BaseContract);
Debug.Assert(baseContractCodeDomInfo.IsProcessed, "Cannot generate code for type if code for base type has not been generated");
type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference!);
Debug.Assert(baseContractCodeDomInfo.TypeReference != null, "Class data contracts should have non-null TypeReference");
type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference);
AddBaseMemberNames(baseContractCodeDomInfo, contractCodeDomInfo);
if (baseContractCodeDomInfo.ReferencedTypeExists)
{
Expand Down Expand Up @@ -1068,6 +1075,7 @@ private void ExportEnumDataContract(DataContract enumDataContract, ContractCodeD

CodeTypeDeclaration type = contractCodeDomInfo.TypeDeclaration;
// BaseContract is never null for EnumDataContract
Debug.Assert(enumDataContract.BaseContract != null, "BaseContract should not be null for EnumDataContract");
Type baseType = enumDataContract.BaseContract!.UnderlyingType;
type.IsEnum = true;
type.BaseTypes.Add(baseType);
Expand Down Expand Up @@ -1152,7 +1160,9 @@ private void ExportISerializableDataContract(DataContract classDataContract, Con
{
ContractCodeDomInfo baseContractCodeDomInfo = GetContractCodeDomInfo(classDataContract.BaseContract);
GenerateType(classDataContract.BaseContract, baseContractCodeDomInfo);
type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference!);

Debug.Assert(baseContractCodeDomInfo.TypeReference != null, "Class data contracts should have non-null TypeReference");
type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference);
if (baseContractCodeDomInfo.ReferencedTypeExists)
{
Type? actualType = (Type?)baseContractCodeDomInfo.TypeReference?.UserData[s_codeUserDataActualTypeKey];
Expand Down Expand Up @@ -1202,8 +1212,8 @@ private void ExportCollectionDataContract(DataContract collectionContract, Contr
SR.Format(SR.CannotUseGenericTypeAsBase, dataContractName,
collectionContract.XmlName.Namespace)));

// ItemContract - aka BaseContract - is never null for CollectionDataContract
DataContract itemContract = collectionContract.BaseContract!;
Debug.Assert(collectionContract.BaseContract != null, "BaseContract should not be null for CollectionDataContract");
DataContract itemContract = collectionContract.BaseContract;
bool isItemTypeNullable = collectionContract.IsItemTypeNullable();
bool isDictionary = collectionContract.IsDictionaryLike(out string? keyName, out string? valueName, out string? itemName);

Expand Down Expand Up @@ -1235,15 +1245,17 @@ private void ExportCollectionDataContract(DataContract collectionContract, Contr

// This is supposed to be set by GenerateType. If it wasn't, there is a problem.
Debug.Assert(contractCodeDomInfo.TypeDeclaration != null);
Debug.Assert(baseTypeReference != null, "Base type reference should not be null for Dictionary/List collection data contracts");

CodeTypeDeclaration generatedType = contractCodeDomInfo.TypeDeclaration;
generatedType.BaseTypes.Add(baseTypeReference!);
generatedType.BaseTypes.Add(baseTypeReference);
CodeAttributeDeclaration collectionContractAttribute = new CodeAttributeDeclaration(GetClrTypeFullName(typeof(CollectionDataContractAttribute)));
collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(ImportGlobals.NameProperty, new CodePrimitiveExpression(dataContractName)));
collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(ImportGlobals.NamespaceProperty, new CodePrimitiveExpression(collectionContract.XmlName.Namespace)));
if (collectionContract.IsReference != ImportGlobals.DefaultIsReference)
collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(ImportGlobals.IsReferenceProperty, new CodePrimitiveExpression(collectionContract.IsReference)));
collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(ImportGlobals.ItemNameProperty, new CodePrimitiveExpression(GetNameForAttribute(itemName!)))); // ItemName is never null for Collection contracts.
Debug.Assert(itemName != null, "ItemName is never null for Collection contracts.");
collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(ImportGlobals.ItemNameProperty, new CodePrimitiveExpression(GetNameForAttribute(itemName))));
if (foundDictionaryBase)
{
// These are not null if we are working with a dictionary. See CollectionDataContract.IsDictionary
Expand Down
Loading