5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Marco Ridoni (marco.ridoni@virgilio.it)
7 // Dennis Hayes (dennish@raytek.com)
9 // (C) 2002 Chris J Breisch
10 // (C) 2003 Marco Ridoni
13 * Copyright (c) 2002-2003 Mainsoft Corporation.
14 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 * DEALINGS IN THE SOFTWARE.
39 using System
.Reflection
;
40 using Microsoft
.VisualBasic
;
41 using System
.ComponentModel
;
44 namespace Microsoft
.VisualBasic
.CompilerServices
{
45 [StandardModule
, EditorBrowsable(EditorBrowsableState
.Never
)]
46 sealed public class LateBinding
{
47 private LateBinding () {}
49 [System
.Diagnostics
.DebuggerHiddenAttribute
]
50 [System
.Diagnostics
.DebuggerStepThroughAttribute
]
51 public static object LateGet(
59 BindingFlags invokeAttr
= 0;
61 if (objType
== null) {
63 throw new NullReferenceException();
64 objType
= o
.GetType();
66 Type
[] typeArr
= null;
68 typeArr
= new Type
[args
.Length
];
69 for (int i
= 0; i
< typeArr
.Length
; i
++) {
70 typeArr
[i
] = args
[i
].GetType();
74 MemberInfo
[] memberInfo
= objType
.GetMember(name
);
76 if (((memberInfo
== null) || (memberInfo
.Length
== 0)))
77 throw new NullReferenceException();
79 if (memberInfo
[0] is MethodInfo
)
80 invokeAttr
= BindingFlags
.InvokeMethod
;
81 else if (memberInfo
[0] is PropertyInfo
)
82 invokeAttr
= BindingFlags
.GetProperty
;
83 else if (memberInfo
[0] is FieldInfo
)
84 invokeAttr
= BindingFlags
.GetField
;
86 throw new NullReferenceException();
88 return objType
.InvokeMember(name
, invokeAttr
, null, o
, args
);
91 [System
.Diagnostics
.DebuggerStepThroughAttribute
]
92 [System
.Diagnostics
.DebuggerHiddenAttribute
]
93 public static void LateSetComplex(
102 LateSet(o
, objType
, name
, args
, paramnames
);
105 [System
.Diagnostics
.DebuggerStepThroughAttribute
]
106 [System
.Diagnostics
.DebuggerHiddenAttribute
]
107 public static void LateSet(
112 string[] paramnames
) {
114 BindingFlags invokeAttr
;
116 if (objType
== null) {
118 throw new NullReferenceException();
119 objType
= o
.GetType();
121 Type
[] typeArr
= null;
123 typeArr
= new Type
[args
.Length
];
124 for (int i
= 0; i
< typeArr
.Length
; i
++) {
125 typeArr
[i
] = args
[i
].GetType();
129 MemberInfo
[] memberInfo
= objType
.GetMember(name
);
131 if (((memberInfo
== null) || (memberInfo
.Length
== 0))) {
132 throw new NullReferenceException();
135 if (memberInfo
[0] is PropertyInfo
)
136 invokeAttr
= BindingFlags
.SetProperty
;
137 else if (memberInfo
[0] is FieldInfo
)
138 invokeAttr
= BindingFlags
.SetField
;
140 throw new NullReferenceException();
142 objType
.InvokeMember(name
, invokeAttr
, null, o
, args
);
145 // [System.Diagnostics.DebuggerStepThroughAttribute]
146 // [System.Diagnostics.DebuggerHiddenAttribute]
147 // public static System.Object LateIndexGet (System.Object o, System.Object[] args, System.String[] paramnames)
150 // Object binderState = null;
152 // if (o == null || args == null)
153 // throw new ArgumentException();
155 // objType = o.GetType();
156 // if (objType.IsArray) {
157 // Array a = (Array) o;
158 // int[] idxs = new int[args.Length];
159 // Array.Copy (args, idxs, args.Length);
161 // return a.GetValue(idxs);
165 // MemberInfo[] defaultMembers = objType.GetDefaultMembers();
166 // if (defaultMembers == null)
167 // throw new Exception(); // FIXME: Which exception should we throw?
169 // // We try to find a default method/property/field we can invoke/use
170 // VBBinder MyBinder = new VBBinder();
171 // BindingFlags bindingFlags = BindingFlags.IgnoreCase |
172 // BindingFlags.Instance |
173 // BindingFlags.Static |
174 // BindingFlags.Public |
175 // BindingFlags.GetProperty |
176 // BindingFlags.GetField |
177 // BindingFlags.InvokeMethod;
179 // MethodBase[] mb = new MethodBase[defaultMembers.Length];
181 // for (int x = 0; x < defaultMembers.Length; x++)
182 // if (defaultMembers[x].MemberType == MemberTypes.Property)
183 // mb[x] = ((PropertyInfo) defaultMembers[x]).GetGetMethod();
185 // mb[x] = (MethodBase) defaultMembers[x];
186 // } catch (Exception e) { }
188 // MethodBase TheMethod = MyBinder.BindToMethod (bindingFlags,
195 // if (TheMethod == null)
196 // throw new TargetInvocationException(new ArgumentNullException());
198 // return TheMethod.Invoke (o, args);
202 [System
.Diagnostics
.DebuggerStepThroughAttribute
]
203 [System
.Diagnostics
.DebuggerHiddenAttribute
]
204 public static object LateIndexGet(
207 string[] paramnames
) {
209 throw new NullReferenceException();
211 throw new NullReferenceException();
212 Type type
= o
.GetType();
213 //late binding for array
217 throw new NotImplementedException("LateBinding not implmented");
218 //int rank = ArrayStaticWrapper.get_Rank(o);
219 //if (rank != args.Length)
220 // throw new RankException();
221 //int[] indices = new int[args.Length];
222 //for (int i = 0; i < indices.Length; i++)
223 // indices[i] = IntegerType.FromObject(args[i]);
224 //return ArrayStaticWrapper.GetValue(o, indices);
226 //late binding for default property
227 Type
[] types
= new Type
[args
.Length
];
228 for (int i
= 0; i
< types
.Length
; i
++) {
229 types
[i
] = args
[i
].GetType();
232 //string defaultPropName;
233 throw new NotImplementedException("LateBinding not implmented");
234 //if (type is TypeInfo)
235 // defaultPropName = getDefaultMemberName(type);
236 //else if (type == Type.StringType ||
237 // type == Type.GetType("System.Text.StringBuilder"))
238 // defaultPropName = "Chars";
240 // defaultPropName = "Item";
241 //PropertyInfo propertyInfo = null;
242 //if (defaultPropName != null)
243 // propertyInfo = type.GetProperty(defaultPropName, types);
244 //if (propertyInfo != null) {
245 // return propertyInfo.GetValue(o, args);
248 // throw new NotSupportedException();
251 private static string getDefaultMemberName(Type type
) {
252 string defaultName
= null;
253 while (type
!= null) {
255 throw new NotImplementedException("LateBinding not implmented");
257 // type.GetCustomAttributes(
258 // Type.GetType("System.Reflection.DefaultMemberAttribute"),
260 //if (locals != null && locals.Length != 0) {
262 // ((DefaultMemberAttribute) locals[0]).get_MemberName();
265 //type = type.get_BaseType();
269 // mono implmentation
270 // [System.Diagnostics.DebuggerStepThroughAttribute]
271 // [System.Diagnostics.DebuggerHiddenAttribute]
272 // public static void LateIndexSet (System.Object o, System.Object[] args, System.String[] paramnames)
275 // Object binderState = null;
278 // if (o == null || args == null)
279 // throw new ArgumentException();
281 // myValue = args[args.Length - 1];
282 // objType = o.GetType();
283 // if (objType.IsArray) {
284 // Array a = (Array) o;
285 // int[] idxs = new int[args.Length - 1];
286 // Array.Copy (args, idxs, args.Length -1);
287 // a.SetValue(myValue, idxs);
291 // MemberInfo[] defaultMembers = objType.GetDefaultMembers();
292 // if (defaultMembers == null)
293 // throw new Exception(); // FIXME: Which exception should we throw?
295 // // We try to find a default method/property/field we can invoke/use
296 // VBBinder MyBinder = new VBBinder();
297 // BindingFlags bindingFlags = BindingFlags.IgnoreCase |
298 // BindingFlags.Instance |
299 // BindingFlags.Static |
300 // BindingFlags.Public |
301 // BindingFlags.GetProperty |
302 // BindingFlags.GetField |
303 // BindingFlags.InvokeMethod;
305 // MethodBase[] mb = new MethodBase[defaultMembers.Length];
307 // for (int x = 0; x < defaultMembers.Length; x++)
308 // if (defaultMembers[x].MemberType == MemberTypes.Property)
309 // mb[x] = ((PropertyInfo) defaultMembers[x]).GetSetMethod();
311 // mb[x] = (MethodBase) defaultMembers[x];
312 // } catch (Exception e) { }
314 // MethodBase TheMethod = MyBinder.BindToMethod (bindingFlags,
321 // if (TheMethod == null)
322 // throw new TargetInvocationException(new ArgumentNullException());
324 // TheMethod.Invoke (o, args);
330 [System
.Diagnostics
.DebuggerHiddenAttribute
]
331 [System
.Diagnostics
.DebuggerStepThroughAttribute
]
332 public static void LateIndexSet(
335 string[] paramnames
) {
337 throw new NullReferenceException();
339 throw new NullReferenceException();
340 Type type
= o
.GetType();
341 //late binding for array
344 throw new NotImplementedException("LateBinding not implmented");
345 //int rank = ArrayStaticWrapper.get_Rank(o);
346 //if (rank != (args.Length - 1))
347 // throw new RankException();
348 //int[] indices = new int[args.Length - 1];
349 //for (int i = 0; i < (indices.Length - 1); i++)
350 // indices[i] = IntegerType.FromObject(args[i]);
351 //ArrayStaticWrapper.SetValue(o, args[args.Length - 1], indices);
354 //late binding for default property
355 Type
[] types
= new Type
[args
.Length
- 1];
356 for (int i
= 0; i
< types
.Length
; i
++) {
358 throw new NotImplementedException("LateBinding not implmented");
359 //types[i] = ObjectStaticWrapper.GetType(args[i]);
360 //System.out.println("in Set:" + types[i].get_FullName());
362 //string defaultPropName;
364 throw new NotImplementedException("LateBinding not implmented");
365 //if (type is TypeInfo)
366 // defaultPropName = getDefaultMemberName(type);
367 //else if (type == Type.StringType ||
368 // type == Type.GetType("System.Text.StringBuilder"))
369 // defaultPropName = "Chars";
371 // defaultPropName = "Item";
372 //PropertyInfo propertyInfo = null;
373 //if (defaultPropName != null)
374 // propertyInfo = type.GetProperty(defaultPropName, types);
375 //if (propertyInfo != null) {
376 // object newVal = args[args.Length - 1];
377 // object[] Params = new object[args.Length - 1];
379 // Array.Copy(args, 0, Params, 0, args.Length - 1);
380 // // java System.arraycopy(args, 0, Params, 0, args.Length - 1);
381 // propertyInfo.SetValue(o, newVal, Params);
384 // throw new NotSupportedException();
387 [System
.Diagnostics
.DebuggerHiddenAttribute
]
388 [System
.Diagnostics
.DebuggerStepThroughAttribute
]
389 public static void LateIndexSetComplex(
395 LateIndexSet(o
, args
, paramnames
);
398 [System
.Diagnostics
.DebuggerStepThroughAttribute
]
399 [System
.Diagnostics
.DebuggerHiddenAttribute
]
400 public static void LateCall(
407 if (objType
== null) {
409 throw new NullReferenceException();
410 objType
= o
.GetType();
412 Type
[] typeArr
= null;
414 typeArr
= new Type
[args
.Length
];
415 for (int i
= 0; i
< typeArr
.Length
; i
++) {
416 typeArr
[i
] = args
[i
].GetType();
419 MethodInfo methodInfo
= objType
.GetMethod(name
, typeArr
);
420 if (methodInfo
== null)
421 throw new NullReferenceException();
422 methodInfo
.Invoke(o
, args
);