Recherche…


Introduction

Cette rubrique couvrira l'interopérabilité entre VFP et .NET.

Utilisation de wwDotNetBridge pour exécuter du code .NET

Avec l'aide de wwDotNetBridge de West Wind , vous pouvez facilement accéder au code .NET dans un programme VFP.

Le livre blanc contient tous les détails, mais cet exemple concis aidera à illustrer les étapes de base de l'exécution d'une méthode dans un assembly .NET.

Notez que wwDotNetBridge peut accéder directement à des propriétés simples telles que les chaînes, les ints, etc. bas de cet exemple).

*!* 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
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow