}
/*
- * InternalInvoke() receives the parameters corretcly converted by the binder
+ * InternalInvoke() receives the parameters correctly converted by the binder
* to match the types of the method signature.
*/
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[DebuggerHidden]
[DebuggerStepThrough]
- public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
+ public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
{
+ if (obj == null) {
+ if (!IsStatic)
+ throw new TargetException ("Instance constructor requires a target");
+ } else if (!DeclaringType.IsInstanceOfType (obj)) {
+ throw new TargetException ("Constructor does not match target type");
+ }
+
return DoInvoke (obj, invokeAttr, binder, parameters, culture);
}
invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
if (binder == null)
- binder = Binder.DefaultBinder;
+ 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;
+ return Activator.CreateInstance (this, invokeAttr, binder, args, culture);
}
if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
{
}
}
+
+ [Test]
+ [ExpectedException (typeof (TargetException))]
+ public void InvokeWithNullTarget ()
+ {
+ typeof (Foo).GetConstructors ()[0].Invoke (null, BindingFlags.Default, null, null, null);
+ }
+
+ [Test]
+ [ExpectedException (typeof (TargetException))]
+ public void InvokeWithWrongTarget ()
+ {
+ typeof (Foo).GetConstructors ()[0].Invoke (new object (), BindingFlags.Default, null, null, null);
+ }
}
}