public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) { const string bindingflags_arg = "bindingFlags"; if ((invokeAttr & BindingFlags.CreateInstance) != 0) { if ((invokeAttr & (BindingFlags.GetField | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.SetProperty)) != 0) { throw new ArgumentException(bindingflags_arg); } } else if (name == null) { throw new ArgumentNullException("name"); } if ((invokeAttr & BindingFlags.GetField) != 0 && (invokeAttr & BindingFlags.SetField) != 0) { throw new ArgumentException("Cannot specify both Get and Set on a field.", bindingflags_arg); } if ((invokeAttr & BindingFlags.GetProperty) != 0 && (invokeAttr & BindingFlags.SetProperty) != 0) { throw new ArgumentException("Cannot specify both Get and Set on a property.", bindingflags_arg); } if ((invokeAttr & BindingFlags.InvokeMethod) != 0) { if ((invokeAttr & BindingFlags.SetField) != 0) { throw new ArgumentException("Cannot specify Set on a field and Invoke on a method.", bindingflags_arg); } if ((invokeAttr & BindingFlags.SetProperty) != 0) { throw new ArgumentException("Cannot specify Set on a property and Invoke on a method.", bindingflags_arg); } } if ((namedParameters != null) && ((args == null) || args.Length < namedParameters.Length)) { throw new ArgumentException("namedParameters cannot be more than named arguments in number"); } if ((invokeAttr & (BindingFlags.InvokeMethod | BindingFlags.CreateInstance | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty)) == 0) { throw new ArgumentException("Must specify binding flags describing the invoke operation required.", bindingflags_arg); } /* set some defaults if none are provided :-( */ if ((invokeAttr & (BindingFlags.Public | BindingFlags.NonPublic)) == 0) { invokeAttr |= BindingFlags.Public; } if ((invokeAttr & (BindingFlags.Static | BindingFlags.Instance)) == 0) { invokeAttr |= BindingFlags.Static | BindingFlags.Instance; } if (binder == null) { binder = Binder.DefaultBinder; } if ((invokeAttr & BindingFlags.CreateInstance) != 0) { /* the name is ignored */ invokeAttr |= BindingFlags.DeclaredOnly; ConstructorInfo[] ctors = GetConstructors(invokeAttr); object state = null; MethodBase ctor = binder.BindToMethod(invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state); if (ctor == null) { if (this.IsValueType && args == null) { return(Activator.CreateInstanceInternal(this)); } throw new MissingMethodException("Constructor on type '" + FullName + "' not found."); } object result = ctor.Invoke(target, invokeAttr, binder, args, culture); binder.ReorderArgumentArray(ref args, state); return(result); } if (name == String.Empty && Attribute.IsDefined(this, typeof(DefaultMemberAttribute))) { DefaultMemberAttribute attr = (DefaultMemberAttribute)Attribute.GetCustomAttribute(this, typeof(DefaultMemberAttribute)); name = attr.MemberName; } bool ignoreCase = (invokeAttr & BindingFlags.IgnoreCase) != 0; string throwMissingMethodDescription = null; bool throwMissingFieldException = false; if ((invokeAttr & BindingFlags.InvokeMethod) != 0) { MethodInfo[] methods = GetMethodsByName(name, invokeAttr, ignoreCase, this); object state = null; if (args == null) { args = new object [0]; } MethodBase m = binder.BindToMethod(invokeAttr, methods, ref args, modifiers, culture, namedParameters, out state); if (m == null) { if (methods.Length > 0) { throwMissingMethodDescription = "The best match for method " + name + " has some invalid parameter."; } else { throwMissingMethodDescription = "Cannot find method " + name + "."; } } else { ParameterInfo[] parameters = m.GetParameters(); for (int i = 0; i < parameters.Length; ++i) { if (System.Reflection.Missing.Value == args [i] && (parameters [i].Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.HasDefault) { throw new ArgumentException("Used Missing.Value for argument without default value", "parameters"); } } object result = m.Invoke(target, invokeAttr, binder, args, culture); binder.ReorderArgumentArray(ref args, state); return(result); } } if ((invokeAttr & BindingFlags.GetField) != 0) { FieldInfo f = GetField(name, invokeAttr); if (f != null) { return(f.GetValue(target)); } else if ((invokeAttr & BindingFlags.GetProperty) == 0) { throwMissingFieldException = true; } /* try GetProperty */ } else if ((invokeAttr & BindingFlags.SetField) != 0) { FieldInfo f = GetField(name, invokeAttr); if (f != null) { if (args == null) { throw new ArgumentNullException("providedArgs"); } if ((args == null) || args.Length != 1) { throw new ArgumentException("Only the field value can be specified to set a field value.", bindingflags_arg); } f.SetValue(target, args [0]); return(null); } else if ((invokeAttr & BindingFlags.SetProperty) == 0) { throwMissingFieldException = true; } /* try SetProperty */ } if ((invokeAttr & BindingFlags.GetProperty) != 0) { PropertyInfo[] properties = GetPropertiesByName(name, invokeAttr, ignoreCase, this); object state = null; int i, count = 0; for (i = 0; i < properties.Length; ++i) { if ((properties [i].GetGetMethod(true) != null)) { count++; } } MethodBase[] smethods = new MethodBase [count]; count = 0; for (i = 0; i < properties.Length; ++i) { MethodBase mb = properties [i].GetGetMethod(true); if (mb != null) { smethods [count++] = mb; } } MethodBase m = binder.BindToMethod(invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state); if (m == null) { throwMissingFieldException = true; } else { object result = m.Invoke(target, invokeAttr, binder, args, culture); binder.ReorderArgumentArray(ref args, state); return(result); } } else if ((invokeAttr & BindingFlags.SetProperty) != 0) { PropertyInfo[] properties = GetPropertiesByName(name, invokeAttr, ignoreCase, this); object state = null; int i, count = 0; for (i = 0; i < properties.Length; ++i) { if (properties [i].GetSetMethod(true) != null) { count++; } } MethodBase[] smethods = new MethodBase [count]; count = 0; for (i = 0; i < properties.Length; ++i) { MethodBase mb = properties [i].GetSetMethod(true); if (mb != null) { smethods [count++] = mb; } } MethodBase m = binder.BindToMethod(invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state); if (m == null) { throwMissingFieldException = true; } else { object result = m.Invoke(target, invokeAttr, binder, args, culture); binder.ReorderArgumentArray(ref args, state); return(result); } } if (throwMissingMethodDescription != null) { throw new MissingMethodException(throwMissingMethodDescription); } if (throwMissingFieldException) { throw new MissingFieldException("Cannot find variable " + name + "."); } return(null); }
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) { if ((invokeAttr & BindingFlags.CreateInstance) != BindingFlags.Default) { if ((invokeAttr & (BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.SetProperty)) != BindingFlags.Default) { throw new ArgumentException("bindingFlags"); } } else if (name == null) { throw new ArgumentNullException("name"); } if ((invokeAttr & BindingFlags.GetField) != BindingFlags.Default && (invokeAttr & BindingFlags.SetField) != BindingFlags.Default) { throw new ArgumentException("Cannot specify both Get and Set on a field.", "bindingFlags"); } if ((invokeAttr & BindingFlags.GetProperty) != BindingFlags.Default && (invokeAttr & BindingFlags.SetProperty) != BindingFlags.Default) { throw new ArgumentException("Cannot specify both Get and Set on a property.", "bindingFlags"); } if ((invokeAttr & BindingFlags.InvokeMethod) != BindingFlags.Default) { if ((invokeAttr & BindingFlags.SetField) != BindingFlags.Default) { throw new ArgumentException("Cannot specify Set on a field and Invoke on a method.", "bindingFlags"); } if ((invokeAttr & BindingFlags.SetProperty) != BindingFlags.Default) { throw new ArgumentException("Cannot specify Set on a property and Invoke on a method.", "bindingFlags"); } } if (namedParameters != null && (args == null || args.Length < namedParameters.Length)) { throw new ArgumentException("namedParameters cannot be more than named arguments in number"); } if ((invokeAttr & (BindingFlags.InvokeMethod | BindingFlags.CreateInstance | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty)) == BindingFlags.Default) { throw new ArgumentException("Must specify binding flags describing the invoke operation required.", "bindingFlags"); } if ((invokeAttr & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.Default) { invokeAttr |= BindingFlags.Public; } if ((invokeAttr & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Default) { invokeAttr |= (BindingFlags.Instance | BindingFlags.Static); } if (binder == null) { binder = Binder.DefaultBinder; } if ((invokeAttr & BindingFlags.CreateInstance) != BindingFlags.Default) { invokeAttr |= BindingFlags.DeclaredOnly; ConstructorInfo[] constructors = this.GetConstructors(invokeAttr); object state = null; MethodBase methodBase = binder.BindToMethod(invokeAttr, constructors, ref args, modifiers, culture, namedParameters, out state); if (methodBase != null) { object result = methodBase.Invoke(target, invokeAttr, binder, args, culture); binder.ReorderArgumentArray(ref args, state); return(result); } if (this.IsValueType && args == null) { return(Activator.CreateInstanceInternal(this)); } throw new MissingMethodException("Constructor on type '" + this.FullName + "' not found."); } else { if (name == string.Empty && Attribute.IsDefined(this, typeof(DefaultMemberAttribute))) { DefaultMemberAttribute defaultMemberAttribute = (DefaultMemberAttribute)Attribute.GetCustomAttribute(this, typeof(DefaultMemberAttribute)); name = defaultMemberAttribute.MemberName; } bool flag = (invokeAttr & BindingFlags.IgnoreCase) != BindingFlags.Default; string text = null; bool flag2 = false; if ((invokeAttr & BindingFlags.InvokeMethod) != BindingFlags.Default) { MethodInfo[] methodsByName = this.GetMethodsByName(name, invokeAttr, flag, this); object state2 = null; if (args == null) { args = new object[0]; } MethodBase methodBase2 = binder.BindToMethod(invokeAttr, methodsByName, ref args, modifiers, culture, namedParameters, out state2); if (methodBase2 != null) { ParameterInfo[] parameters = methodBase2.GetParameters(); for (int i = 0; i < parameters.Length; i++) { if (System.Reflection.Missing.Value == args[i] && (parameters[i].Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.HasDefault) { throw new ArgumentException("Used Missing.Value for argument without default value", "parameters"); } } bool flag3 = parameters.Length > 0 && Attribute.IsDefined(parameters[parameters.Length - 1], typeof(ParamArrayAttribute)); if (flag3) { this.ReorderParamArrayArguments(ref args, methodBase2); } object result2 = methodBase2.Invoke(target, invokeAttr, binder, args, culture); binder.ReorderArgumentArray(ref args, state2); return(result2); } if (methodsByName.Length > 0) { text = "The best match for method " + name + " has some invalid parameter."; } else { text = "Cannot find method " + name + "."; } } if ((invokeAttr & BindingFlags.GetField) != BindingFlags.Default) { FieldInfo field = this.GetField(name, invokeAttr); if (field != null) { return(field.GetValue(target)); } if ((invokeAttr & BindingFlags.GetProperty) == BindingFlags.Default) { flag2 = true; } } else if ((invokeAttr & BindingFlags.SetField) != BindingFlags.Default) { FieldInfo field2 = this.GetField(name, invokeAttr); if (field2 != null) { if (args == null) { throw new ArgumentNullException("providedArgs"); } if (args == null || args.Length != 1) { throw new ArgumentException("Only the field value can be specified to set a field value.", "bindingFlags"); } field2.SetValue(target, args[0]); return(null); } else if ((invokeAttr & BindingFlags.SetProperty) == BindingFlags.Default) { flag2 = true; } } if ((invokeAttr & BindingFlags.GetProperty) != BindingFlags.Default) { PropertyInfo[] propertiesByName = this.GetPropertiesByName(name, invokeAttr, flag, this); object state3 = null; int num = 0; for (int j = 0; j < propertiesByName.Length; j++) { if (propertiesByName[j].GetGetMethod(true) != null) { num++; } } MethodBase[] array = new MethodBase[num]; num = 0; for (int j = 0; j < propertiesByName.Length; j++) { MethodBase getMethod = propertiesByName[j].GetGetMethod(true); if (getMethod != null) { array[num++] = getMethod; } } MethodBase methodBase3 = binder.BindToMethod(invokeAttr, array, ref args, modifiers, culture, namedParameters, out state3); if (methodBase3 != null) { ParameterInfo[] parameters2 = methodBase3.GetParameters(); bool flag4 = parameters2.Length > 0 && Attribute.IsDefined(parameters2[parameters2.Length - 1], typeof(ParamArrayAttribute)); if (flag4) { this.ReorderParamArrayArguments(ref args, methodBase3); } object result3 = methodBase3.Invoke(target, invokeAttr, binder, args, culture); binder.ReorderArgumentArray(ref args, state3); return(result3); } flag2 = true; } else if ((invokeAttr & BindingFlags.SetProperty) != BindingFlags.Default) { PropertyInfo[] propertiesByName2 = this.GetPropertiesByName(name, invokeAttr, flag, this); object state4 = null; int num2 = 0; for (int k = 0; k < propertiesByName2.Length; k++) { if (propertiesByName2[k].GetSetMethod(true) != null) { num2++; } } MethodBase[] array2 = new MethodBase[num2]; num2 = 0; for (int k = 0; k < propertiesByName2.Length; k++) { MethodBase setMethod = propertiesByName2[k].GetSetMethod(true); if (setMethod != null) { array2[num2++] = setMethod; } } MethodBase methodBase4 = binder.BindToMethod(invokeAttr, array2, ref args, modifiers, culture, namedParameters, out state4); if (methodBase4 != null) { ParameterInfo[] parameters3 = methodBase4.GetParameters(); bool flag5 = parameters3.Length > 0 && Attribute.IsDefined(parameters3[parameters3.Length - 1], typeof(ParamArrayAttribute)); if (flag5) { this.ReorderParamArrayArguments(ref args, methodBase4); } object result4 = methodBase4.Invoke(target, invokeAttr, binder, args, culture); binder.ReorderArgumentArray(ref args, state4); return(result4); } flag2 = true; } if (text != null) { throw new MissingMethodException(text); } if (flag2) { throw new MissingFieldException("Cannot find variable " + name + "."); } return(null); } }