Bonjour,

Soit deux classes dont je n'ai pas le source et que je ne peux pas modifier :
Code csharp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
        public class Contenu
        {
            public Contenu()
            {
 
            }
        }
 
        public class Conteneur
        {
            public Conteneur(params Contenu[] contenus)
            {
 
            }
        }

Et le code suivant, de mon cru :
Code csharp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
   int[] tab = new int[4];
   tab[0] = 1;
   tab[1] = 0;
   tab[2] = 5;
   tab[3] = 0;
   var conteneur = new Conteneur(
                                      new Contenu(tab[0]),
                                      new Contenu(tab[1]),
                                      new Contenu(tab[2]),
                                      new Contenu(tab[3])
                           );

Nickel, ça marche.

Maintenant, sans modifier les classes, uniquement le code appelant, et sans remettre en cause toute la structure du code (donc conserver la déclaration inline du Contenu[] passé en paramètre à mon Conteneur()) n'envoyer que les Contenu() pour lesquels le paramètre du constructeur est différent de 0.

En gros, je voudrais un truc du genre :
Code csharp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
   int[] tab = new int[4];
   tab[0] = 1;
   tab[1] = 0;
   tab[2] = 5;
   tab[3] = 0;
   var conteneur = new Conteneur(
                                      if (tab[0] > 0) {new Contenu(tab[0])},
                                      if (tab[1] > 0) {new Contenu(tab[1])},
                                      if (tab[2] > 0) {new Contenu(tab[2])},
                                      if (tab[3] > 0) {new Contenu(tab[3])}
                           );

Évidemment, ça ne compile pas.
Pensez-vous qu'il existe une syntaxe qui me permette de m'en sortir ?

Pour les sceptiques, voici le code "réel" avec lequel je me dépatouille (merci à Microsoft et son OpenXml... et ses classes en bois permettant de le manipuler !)
Code csharp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
 
        private static Document GenerateMainDocumentPart(ClaimHeader header, List<ClaimLine> lines)
        {
            var element =
                new Document(
                    new Body(
                        // Premier tableau (entête de la réclamation)
                        new Table(
                            new TableProperties()
                            {
                                TableWidth = new TableWidth()
                                {
                                    Width = "10480"
                                },
                                TableBorders = new TableBorders()
                                {
                                    TopBorder = new TopBorder()
                                    {
                                        Val = BorderValues.Single,
                                        Size = 8,
                                        Space = 0,
                                        Color = "2E74B5"
                                    },
                                    LeftBorder = new LeftBorder()
                                    {
                                        Val = BorderValues.Single,
                                        Size = 8,
                                        Space = 0,
                                        Color = "2E74B5"
                                    },
                                    RightBorder = new RightBorder()
                                    {
                                        Val = BorderValues.Single,
                                        Size = 8,
                                        Space = 0,
                                        Color = "2E74B5"
                                    },
                                    BottomBorder = new BottomBorder()
                                    {
                                        Val = BorderValues.Single,
                                        Size = 8,
                                        Space = 0,
                                        Color = "2E74B5"
                                    },
                                    InsideHorizontalBorder = new InsideHorizontalBorder()
                                    {
                                        Val = BorderValues.Single,
                                        Size = 8,
                                        Space = 0,
                                        Color = "2E74B5"
                                    },
                                    InsideVerticalBorder = new InsideVerticalBorder()
                                    {
                                        Val = BorderValues.Single,
                                        Size = 8,
                                        Space = 0,
                                        Color = "2E74B5"
                                    }
                                },
                                TableLook = new TableLook()
                                {
                                    Val = "04A0",
                                    FirstRow = true,
                                    LastRow = false,
                                    FirstColumn = true,
                                    LastColumn = false,
                                    NoHorizontalBand = false,
                                    NoVerticalBand = false
                                }
                            },
                            new TableGrid(
                                new GridColumn()
                                {
                                    Width = "5519"
                                },
                                new GridColumn()
                                {
                                    Width = "4961"
                                }
                            ),
                            new TableRow(
                                new TableRowProperties(
                                    new TableRowHeight()
                                    {
                                        Val = 2463
                                    }
                                ),
                                new TableCell(
                                    new TableCellProperties()
                                    {
                                        TableCellWidth = new TableCellWidth()
                                        {
                                            Width = "5519"
                                        }
                                    },
                                    new Paragraph(
                                        new ParagraphProperties(
                                            new Tabs(
                                                new TabStop()
                                                {
                                                    Val = TabStopValues.Left,
                                                    Position = 1000
                                                }
                                            )
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new Bold(),
                                                new Color()
                                                {
                                                    Val = "1F4E79"
                                                },
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new Text()
                                            {
                                                Text = Properties.Resources.STR_COMPANY_NAME
                                            }
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new TabChar()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new Color()
                                                {
                                                    Val = "000000"
                                                },
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new Text()
                                            {
                                                Text = header.Company
                                            }
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new Break()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new TabChar()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new TabChar()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new Color()
                                                {
                                                    Val = "000000"
                                                },
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new Text()
                                            {
                                                Text = header.Address1
                                            }
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new Break()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new TabChar()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new TabChar()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new Color()
                                                {
                                                    Val = "000000"
                                                },
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new Text()
                                            {
                                                Text = header.Address2
                                            }
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new Break()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new TabChar()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new TabChar()
                                        ),
                                        new Run(
                                            new RunProperties(
                                                new Color()
                                                {
                                                    Val = "000000"
                                                },
                                                new FontSize()
                                                {
                                                    Val = "20"
                                                }
                                            ),
                                            new Text()
                                            {
                                                Text = $"{header.PostalCode} {header.City}"
                                            }
                                        )
                                    )
                                )
                            )
                        ),
                        new Paragraph(
                            new Run(
                                new Text("Page 1 content"))
                        ),
                        new Paragraph(
                            new Run(
                                new Break() { Type = BreakValues.Page })
                        ),
                        // Second tableau (liste des produits)
                        new Paragraph(
                            new Run(
                                new LastRenderedPageBreak(),
                                new Text("Page 2 content"))
                        ),
                        new Paragraph(
                            new Run(
                                new Break() { Type = BreakValues.Page })
                        ),
                        new Paragraph(
                            new Run(
                                new LastRenderedPageBreak(),
                                new Text("Page 3 content"))
                        ),
                        new Paragraph(
                            new Run(
                                new Break() { Type = BreakValues.Page })
                        ),
                        new Paragraph(
                            new Run(
                                new LastRenderedPageBreak(),
                                new Text("Page 4 content"))
                        ),
                        new Paragraph(
                            new Run(
                                new Break() { Type = BreakValues.Page })
                        ),
                        new Paragraph(
                            new Run(
                                new LastRenderedPageBreak(),
                                new Text("Page 5 content"))
                        ),
                        new SectionProperties(
                            new HeaderReference()
                            {
                                Type = HeaderFooterValues.First,
                                Id = "rId2"
                            },
                            new HeaderReference()
                            {
                                Type = HeaderFooterValues.Even,
                                Id = "rId2"
                            },
                            new HeaderReference()
                            {
                                Type = HeaderFooterValues.Default,
                                Id = "rId2"
                            },
                            new PageMargin()
                            {
                                Top = 1440,
                                Right = (UInt32Value)1440UL,
                                Bottom = 1440,
                                Left = (UInt32Value)1440UL,
                                Header = (UInt32Value)720UL,
                                Footer = (UInt32Value)720UL,
                                Gutter = (UInt32Value)0UL
                            },
                            new TitlePage()
                        )
                    )
                );
 
            return element;
        }

Vous comprendrez pourquoi j'ai pas envie de créer des variables en plus du merdier intersidéral dans lequel j'ai mis les pieds...
Le but était par exemple de ne pas générer la ligne qui contient "header.Address2" si cette information est vide.