Class wwDotNetBridge

This class allows accessing of .NET classes without requiring COM registration for the .NET classes. It works by hosting the .NET runtime in Visual FoxPro and using a small proxy class to act as a Class Factory for instantiating .NET objects and passing them back for use in Visual FoxPro.

The class works through a DLL interface in wwIPStuff.dll that loads the .NET runtime, and a small .NET assemblys wwDotNetBridge.dll that provides the proxy services for instantiating types and passing the back to VFP. The wwDotBridge FoxPro class is simply a front end for the .NET class.

This class supports:

  • No COM Registration required for accessing .NET Assemblies
  • Explicit loading of .NET assemblies from disk or the GAC
  • Support for .NET 2.0 (incl. 3.x) and 4.0 Runtimes (incl. 4.5.x, 4.6.x, 4.7.x)
  • Access to most .NET components from FoxPro
  • Access to static values and methods
  • Access to Value Types
  • Access to Enumerated values
  • Accessing to Generic .NET Types
  • Call .NET methods asynchronously
  • Many helpers for accessing .NET Arrays, Lists and Dictionaries
  • Auto-Conversion of many problem .NET types in FoxPro
  • DataSet conversions to and from XmlAdapter (and from XmlAdapter to cursors)
  • Auto-Conversion of many general FoxPro types to specific .NET types
  • ToJson and ToXml Helper Routines to debug object value state

Getting Started

Somewhere in the startup of your application call InitializeDotnetVersion()

*** Load dependencies and add to Procedure stack
DO wwDotnetBridge
InitializeDotnetVersion("V4")   && V2

This ensures that wwDotnetBridge loads with the specified single version of the .NET Runtime that your FoxPro application can load.

Then when you need to utilize wwDotnetBridge call GetwwDotnetBridge() to get a cached instance and use it to access .NET components:

*** Create or get cached instance of wwdotnetbridge
LOCAL loBridge as wwDotnetBridge, loHttp
loBridge = GetwwDotnetBridge()

*** Create a built-in .NET class and run a method
loHttp = loBridge.CreateInstance("System.Net.WebClient")
loHttp.DownloadFile("http://west-wind.com/files/MarkdownMonsterSetup.exe",
                    "MarkdownMonsterSetup.exe")
DO wwUtils
GoUrl(FULLPATH("MarkdownMonsterSetup.exe"))  && run it


*** Load a custom .NET assembly
loBridge.LoadAssembly("CustomDotnet.dll")

*** Access a .NET component from the new assembly
loItem = loBridge.CreateInstance("Custom.Item")
? loItem.Sku
loItem.Sku = "NewSku"
lnTotal = loItem.CalculateTotal()

Note that not all properties and methods can be accessed directly as shown on the loItem example above. Instead you may have to call GetProperty(), SetProperty() or InvokeMethod() to indirectly access object members. If direct access fails, always try the indirect methods.

For much more detailed usage information you can also check out our white paper:

Remarks

Cannot load CLR Instance Errors

If you get an Cannot load CLR Instance error when creating an instance of wwDotnetBridge, you probably need to unblock the wwdotnetbridge.dll. Right click on wwdotnetbridge.dll and click the Unblock button. Please check Cannot load CLR Instance for more info.

Set .NET Version on Application Startup

You can only load a single version of the .NET Framework into a FoxPro app with wwDotnetBridge. To ensure you control the version that you want your application to use explicitly specify the version when your application starts up, in the main program or main form.

InitializeDotnetVersion("V4")   && or "V2"
  • Relies on wwipstuff.dll and wwDotNetBridge.dll (.NET 4.0 binary)

.NET Runtime Support

The following versions of .NET are supported with the version string in bold:

  • .NET 4.0 (Full Framework only) - V4
  • .NET 4.5.x, .NET 4.6.x, .NET 4.7.x - V4

wwDotnetBridge does not support:

  • .NET 4.0 Client profile

Class Members

MemberDescription

ConvertToDotNetValue

Converts a FoxPro value of an 'unsupported' type to a .NET type by either using a FoxPro CAST() or converting the object into a wwDotnetBridge .NET ComValue object.

o.ConvertToDotNetValue(lvValue, lcType)

CreateArray

Creates a new ComArray instance based on the .NET type you specify as a parameter.

o.CreateArray(lvArrayInstanceOrTypeName)

CreateComValue

Creates an instance of a ComValue object which allows you to capture values from method calls and properties and keep them in .NET.

o.CreateComValue(lvValue)

CreateInstance

Creates an instance of a .NET class and passes it back to Visual FoxPro as a COM reference.

o.CreateInstance(lcClass, lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)

CreateInstanceOnType

Allows creation of a new .NET type and assigning that type to a .NET object property without ever going through Visual FoxPro. This allows for creation on non-COM visible types in .NET and indirect manipulation of them.

o.wwDotNetBridge.CreateInstanceOnType(loInstance,lcProperty, lcClass,lvParm1,lvParm2,lvParm3, lvParm4, lvParm5)

CursorToDataSet

Creates a .NET DataSet from a single or multiple open FoxPro cursors.

o.CursorToDataSet(lcAliasList)

DataSetToCursors

Converts a .NET DataSet to cursors that match the tables in the DataSet's Tables collection.

o.DataSetToCursors(loDs)

DataSetToXmlAdapter

Converts a .NET DataSet object instance to a Visual FoxPro XmlAdapter object that can be turned easily into cursors.

o.DataSetToXmlAdapter(loDs)

FromJson

Deserializes JSON into a .NET object or simple value.

o.FromJson(lcJson,lvDotnetType)

FromXml

Parses an XML string into a .NET object.

o.FromXml(lcJson,lvDotnetType)

GetEnumString

Retrieves the string enum field name from a .NET Enum value.

o.GetEnumString(lcEnumType, lvEnumValue)

GetEnumValue

Retrieves a .NET Enumerated value by passing in a string representation and retrieving the corresponding numeric value.

o.GetEnumValue(lcType, lcValue)

GetIndexedProperty

Retrieves an indexed value from an indexed list like an array or a IList based collection.

o.GetIndexedProperty(loInstance, lcProperty)

GetProperty

Retrieves a property value from an object instance via Reflection.

o.GetProperty(loInstance, lcProperty)

GetStaticProperty

Retrieves a static property from a .NET type.

o.GetStaticProperty(lcType, lcProperty)

GetType

Returns a .NET Type object from the value passed to this function.

o.GetType(lvValue)

GetTypeFromName

Returns a .NET type reference for a type by its fully qualified .NET type name (ie. namespace.classname).

o.GetTypeFromName(lcTypeName)

GetwwDotNetBridge

Factory method that can be used to create a PUBLIC instance of the wwDotNetBridge class. Use this method instead of explicitly instantiating the class to avoid recreating the .NET object wrapper object each time.

GetwwDotNetBridge(lcVersion)

Init

Constructor for the wwDotNetBridge class. Allows specification of the .NET runtime to be used via optional parameter. Last optional parameter determines whether .NET runtime is loaded directly into VFP or whether the .NET component is loaded via COM Interop.

o.wwDotNetBridge.Init(lcDotNetVersion, llUseComInterop)

InitializeDotnetVersion

Use this function in your application's startup program to force the .NET version used by wwDotnetBridge for the entire application.

o.InitializeDotnetVersion(lcVersion)

InvokeMethod

Invokes a method on a .NET object instance. This method can be used when the object instance cannot be directly accessed via COM such as special collections or generic types for example.

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

InvokeMethodAsync

Allows calling a .NET method and executing it asynchronously on a background thread. This is useful for long running operations that take a long time to complete, or if you need to run many simultaneous operations at the same time.

o.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1-10)

InvokeMethod_ParameterArray

Like InvokeMethod, but allows passing an arbitrary number of parameters in an array rather than as a named parameter. The array is translated into individual parameters passed into the method called.

o.InvokeMethod_ParameterArray(loInst,lcMethod,laParams)

InvokeStaticMethod

This method allows you to call a static .NET method such as System.Windows.Forms.MessageBox.Show() for example. Because the methods are static you can't create an instance and pass it back to FoxPro, instead this method has to be used to invoke a method.

o.InvokeStaticMethod(lcTypeName, lcMethod, lvParm1, ... lvParm10)

InvokeStaticMethodAsync

Invokes a .NET static method asynchronously on a seperate thread and fires OnCompleted() and OnError() events into the passed in Callback object.

o.InvokeStaticMethodAsync(loCallback,lcTypeName,lcMethod,lvParm1-10)

Load

Internal function responsible for bootstrapping the .NET Runtime and grabbing a wwdotnetBridge instance.

o.Load()

LoadAssembly

Loads a .NET assembly into the .NET Runtime currently executing. Note that you should ensure that any required assemblies and dependent assemblies are loaded explicitly into the runtime.

o.LoadAssembly(lcAssembly)

RunThread

Allows firing off a PRG file on a new thread. Compiles and executes this PRG and can fire events to an loEvents object passed in as a parameter. You can implement callback functionality on the loEvents object.

o.RunThread(lcPrgFilename,loEvents)

SetProperty

Sets a .NET object property via Reflection.

o.SetProperty(loInstance,lcProperty,lvValue)

SetStaticProperty

Sets a static property or Enum value. Same behavior as SetProperty() but with static properties.

o.SetStaticProperty(lcType,lcProperty,lcValue)

ToJson

Creates a JSON string from a .NET object or value passed in.

o.ToJson(loObject)

ToXml

Serializes a .NET object to an XML string.

o.ToXml(loObject)

Unload

Unloads the .NET AppDomain and any loaded assemblies within it.

o.Unload()

XmlAdapterGetCursor

Retrieves an indivdual cursor from a loaded XmlAdapter object.

o.XmlAdapterGetCursor(loAdapter,lvIndex)

XmlAdapterToCursors

Helper method that generates one cursor for each of the XmlAdapter's Tables. Cursors are created with the names defined in the XML document's item nodes (ie. the DataSet Table names).

o.XmlAdapterToCursors(loAdapter)

XmlStringToDataSet

Creates a .NET DataSet from an XML String that is exported from XmlAdapter or CursorToXml.

o.XmlStringToDataSet(lcXml)

cErrorMsg

Contains an error message if a method call fails.

lError

Error flag that can be checked after certain method calls.

oLastException

Returns the last .NET exception that was caused by a failed operation.

Requirements

Assembly: wwdotnetbridge.prg

See also:

wwDotNetBridge Examples | What is wwDotnetBridge? | wwDotnetBridge Features

© West Wind Technologies, 2004-2017 • Updated: 06/07/17
Comment or report problem with topic