Repository navigation
Expand file tree
/
Copy pathObjectFormatter.Formatter.cs
More file actions
895 lines (771 loc) · 35.6 KB
/
Copy pathObjectFormatter.Formatter.cs
File metadata and controls
895 lines (771 loc) · 35.6 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using Roslyn.Utilities;
using Ref = System.Reflection;
namespace Microsoft.CodeAnalysis.Scripting
{
public abstract partial class ObjectFormatter
{
// internal for testing
internal sealed class Formatter
{
private readonly ObjectFormatter _language;
private readonly ObjectFormattingOptions _options;
private HashSet<object> _lazyVisitedObjects;
private HashSet<object> VisitedObjects
{
get
{
if (_lazyVisitedObjects == null)
{
_lazyVisitedObjects = new HashSet<object>(ReferenceEqualityComparer.Instance);
}
return _lazyVisitedObjects;
}
}
public Formatter(ObjectFormatter language, ObjectFormattingOptions options)
{
_options = options ?? ObjectFormattingOptions.Default;
_language = language;
}
private Builder MakeMemberBuilder(int limit)
{
return new Builder(Math.Min(_options.MaxLineLength, limit), _options, insertEllipsis: false);
}
public string FormatObject(object obj)
{
try
{
var builder = new Builder(_options.MaxOutputLength, _options, insertEllipsis: true);
string _;
return FormatObjectRecursive(builder, obj, _options.QuoteStrings, _options.MemberFormat, out _).ToString();
}
catch (InsufficientExecutionStackException)
{
return ScriptingResources.StackOverflowWhileEvaluat;
}
}
private Builder FormatObjectRecursive(Builder result, object obj, bool quoteStrings, MemberDisplayFormat memberFormat, out string name)
{
name = null;
string primitive = _language.FormatPrimitive(obj, quoteStrings, _options.IncludeCodePoints, _options.UseHexadecimalNumbers);
if (primitive != null)
{
result.Append(primitive);
return result;
}
object originalObj = obj;
Type originalType = originalObj.GetType();
//
// Override KeyValuePair<,>.ToString() to get better dictionary elements formatting:
//
// { { format(key), format(value) }, ... }
// instead of
// { [key.ToString(), value.ToString()], ... }
//
// This is more general than overriding Dictionary<,> debugger proxy attribute since it applies on all
// types that return an array of KeyValuePair in their DebuggerDisplay to display items.
//
if (originalType.IsGenericType && originalType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
if (memberFormat != MemberDisplayFormat.InlineValue)
{
result.Append(_language.FormatTypeName(originalType, _options));
result.Append(' ');
}
FormatKeyValuePair(result, originalObj);
return result;
}
if (originalType.IsArray)
{
if (!VisitedObjects.Add(originalObj))
{
result.AppendInfiniteRecursionMarker();
return result;
}
FormatArray(result, (Array)originalObj, inline: memberFormat != MemberDisplayFormat.List);
VisitedObjects.Remove(originalObj);
return result;
}
DebuggerDisplayAttribute debuggerDisplay = GetApplicableDebuggerDisplayAttribute(originalType);
if (debuggerDisplay != null)
{
name = debuggerDisplay.Name;
}
bool suppressMembers = false;
//
// TypeName(count) for ICollection implementers
// or
// TypeName([[DebuggerDisplay.Value]]) // Inline
// [[DebuggerDisplay.Value]] // InlineValue
// or
// [[ToString()]] if ToString overridden
// or
// TypeName
//
ICollection collection;
if ((collection = originalObj as ICollection) != null)
{
FormatCollectionHeader(result, collection);
}
else if (debuggerDisplay != null && !String.IsNullOrEmpty(debuggerDisplay.Value))
{
if (memberFormat != MemberDisplayFormat.InlineValue)
{
result.Append(_language.FormatTypeName(originalType, _options));
result.Append('(');
}
FormatWithEmbeddedExpressions(result, debuggerDisplay.Value, originalObj);
if (memberFormat != MemberDisplayFormat.InlineValue)
{
result.Append(')');
}
suppressMembers = true;
}
else if (HasOverriddenToString(originalType))
{
ObjectToString(result, originalObj);
suppressMembers = true;
}
else
{
result.Append(_language.FormatTypeName(originalType, _options));
}
if (memberFormat == MemberDisplayFormat.NoMembers)
{
return result;
}
bool includeNonPublic = memberFormat == MemberDisplayFormat.List;
object proxy = GetDebuggerTypeProxy(obj);
if (proxy != null)
{
obj = proxy;
includeNonPublic = false;
suppressMembers = false;
}
if (memberFormat != MemberDisplayFormat.List && suppressMembers)
{
return result;
}
// TODO (tomat): we should not use recursion
RuntimeHelpers.EnsureSufficientExecutionStack();
result.Append(' ');
if (!VisitedObjects.Add(originalObj))
{
result.AppendInfiniteRecursionMarker();
return result;
}
// handle special types only if a proxy isn't defined
if (proxy == null)
{
IDictionary dictionary;
if ((dictionary = obj as IDictionary) != null)
{
FormatDictionary(result, dictionary, inline: memberFormat != MemberDisplayFormat.List);
return result;
}
IEnumerable enumerable;
if ((enumerable = obj as IEnumerable) != null)
{
FormatSequence(result, enumerable, inline: memberFormat != MemberDisplayFormat.List);
return result;
}
}
FormatObjectMembers(result, obj, originalType, includeNonPublic, inline: memberFormat != MemberDisplayFormat.List);
VisitedObjects.Remove(obj);
return result;
}
#region Members
/// <summary>
/// Formats object members to a list.
///
/// Inline == false:
/// <code>
/// { A=true, B=false, C=new int[3] { 1, 2, 3 } }
/// </code>
///
/// Inline == true:
/// <code>
/// {
/// A: true,
/// B: false,
/// C: new int[3] { 1, 2, 3 }
/// }
/// </code>
/// </summary>
private void FormatObjectMembers(Builder result, object obj, Type originalType, bool includeNonPublic, bool inline)
{
int lengthLimit = result.Remaining;
if (lengthLimit < 0)
{
return;
}
var members = new List<FormattedMember>();
// Limits the number of members added into the result. Some more members may be added than it will fit into the result
// and will be thrown away later but not many more.
FormatObjectMembersRecursive(members, obj, includeNonPublic, ref lengthLimit);
bool useCollectionFormat = UseCollectionFormat(members, originalType);
result.AppendGroupOpening();
for (int i = 0; i < members.Count; i++)
{
result.AppendCollectionItemSeparator(isFirst: i == 0, inline: inline);
if (useCollectionFormat)
{
members[i].AppendAsCollectionEntry(result);
}
else
{
members[i].Append(result, inline ? "=" : ": ");
}
if (result.LimitReached)
{
break;
}
}
result.AppendGroupClosing(inline);
}
private static bool UseCollectionFormat(IEnumerable<FormattedMember> members, Type originalType)
{
return typeof(IEnumerable).IsAssignableFrom(originalType) && members.All(member => member.Index >= 0);
}
private struct FormattedMember
{
// Non-negative if the member is an inlined element of an array (DebuggerBrowsableState.RootHidden applied on a member of array type).
public readonly int Index;
// Formatted name of the member or null if it doesn't have a name (Index is >=0 then).
public readonly string Name;
// Formatted value of the member.
public readonly string Value;
public FormattedMember(int index, string name, string value)
{
Name = name;
Index = index;
Value = value;
}
public int MinimalLength
{
get { return (Name != null ? Name.Length : "[0]".Length) + Value.Length; }
}
public string GetDisplayName()
{
return Name ?? "[" + Index.ToString() + "]";
}
public bool HasKeyName()
{
return Index >= 0 && Name != null && Name.Length >= 2 && Name[0] == '[' && Name[Name.Length - 1] == ']';
}
public bool AppendAsCollectionEntry(Builder result)
{
// Some BCL collections use [{key.ToString()}]: {value.ToString()} pattern to display collection entries.
// We want them to be printed initializer-style, i.e. { <key>, <value> }
if (HasKeyName())
{
result.AppendGroupOpening();
result.AppendCollectionItemSeparator(isFirst: true, inline: true);
result.Append(Name, 1, Name.Length - 2);
result.AppendCollectionItemSeparator(isFirst: false, inline: true);
result.Append(Value);
result.AppendGroupClosing(inline: true);
}
else
{
result.Append(Value);
}
return true;
}
public bool Append(Builder result, string separator)
{
result.Append(GetDisplayName());
result.Append(separator);
result.Append(Value);
return true;
}
}
/// <summary>
/// Enumerates sorted object members to display.
/// </summary>
private void FormatObjectMembersRecursive(List<FormattedMember> result, object obj, bool includeNonPublic, ref int lengthLimit)
{
Debug.Assert(obj != null);
var type = obj.GetType();
var fields = type.GetFields(Ref.BindingFlags.Instance | Ref.BindingFlags.Public | Ref.BindingFlags.NonPublic);
var properties = type.GetProperties(Ref.BindingFlags.Instance | Ref.BindingFlags.Public | Ref.BindingFlags.NonPublic);
var members = new List<Ref.MemberInfo>(fields.Length + properties.Length);
members.AddRange(fields);
members.AddRange(properties);
// kirillo: need case-sensitive comparison here so that the order of members is
// always well-defined (members can differ by case only). And we don't want to
// depend on that order. TODO (tomat): sort by visibility.
members.Sort(new Comparison<Ref.MemberInfo>((x, y) =>
{
int comparisonResult = StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name);
if (comparisonResult == 0)
{
comparisonResult = StringComparer.Ordinal.Compare(x.Name, y.Name);
}
return comparisonResult;
}));
foreach (var member in members)
{
if (_language.IsHiddenMember(member))
{
continue;
}
bool rootHidden = false, ignoreVisibility = false;
var browsable = (DebuggerBrowsableAttribute)member.GetCustomAttributes(typeof(DebuggerBrowsableAttribute), false).FirstOrDefault();
if (browsable != null)
{
if (browsable.State == DebuggerBrowsableState.Never)
{
continue;
}
ignoreVisibility = true;
rootHidden = browsable.State == DebuggerBrowsableState.RootHidden;
}
Ref.FieldInfo field = member as Ref.FieldInfo;
if (field != null)
{
if (!(includeNonPublic || ignoreVisibility || field.IsPublic || field.IsFamily || field.IsFamilyOrAssembly))
{
continue;
}
}
else
{
Ref.PropertyInfo property = (Ref.PropertyInfo)member;
var getter = property.GetGetMethod(nonPublic: true);
var setter = property.GetSetMethod(nonPublic: true);
if (!(includeNonPublic || ignoreVisibility ||
getter != null && (getter.IsPublic || getter.IsFamily || getter.IsFamilyOrAssembly) ||
setter != null && (setter.IsPublic || setter.IsFamily || setter.IsFamilyOrAssembly)))
{
continue;
}
if (getter.GetParameters().Length > 0)
{
continue;
}
}
var debuggerDisplay = GetApplicableDebuggerDisplayAttribute(member);
if (debuggerDisplay != null)
{
string k = FormatWithEmbeddedExpressions(lengthLimit, debuggerDisplay.Name, obj) ?? _language.FormatMemberName(member);
string v = FormatWithEmbeddedExpressions(lengthLimit, debuggerDisplay.Value, obj) ?? String.Empty; // TODO: ?
if (!AddMember(result, new FormattedMember(-1, k, v), ref lengthLimit))
{
return;
}
continue;
}
Exception exception;
object value = GetMemberValue(member, obj, out exception);
if (exception != null)
{
var memberValueBuilder = MakeMemberBuilder(lengthLimit);
FormatException(memberValueBuilder, exception);
if (!AddMember(result, new FormattedMember(-1, _language.FormatMemberName(member), memberValueBuilder.ToString()), ref lengthLimit))
{
return;
}
continue;
}
if (rootHidden)
{
if (value != null && !VisitedObjects.Contains(value))
{
Array array;
if ((array = value as Array) != null) // TODO (tomat): n-dim arrays
{
int i = 0;
foreach (object item in array)
{
string name;
Builder valueBuilder = MakeMemberBuilder(lengthLimit);
FormatObjectRecursive(valueBuilder, item, _options.QuoteStrings, MemberDisplayFormat.InlineValue, out name);
if (!String.IsNullOrEmpty(name))
{
name = FormatWithEmbeddedExpressions(MakeMemberBuilder(lengthLimit), name, item).ToString();
}
if (!AddMember(result, new FormattedMember(i, name, valueBuilder.ToString()), ref lengthLimit))
{
return;
}
i++;
}
}
else if (_language.FormatPrimitive(value, _options.QuoteStrings, _options.IncludeCodePoints, _options.UseHexadecimalNumbers) == null && VisitedObjects.Add(value))
{
FormatObjectMembersRecursive(result, value, includeNonPublic, ref lengthLimit);
VisitedObjects.Remove(value);
}
}
}
else
{
string name;
Builder valueBuilder = MakeMemberBuilder(lengthLimit);
FormatObjectRecursive(valueBuilder, value, _options.QuoteStrings, MemberDisplayFormat.InlineValue, out name);
if (String.IsNullOrEmpty(name))
{
name = _language.FormatMemberName(member);
}
else
{
name = FormatWithEmbeddedExpressions(MakeMemberBuilder(lengthLimit), name, value).ToString();
}
if (!AddMember(result, new FormattedMember(-1, name, valueBuilder.ToString()), ref lengthLimit))
{
return;
}
}
}
}
private bool AddMember(List<FormattedMember> members, FormattedMember member, ref int remainingLength)
{
// Add this item even if we exceed the limit - its prefix might be appended to the result.
members.Add(member);
// We don't need to calculate an exact length, just a lower bound on the size.
// We can add more members to the result than it will eventually fit, we shouldn't add less.
// Add 2 more, even if only one or half of it fit, so that the separator is included in edge cases.
if (remainingLength == Int32.MinValue)
{
return false;
}
remainingLength -= member.MinimalLength;
if (remainingLength <= 0)
{
remainingLength = Int32.MinValue;
}
return true;
}
private void FormatException(Builder result, Exception exception)
{
result.Append("!<");
result.Append(_language.FormatTypeName(exception.GetType(), _options));
result.Append('>');
}
#endregion
#region Collections
private void FormatKeyValuePair(Builder result, object obj)
{
Type type = obj.GetType();
object key = type.GetProperty("Key").GetValue(obj, SpecializedCollections.EmptyObjects);
object value = type.GetProperty("Value").GetValue(obj, SpecializedCollections.EmptyObjects);
string _;
result.AppendGroupOpening();
result.AppendCollectionItemSeparator(isFirst: true, inline: true);
FormatObjectRecursive(result, key, quoteStrings: true, memberFormat: MemberDisplayFormat.InlineValue, name: out _);
result.AppendCollectionItemSeparator(isFirst: false, inline: true);
FormatObjectRecursive(result, value, quoteStrings: true, memberFormat: MemberDisplayFormat.InlineValue, name: out _);
result.AppendGroupClosing(inline: true);
}
private void FormatCollectionHeader(Builder result, ICollection collection)
{
Array array = collection as Array;
if (array != null)
{
result.Append(_language.FormatArrayTypeName(array, _options));
return;
}
result.Append(_language.FormatTypeName(collection.GetType(), _options));
try
{
result.Append('(');
result.Append(collection.Count.ToString());
result.Append(')');
}
catch (Exception)
{
// skip
}
}
private void FormatArray(Builder result, Array array, bool inline)
{
FormatCollectionHeader(result, array);
if (array.Rank > 1)
{
FormatMultidimensionalArray(result, array, inline);
}
else
{
result.Append(' ');
FormatSequence(result, (IEnumerable)array, inline);
}
}
private void FormatDictionary(Builder result, IDictionary dict, bool inline)
{
result.AppendGroupOpening();
int i = 0;
try
{
IDictionaryEnumerator enumerator = dict.GetEnumerator();
IDisposable disposable = enumerator as IDisposable;
try
{
while (enumerator.MoveNext())
{
var entry = enumerator.Entry;
string _;
result.AppendCollectionItemSeparator(isFirst: i == 0, inline: inline);
result.AppendGroupOpening();
result.AppendCollectionItemSeparator(isFirst: true, inline: true);
FormatObjectRecursive(result, entry.Key, quoteStrings: true, memberFormat: MemberDisplayFormat.InlineValue, name: out _);
result.AppendCollectionItemSeparator(isFirst: false, inline: true);
FormatObjectRecursive(result, entry.Value, quoteStrings: true, memberFormat: MemberDisplayFormat.InlineValue, name: out _);
result.AppendGroupClosing(inline: true);
i++;
}
}
finally
{
if (disposable != null)
{
disposable.Dispose();
}
}
}
catch (Exception e)
{
result.AppendCollectionItemSeparator(isFirst: i == 0, inline: inline);
FormatException(result, e);
result.Append(' ');
result.Append(_options.Ellipsis);
}
result.AppendGroupClosing(inline);
}
private void FormatSequence(Builder result, IEnumerable sequence, bool inline)
{
result.AppendGroupOpening();
int i = 0;
try
{
foreach (var item in sequence)
{
string name;
result.AppendCollectionItemSeparator(isFirst: i == 0, inline: inline);
FormatObjectRecursive(result, item, quoteStrings: true, memberFormat: MemberDisplayFormat.InlineValue, name: out name);
i++;
}
}
catch (Exception e)
{
result.AppendCollectionItemSeparator(isFirst: i == 0, inline: inline);
FormatException(result, e);
result.Append(" ...");
}
result.AppendGroupClosing(inline);
}
private void FormatMultidimensionalArray(Builder result, Array array, bool inline)
{
Debug.Assert(array.Rank > 1);
if (array.Length == 0)
{
result.AppendCollectionItemSeparator(isFirst: true, inline: true);
result.AppendGroupOpening();
result.AppendGroupClosing(inline: true);
return;
}
int[] indices = new int[array.Rank];
for (int i = array.Rank - 1; i >= 0; i--)
{
indices[i] = array.GetLowerBound(i);
}
int nesting = 0;
int flatIndex = 0;
while (true)
{
// increment indices (lower index overflows to higher):
int i = indices.Length - 1;
while (indices[i] > array.GetUpperBound(i))
{
indices[i] = array.GetLowerBound(i);
result.AppendGroupClosing(inline: inline || nesting != 1);
nesting--;
i--;
if (i < 0)
{
return;
}
indices[i]++;
}
result.AppendCollectionItemSeparator(isFirst: flatIndex == 0, inline: inline || nesting != 1);
i = indices.Length - 1;
while (i >= 0 && indices[i] == array.GetLowerBound(i))
{
result.AppendGroupOpening();
nesting++;
// array isn't empty, so there is always an element following this separator
result.AppendCollectionItemSeparator(isFirst: true, inline: inline || nesting != 1);
i--;
}
string name;
FormatObjectRecursive(result, array.GetValue(indices), quoteStrings: true, memberFormat: MemberDisplayFormat.InlineValue, name: out name);
indices[indices.Length - 1]++;
flatIndex++;
}
}
#endregion
#region Scalars
private void ObjectToString(Builder result, object obj)
{
try
{
string str = obj.ToString();
result.Append('[');
result.Append(str);
result.Append(']');
}
catch (Exception e)
{
FormatException(result, e);
}
}
#endregion
#region DebuggerDisplay Embedded Expressions
/// <summary>
/// Evaluate a format string with possible member references enclosed in braces.
/// E.g. "foo = {GetFooString(),nq}, bar = {Bar}".
/// </summary>
/// <remarks>
/// Although in theory any expression is allowed to be embedded in the string such behavior is in practice fundamentally broken.
/// The attribute doesn't specify what language (VB, C#, F#, etc.) to use to parse these expressions. Even if it did all languages
/// would need to be able to evaluate each other language's expressions, which is not viable and the Expression Evaluator doesn't
/// work that way today. Instead it evaluates the embedded expressions in the language of the current method frame. When consuming
/// VB objects from C#, for example, the evaluation migth fail due to language mismatch (evaluating VB expression using C# parser).
///
/// Therefore we limit the expressions to a simple language independent syntax: {clr-member-name} '(' ')' ',nq',
/// where parentheses and ,nq suffix (no-quotes) are optional and the name is an arbitrary CLR field, property, or method name.
/// We then resolve the member by name using case-sensitive lookup first with fallback to case insensitive and evaluate it.
/// If parentheses are present we only look for methods.
/// Only parameter less members are considered.
/// </remarks>
private string FormatWithEmbeddedExpressions(int lengthLimit, string format, object obj)
{
if (String.IsNullOrEmpty(format))
{
return null;
}
return FormatWithEmbeddedExpressions(new Builder(lengthLimit, _options, insertEllipsis: false), format, obj).ToString();
}
private Builder FormatWithEmbeddedExpressions(Builder result, string format, object obj)
{
int i = 0;
while (i < format.Length)
{
char c = format[i++];
if (c == '{')
{
if (i >= 2 && format[i - 2] == '\\')
{
result.Append('{');
}
else
{
int expressionEnd = format.IndexOf('}', i);
bool noQuotes, callableOnly;
string memberName;
if (expressionEnd == -1 || (memberName = ParseSimpleMemberName(format, i, expressionEnd, out noQuotes, out callableOnly)) == null)
{
// the expression isn't properly formatted
result.Append(format, i - 1, format.Length - i + 1);
break;
}
Ref.MemberInfo member = ResolveMember(obj, memberName, callableOnly);
if (member == null)
{
result.AppendFormat(callableOnly ? "!<Method '{0}' not found>" : "!<Member '{0}' not found>", memberName);
}
else
{
Exception exception;
object value = GetMemberValue(member, obj, out exception);
if (exception != null)
{
FormatException(result, exception);
}
else
{
string name;
FormatObjectRecursive(result, value, !noQuotes, MemberDisplayFormat.NoMembers, out name);
}
}
i = expressionEnd + 1;
}
}
else
{
result.Append(c);
}
}
return result;
}
// Parses
// <clr-member-name>
// <clr-member-name> ',' 'nq'
// <clr-member-name> '(' ')'
// <clr-member-name> '(' ')' ',' 'nq'
//
// Internal for testing purposes.
internal static string ParseSimpleMemberName(string str, int start, int end, out bool noQuotes, out bool isCallable)
{
Debug.Assert(str != null && start >= 0 && end >= start);
isCallable = false;
noQuotes = false;
// no-quotes suffix:
if (end - 3 >= start && str[end - 2] == 'n' && str[end - 1] == 'q')
{
int j = end - 3;
while (j >= start && Char.IsWhiteSpace(str[j]))
{
j--;
}
if (j >= start && str[j] == ',')
{
noQuotes = true;
end = j;
}
}
int i = end - 1;
EatTrailingWhiteSpace(str, start, ref i);
if (i > start && str[i] == ')')
{
int closingParen = i;
i--;
EatTrailingWhiteSpace(str, start, ref i);
if (str[i] != '(')
{
i = closingParen;
}
else
{
i--;
EatTrailingWhiteSpace(str, start, ref i);
isCallable = true;
}
}
EatLeadingWhiteSpace(str, ref start, i);
return str.Substring(start, i - start + 1);
}
private static void EatTrailingWhiteSpace(string str, int start, ref int i)
{
while (i >= start && Char.IsWhiteSpace(str[i]))
{
i--;
}
}
private static void EatLeadingWhiteSpace(string str, ref int i, int end)
{
while (i < end && Char.IsWhiteSpace(str[i]))
{
i++;
}
}
#endregion
}
}
}