summaryrefslogtreecommitdiff
path: root/csharp/src/Ice/ValueWriter.cs
blob: ead5bafcd40aee034a5b5642240482a8518555b5 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

namespace IceInternal
{

    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Reflection;
    using IceUtilInternal;

    public sealed class ValueWriter
    {
        public static void write(object obj, OutputBase output)
        {
            writeValue(null, obj, null, output);
        }
        
        private static void writeValue(string name, object val, Dictionary<Ice.Object, object> objectTable, OutputBase output)
        {
            if(val == null)
            {
                writeName(name, output);
                output.print("(null)");
            }
            else
            {
                System.Type c = val.GetType();
                if(c.Equals(typeof(byte)) || c.Equals(typeof(short)) || c.Equals(typeof(int)) ||
                   c.Equals(typeof(long)) || c.Equals(typeof(double)) || c.Equals(typeof(float)) ||
                   c.Equals(typeof(bool)))
                {
                    writeName(name, output);
                    output.print(val.ToString());
                }
                else if(c.Equals(typeof(string)))
                {
                    writeName(name, output);
                    output.print("\"");
                    output.print(val.ToString());
                    output.print("\"");
                }
                else if(val is IList)
                {
                    int n = 0;
                    IEnumerator i = ((IList)val).GetEnumerator();
                    while(i.MoveNext())
                    {
                        string elem = (name != null ? name : "");
                        elem += "[" + n++ + "]";
                        writeValue(elem, i.Current, objectTable, output);
                    }
                }
                else if(val is IDictionary)
                {
                    foreach(DictionaryEntry entry in (IDictionary)val)
                    {
                        string elem = name != null ? name + "." : "";
                        writeValue(elem + "key", entry.Key, objectTable, output);
                        writeValue(elem + "value", entry.Value, objectTable, output);
                    }
                }
                else if(val is Ice.ObjectPrxHelperBase)
                {
                    writeName(name, output);
                    Ice.ObjectPrxHelperBase proxy = (Ice.ObjectPrxHelperBase)val;
                    output.print(proxy.reference__().ToString());
                }
                else if(val is Ice.Object)
                {
                    //
                    // Check for recursion.
                    //
                    if(objectTable != null && objectTable.ContainsKey((Ice.Object)val))
                    {
                        writeName(name, output);
                        output.print("(recursive)");
                    }
                    else
                    {
                        if(objectTable == null)
                        {
                            objectTable = new Dictionary<Ice.Object, object>();
                        }
                        objectTable[(Ice.Object)val] = null;
                        writeFields(name, val, c, objectTable, output);
                    }
                }
                else if(c.IsEnum)
                {
                    writeName(name, output);
                    output.print(val.ToString());                
                }
                else
                {           
                    //
                    // Must be struct.
                    //
                    writeFields(name, val, c, objectTable, output);
                }
            }
        }
        
        private static void writeFields(string name, object obj, System.Type c, Dictionary<Ice.Object, object> objectTable,
                                        OutputBase output)
        {
            if(!c.Equals(typeof(object)))
            {
                //
                // Write the superclass first.
                //
                writeFields(name, obj, c.BaseType, objectTable, output);
                
                //
                // Write the declared fields of the given class.
                //
                FieldInfo[] fields = 
                    c.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);

                for(int i = 0; i < fields.Length; i++)
                {
                    string fieldName = (name != null ? name + '.' + fields[i].Name : fields[i].Name);
                    
                    try
                    {
                        object val = fields[i].GetValue(obj);
                        writeValue(fieldName, val, objectTable, output);
                    }
                    catch(System.UnauthorizedAccessException)
                    {
                        Debug.Assert(false);
                    }
                }
            }
        }
        
        private static void writeName(string name, OutputBase output)
        {
            if(name != null)
            {
                output.nl();
                output.print(name + " = ");
            }
        }
    }

}