How Schema.NET handles single or multiple values and mixed types
mainSchema.NET uses generics and implicit conversion operators (via OneOrMany<T>, Values<T1, T2>, etc.) to allow a single property to accept a single value, a list of values, or different types (e.g., a string or a PostalAddress for an Address property).
Setting single or multiple values
// Single string address
var organization = new Organization() { Address = "123 Old Kent Road E10 6RL" };
// Multiple string addresses
var organization = new Organization() { Address = new List<string> { "Address 1", "Address 2" } };
// Single complex type (PostalAddress)
var organization = new Organization() { Address = new PostalAddress { StreetAddress = "123 Old Kent Road" } };Handling mixed types
If a property contains multiple possible types (e.g., an Author that can be a Person or an Organization), you can use deconstruction to separate them:
var book = new Book()
{
Author = new List<object>()
{
new Organization() { Name = "Penguin" },
new Person() { Name = "J.D. Salinger" }
}
};
// Deconstruct a property containing mixed types
if (book.Author.HasValue)
{
var (organisations, people) = book.Author.Value;
}// Mixed Author types
var book = new Book()
{
Author = new List<object>()
{
new Organization() { Name = "Penguin" },
new Person() { Name = "J.D. Salinger" }
}
};
// Deconstruct a property containing mixed types
if (book.Author.HasValue)
{
var (organisations, people) = book.Author.Value;
}