in src/Json.Schema.ToDotNet/ClassGenerator.cs [958:1012]
private StatementSyntax GenerateDictionaryInitializationWithCollectionElements(string propertyName)
{
string argName = propertyName.ToCamelCase();
string dictionaryElementInfoKey = PropertyInfoDictionary.MakeDictionaryItemKeyName(propertyName);
string listElementInfoKey = PropertyInfoDictionary.MakeElementKeyName(dictionaryElementInfoKey);
TypeSyntax dictionaryType = PropInfoDictionary.GetConcreteDictionaryType(propertyName);
string dictionaryElementVariableName = _localVariableNameGenerator.GetNextCollectionElementVariableName();
ExpressionSyntax dictionaryElement = SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName(dictionaryElementVariableName),
SyntaxFactory.IdentifierName(ValuePropertyName));
string listElementVariableName = _localVariableNameGenerator.GetNextCollectionElementVariableName();
string collectionVariableName = _localVariableNameGenerator.GetNextDestinationVariableName();
return SyntaxFactory.IfStatement(
// if (foo != null)
SyntaxHelper.IsNotNull(argName),
SyntaxFactory.Block(
// Foo = new Dictionary<string, IList<D>>();
SyntaxFactory.ExpressionStatement(
SyntaxFactory.AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
SyntaxFactory.IdentifierName(propertyName),
SyntaxFactory.ObjectCreationExpression(
dictionaryType,
SyntaxFactory.ArgumentList(),
default(InitializerExpressionSyntax)))),
// foreach (var value_0 in foo)
SyntaxFactory.ForEachStatement(
SyntaxHelper.Var(),
dictionaryElementVariableName,
SyntaxFactory.IdentifierName(argName),
SyntaxFactory.Block(
DeclareCollection(
PropInfoDictionary.GetConcreteListType(dictionaryElementInfoKey), collectionVariableName),
GenerateElementInitializationLoop(
listElementVariableName,
dictionaryElement,
listElementInfoKey,
collectionVariableName),
SyntaxFactory.ExpressionStatement(
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName(propertyName),
SyntaxFactory.IdentifierName(AddMethodName)),
SyntaxHelper.ArgumentList(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName(dictionaryElementVariableName),
SyntaxFactory.IdentifierName(KeyPropertyName)),
SyntaxFactory.IdentifierName(collectionVariableName))))))));
}