wwDotNetBridge::InvokeMethod

Invokes a method on a .NET object instance.

While you can often call methods directly on an instance, the COM Interop mechanism has many limitations for calling .NET methods that have multiple overloads, have value, generic or incompatible types as parameters.

InvokeMethod() can be used when the method call will not work directly.

This method is limited to a maximum of 10 parameters. If you need more parameters use InvokeMethod_ParameterArray() instead.

Invoked method is fixed up so that arrays are passed/returned as ComArray instances, Guids as ComGuid.

o.InvokeMethod(loInstance,lcMethod,lvParm1,...lvParm10)

Parameters

loInstance
An object instance on which to invoke the method

lcMethod
The name of the method to call on the Instance.

The method can also be a 'nested' method name like "ValidationErrors.ToHtml". Use this syntax if a type along the hierarchy is not accessible by FoxPro (Value type, Generic Type etc.)

lvParm1..lvParm24
Optional parameters up to 24

Remarks

Parameter passing errors, or errors that occur inside of the .NET code that you are calling cause an Exception in FoxPro code.

If you get Invalid Method or Method not found errors most likely you are calling the method with missing or invalidly typed parameters.

In some cases you may have to explicitly coerce parameter types by using the VFP CAST() function. This is especially true for numeric values since VFP numbers are typically passed as double or int depending on the existance of decimals. For example, decimal values need CAST(1.11 as Currency) or CAST(10 as int).

You can also use the ComValue Class to explicitly force parameter types to specific types, including types that you cannot set directly from FoxPro such as Single, Long, Byte, Guid, DbNull and numerous others.

To pass ByRef/Ref parameters use ComValue objects instead of plain parameter values. ComValue parameters will always be updated after returning from a method call.

Example

*** Simple Method Invokation:

loNet = loBridge.Createinstance("Westwind.WebConnection.TypePassingTests")
? loBridge.InvokeMethod(loNet,"HelloWorld","Rick")
*** Note this is the same as:
loNet.HelloWorld("Rick")


*** Pass ByRef parameters and return values:

*** Create ComValue objects for each parameter
loInt = loBridge.CreateComValue(INT(10))
loString = loBridge.CreateComValue("Hello World.")
loDecimal = loBridge.CreateComValue(CAST( 5.22 as Currency))

? "Original:"
? loInt.Value, loString.Value, loDecimal.Value

lobridge.InvokeMethod(loNet,;
                      "PassByReference",;
                      loInt,loString,loDecimal)

*** Look at the result values
? "Updated:"
? loInt.Value, loString.Value, loDecimal.Value

See also:

Class wwDotNetBridge

© West Wind Technologies, 2023 • Updated: 05/20/18
Comment or report problem with topic