2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MethodReturnMessageWrapper.cs
1 //
2 // System.Runtime.Remoting.Messaging.MethodReturnMessageWrapper.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //         Lluis Sanchez Gual (lluis@ideary.com)
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.Serialization;
37
38 namespace System.Runtime.Remoting.Messaging {
39
40         [System.Runtime.InteropServices.ComVisible (true)]
41         public class MethodReturnMessageWrapper : InternalMessageWrapper, IMethodReturnMessage, IMethodMessage, IMessage
42         {
43                 object[] _args;
44                 ArgInfo _outArgInfo;
45                 DictionaryWrapper _properties;
46                 Exception _exception;
47                 object _return;
48
49                 public MethodReturnMessageWrapper (IMethodReturnMessage msg)
50                         : base (msg)
51                 {
52                         if (msg.Exception != null) {
53                                 _exception = msg.Exception;
54                                 _args = new object[0];
55                         } else {
56                                 _args = msg.Args;
57                                 _return = msg.ReturnValue;
58                                 if (msg.MethodBase != null)
59                                         _outArgInfo = new ArgInfo (msg.MethodBase, ArgInfoType.Out);
60                         }
61                 }
62
63                 public virtual int ArgCount {
64                         get { return _args.Length; }
65                 }
66
67                 public virtual object [] Args \r
68                 {
69                         get { return _args; }
70                         set { _args = value; }
71                 }
72
73                 public virtual Exception Exception {
74                         get { return _exception; }
75                         set { _exception = value; }
76                 }
77                 
78                 public virtual bool HasVarArgs {
79                         get { return ((IMethodReturnMessage)WrappedMessage).HasVarArgs; }
80                 }
81                 
82                 public virtual LogicalCallContext LogicalCallContext {
83                         get { return ((IMethodReturnMessage)WrappedMessage).LogicalCallContext; }
84                 }
85                 
86                 public virtual MethodBase MethodBase {
87                         get { return ((IMethodReturnMessage)WrappedMessage).MethodBase; }
88                 }
89
90                 public virtual string MethodName {
91                         get { return ((IMethodReturnMessage)WrappedMessage).MethodName; }
92                 }
93
94                 public virtual object MethodSignature {
95                         get { return ((IMethodReturnMessage)WrappedMessage).MethodSignature; }
96                 }
97
98                 public virtual int OutArgCount {
99                         get { return _outArgInfo != null ? _outArgInfo.GetInOutArgCount() : 0; }
100                 }
101
102                 public virtual object[] OutArgs {
103                         get { return _outArgInfo != null ? _outArgInfo.GetInOutArgs (_args) : _args; }
104                 }
105
106                 public virtual IDictionary Properties 
107                 {
108                         get { 
109                                 if (_properties == null) _properties = new DictionaryWrapper(this, WrappedMessage.Properties);
110                                 return _properties; 
111                         }
112                 }
113
114                 public virtual object ReturnValue {
115                         get { return _return; }
116                         set { _return = value; }
117                 }
118
119                 public virtual string TypeName {
120                         get { return ((IMethodReturnMessage)WrappedMessage).TypeName; }
121                 }
122
123                 public string Uri \r
124                 {
125                         get { return ((IMethodReturnMessage)WrappedMessage).Uri; }
126                         set { Properties["__Uri"] = value; }
127                 }
128
129                 public virtual object GetArg (int argNum)
130                 {
131                         return _args[argNum];
132                 }
133
134                 public virtual string GetArgName (int index)
135                 {
136                         return ((IMethodReturnMessage)WrappedMessage).GetArgName(index);
137                 }
138
139                 public virtual object GetOutArg (int argNum)
140                 {
141                         return _args [_outArgInfo.GetInOutArgIndex (argNum)];
142                 }
143
144                 public virtual string GetOutArgName (int index)
145                 {
146                         return _outArgInfo.GetInOutArgName(index);
147                 }
148
149                 class DictionaryWrapper : MethodReturnDictionary\r
150                 {\r
151                         IDictionary _wrappedDictionary;\r
152                         static string[] _keys = new string[] {"__Args", "__Return"};\r
153 \r
154                         public DictionaryWrapper(IMethodReturnMessage message, IDictionary wrappedDictionary) : base (message)\r
155                         {\r
156                                 _wrappedDictionary = wrappedDictionary;\r
157                                 MethodKeys = _keys;\r
158                         }\r
159
160                         protected override IDictionary AllocInternalProperties()\r
161                         {\r
162                                 return _wrappedDictionary;\r
163                         }\r
164
165                         protected override void SetMethodProperty (string key, object value)
166                         {
167                                 if (key == "__Args") ((MethodReturnMessageWrapper)_message)._args = (object[])value;
168                                 else if (key == "__Return") ((MethodReturnMessageWrapper)_message)._return = value;
169                                 else base.SetMethodProperty (key, value);
170                         }
171
172                         protected override object GetMethodProperty (string key)
173                         {
174                                 if (key == "__Args") return ((MethodReturnMessageWrapper)_message)._args;
175                                 else if (key == "__Return") return ((MethodReturnMessageWrapper)_message)._return;
176                                 else return base.GetMethodProperty (key);
177                         }
178                 }
179         }
180 }