diff --git a/XmlSchemaClassGenerator.Console/Program.cs b/XmlSchemaClassGenerator.Console/Program.cs
index f9c38d4..40782ae 100644
--- a/XmlSchemaClassGenerator.Console/Program.cs
+++ b/XmlSchemaClassGenerator.Console/Program.cs
@@ -120,7 +120,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "cit|collectionImplementationType=", "the default collection type implementation to use (default is null)", v => collectionImplementationType = v == null ? null : Type.GetType(v, true) },
{ "csm|collectionSettersMode=", @"generate a private, public or public setters
without backing field initialization for collections
-(default is Private; can be: {Private, Public, PublicWithoutConstructorInitialization})",
+(default is Private; can be: {Private, Public, PublicWithoutConstructorInitialization, Init})",
v =>
{
collectionSettersMode = v switch
@@ -129,6 +129,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
"pu" or "Public" => CollectionSettersMode.Public,
"puwci" or "PublicWithoutConstructorInitialization" =>
CollectionSettersMode.PublicWithoutConstructorInitialization,
+ "in" or "Init" => CollectionSettersMode.Init,
_ => CollectionSettersMode.Private
};
}},
diff --git a/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj b/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj
index 60746d1..bc00e48 100644
--- a/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj
+++ b/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj
@@ -12,7 +12,7 @@
false
false
false
- latest
+ preview
opencover
../coverage.xml
[XmlSchemaClassGenerator]*
diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs
index f9f0c47..6ced859 100644
--- a/XmlSchemaClassGenerator.Tests/XmlTests.cs
+++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs
@@ -23,14 +23,9 @@
namespace XmlSchemaClassGenerator.Tests
{
[TestCaseOrderer("XmlSchemaClassGenerator.Tests.PriorityOrderer", "XmlSchemaClassGenerator.Tests")]
- public class XmlTests
+ public class XmlTests(ITestOutputHelper output)
{
- private readonly ITestOutputHelper Output;
-
- public XmlTests(ITestOutputHelper output)
- {
- Output = output;
- }
+ private readonly ITestOutputHelper Output = output;
private static IEnumerable ConvertXml(string name, IEnumerable xsds, Generator generatorPrototype = null)
{
@@ -300,7 +295,8 @@ public void TestListWithPublicPropertySetters()
[Fact]
public void TestListWithPublicPropertySettersWithoutConstructors()
{
- var assembly = Compiler.Generate("ListPublicWithoutConstructorInitialization", ListPattern, new Generator {
+ var assembly = Compiler.Generate("ListPublicWithoutConstructorInitialization", ListPattern, new Generator
+ {
GenerateNullables = true,
IntegerDataType = typeof(int),
DataAnnotationMode = DataAnnotationMode.All,
@@ -317,10 +313,45 @@ public void TestListWithPublicPropertySettersWithoutConstructors()
var myClassType = assembly.GetType("List.MyClass");
Assert.NotNull(myClassType);
var iListType = typeof(Collection<>);
- var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p=>p.Name).ToList();
- var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p=>p.Name).ToList();
+ var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p => p.Name).ToList();
+ var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p => p.Name).ToList();
+ Assert.NotEmpty(collectionPropertyInfos);
+ Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
+ var myClassInstance = Activator.CreateInstance(myClassType);
+ foreach (var collectionPropertyInfo in publicCollectionPropertyInfos)
+ {
+ Assert.Null(collectionPropertyInfo.GetValue(myClassInstance));
+ }
+ }
+
+ [Fact]
+ public void TestListWithInitPropertySettersWithoutConstructors()
+ {
+ var assembly = Compiler.Generate("ListInitWithoutConstructorInitialization", ListPattern, new Generator
+ {
+ GenerateNullables = true,
+ IntegerDataType = typeof(int),
+ DataAnnotationMode = DataAnnotationMode.All,
+ GenerateDesignerCategoryAttribute = false,
+ GenerateComplexTypesForCollections = true,
+ EntityFramework = false,
+ GenerateInterfaces = true,
+ NamespacePrefix = "List",
+ GenerateDescriptionAttribute = true,
+ TextValuePropertyName = "Value",
+ CollectionSettersMode = CollectionSettersMode.Init
+ });
+ Assert.NotNull(assembly);
+ var myClassType = assembly.GetType("List.MyClass");
+ Assert.NotNull(myClassType);
+ var iListType = typeof(Collection<>);
+ var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p => p.Name).ToList();
+ var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p => p.Name).ToList();
Assert.NotEmpty(collectionPropertyInfos);
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
+ var requiredCustomModifiers = collectionPropertyInfos.Select(p => p.SetMethod.ReturnParameter.GetRequiredCustomModifiers()).ToList();
+ Assert.Equal(collectionPropertyInfos.Count, requiredCustomModifiers.Count);
+ Assert.All(requiredCustomModifiers, m => Assert.Contains(typeof(System.Runtime.CompilerServices.IsExternalInit), m));
var myClassInstance = Activator.CreateInstance(myClassType);
foreach (var collectionPropertyInfo in publicCollectionPropertyInfos)
{
@@ -393,14 +424,16 @@ public void TestListWithPublicPropertySettersWithoutConstructorsSpecified()
}
}
- public static IEnumerable