Buscar..


Introducción

Este tema cubrirá la interoperabilidad entre VFP y .NET.

Usando wwDotNetBridge para ejecutar código .NET

Con la ayuda de wwDotNetBridge de West Wind , puede tener acceso fácilmente al código .NET dentro de un programa VFP.

El documento técnico tiene todos los detalles, pero este ejemplo conciso ayudará a ilustrar los pasos básicos para ejecutar un método en un ensamblado .NET.

Tenga en cuenta que wwDotNetBridge puede acceder directamente a propiedades simples como cadenas, ints, etc. Para acceder a estructuras más complicadas como listas, primero debe usar la función wArtNetBridge CreateArray para convertir la estructura .NET a una matriz COM de VFP (como se muestra en la parte inferior de este ejemplo).

*!* Load WestWind .NET wrapper library (wwdotnetbridge.prg assumed to be in the search path)
IF (!wwDotNetBridge())
    RETURN .F.
ENDIF

lowwDotNetBridge = CREATEOBJECT("wwDotNetBridge","V4")

*!* Load .NET Assembly (include full or relative path if necessary)
IF !lowwDotNetBridge.LoadAssembly("SomeDotNetAssembly.dll")
    lcAssemblyLoadError = "LoadAssembly error: " + lowwDotNetBridge.cErrorMsg
    =MESSAGEBOX(lcAssemblyLoadError, MB_ICONSTOP, "Error")
    RETURN .F.
ENDIF

*!* Parameters to pass to class constructor
*!* You can pass up to 5 paramenters to the constructor
lcParameter1 = "StringParameter1"
lcParameter2 = "StringParameter2"
lnParameter3 = 3
lcParameter4 = .NULL.

*!* Get an instance of the assembly class
loAssemblyReference = lowwDotNetBridge.CreateInstance("MyDotNetProject.MyDotNetClass", ;
    lcParameter1, lcParameter2, lnParameter3, lcParameter4)
IF lowwDotNetBridge.lError
    lcAssemblyLoadError = "An error occurred loading the class: " + lowwDotNetBridge.cErrorMsg
    RETURN .F.
ENDIF

*!* Usage Example

*!* This example runs a method that return a boolean 
*!* and populates a List<string> (SomeStringList).
*!*
*!* The assembly has a public property named "LastErrorMessage" 
*!* with details about any handled exceptions/problems.

IF (!loAssemblyReference.SomePublicMethod())
    msg = "There was a problem executing the method:" + CRLF + ;
        loAssemblyReference.LastErrorMessage
    =MESSAGEBOX(msg, MB_ICONSTOP, "Error")
    RETURN .F.
ENDIF

*!* At this point the string list (SomeStringList) should be populated
*!* wwDotNetBridge can convert that list to a VFP COM array (0-based)

laVFPArrayOfStrings = lowwDotNetBridge.CreateArray()
laVFPArrayOfStrings.FromEnumerable(loAssemblyReference.SomeStringList)

FOR x = 0 TO laVFPArrayOfStrings.Count-1
    ? laVFPArrayOfStrings.Item(x)
ENDFOR


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow