(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / LateBinding.cs
blob4fa5b804e686772a1827687eb9fe716b86268d69
1 //
2 // LateBinding.cs
3 //
4 // Author:
5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Marco Ridoni (marco.ridoni@virgilio.it)
7 // Dennis Hayes (dennish@raytek.com)
8 //
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.
34 /**
38 using System;
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(
52 object o,
53 Type objType,
54 string name,
55 object[] args,
56 string[] paramnames,
57 bool[] CopyBack) {
59 BindingFlags invokeAttr = 0;
61 if (objType == null) {
62 if (o == null)
63 throw new NullReferenceException();
64 objType = o.GetType();
66 Type[] typeArr = null;
67 if (args != 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;
85 else
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(
94 object o,
95 Type objType,
96 string name,
97 object[] args,
98 string[] paramnames,
99 bool OptimisticSet,
100 bool RValueBase)
102 LateSet(o, objType, name, args, paramnames);
105 [System.Diagnostics.DebuggerStepThroughAttribute]
106 [System.Diagnostics.DebuggerHiddenAttribute]
107 public static void LateSet(
108 object o,
109 Type objType,
110 string name,
111 object[] args,
112 string[] paramnames) {
114 BindingFlags invokeAttr;
116 if (objType == null) {
117 if (o == null)
118 throw new NullReferenceException();
119 objType = o.GetType();
121 Type[] typeArr = null;
122 if (args != 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;
139 else
140 throw new NullReferenceException();
142 objType.InvokeMember(name, invokeAttr, null, o, args);
144 //mono implmentation
145 // [System.Diagnostics.DebuggerStepThroughAttribute]
146 // [System.Diagnostics.DebuggerHiddenAttribute]
147 // public static System.Object LateIndexGet (System.Object o, System.Object[] args, System.String[] paramnames)
148 // {
149 // Type objType;
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);
162 // }
163 // else
164 // {
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];
180 // try {
181 // for (int x = 0; x < defaultMembers.Length; x++)
182 // if (defaultMembers[x].MemberType == MemberTypes.Property)
183 // mb[x] = ((PropertyInfo) defaultMembers[x]).GetGetMethod();
184 // else
185 // mb[x] = (MethodBase) defaultMembers[x];
186 // } catch (Exception e) { }
188 // MethodBase TheMethod = MyBinder.BindToMethod (bindingFlags,
189 // mb,
190 // ref args,
191 // null,
192 // null,
193 // paramnames,
194 // out binderState);
195 // if (TheMethod == null)
196 // throw new TargetInvocationException(new ArgumentNullException());
198 // return TheMethod.Invoke (o, args);
199 // }
200 // }
202 [System.Diagnostics.DebuggerStepThroughAttribute]
203 [System.Diagnostics.DebuggerHiddenAttribute]
204 public static object LateIndexGet(
205 object o,
206 object[] args,
207 string[] paramnames) {
208 if (o == null)
209 throw new NullReferenceException();
210 if (args == null)
211 throw new NullReferenceException();
212 Type type = o.GetType();
213 //late binding for array
215 if (type.IsArray) {
216 // TODO:
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();
231 // TODO:
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";
239 //else
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);
247 //else
248 // throw new NotSupportedException();
251 private static string getDefaultMemberName(Type type) {
252 string defaultName = null;
253 while (type != null) {
254 // TODO:
255 throw new NotImplementedException("LateBinding not implmented");
256 //object[] locals =
257 // type.GetCustomAttributes(
258 // Type.GetType("System.Reflection.DefaultMemberAttribute"),
259 // false);
260 //if (locals != null && locals.Length != 0) {
261 // defaultName =
262 // ((DefaultMemberAttribute) locals[0]).get_MemberName();
263 // break;
265 //type = type.get_BaseType();
267 return defaultName;
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)
273 // {
274 // Type objType;
275 // Object binderState = null;
276 // Object myValue;
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);
288 // }
289 // else
290 // {
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];
306 // try {
307 // for (int x = 0; x < defaultMembers.Length; x++)
308 // if (defaultMembers[x].MemberType == MemberTypes.Property)
309 // mb[x] = ((PropertyInfo) defaultMembers[x]).GetSetMethod();
310 // else
311 // mb[x] = (MethodBase) defaultMembers[x];
312 // } catch (Exception e) { }
314 // MethodBase TheMethod = MyBinder.BindToMethod (bindingFlags,
315 // mb,
316 // ref args,
317 // null,
318 // null,
319 // paramnames,
320 // out binderState);
321 // if (TheMethod == null)
322 // throw new TargetInvocationException(new ArgumentNullException());
324 // TheMethod.Invoke (o, args);
325 // }
326 // }
330 [System.Diagnostics.DebuggerHiddenAttribute]
331 [System.Diagnostics.DebuggerStepThroughAttribute]
332 public static void LateIndexSet(
333 object o,
334 object[] args,
335 string[] paramnames) {
336 if (o == null)
337 throw new NullReferenceException();
338 if (args == null)
339 throw new NullReferenceException();
340 Type type = o.GetType();
341 //late binding for array
342 if (type.IsArray) {
343 // TODO:
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);
352 //return;
354 //late binding for default property
355 Type[] types = new Type[args.Length - 1];
356 for (int i = 0; i < types.Length; i++) {
357 // TODO:
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;
363 // TODO:
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";
370 //else
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);
383 //else
384 // throw new NotSupportedException();
387 [System.Diagnostics.DebuggerHiddenAttribute]
388 [System.Diagnostics.DebuggerStepThroughAttribute]
389 public static void LateIndexSetComplex(
390 object o,
391 object[] args,
392 string[] paramnames,
393 bool OptimisticSet,
394 bool RValueBase) {
395 LateIndexSet(o, args, paramnames);
398 [System.Diagnostics.DebuggerStepThroughAttribute]
399 [System.Diagnostics.DebuggerHiddenAttribute]
400 public static void LateCall(
401 object o,
402 Type objType,
403 string name,
404 object[] args,
405 string[] paramnames,
406 bool[] CopyBack) {
407 if (objType == null) {
408 if (o == null)
409 throw new NullReferenceException();
410 objType = o.GetType();
412 Type[] typeArr = null;
413 if (args != 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);