NetObject: NetObject class

Description Active bindings Methods Examples

Description

NetObject R6 class to wrap an 'externalptr“which represents a .Net object.

Active bindings

Ptr

externalptr of the wrapped .Net object

Methods

Public methods


Method new()

Create a new NetObject.

Usage
NetObject$new(typeName = NULL, ptr = NULL, ...)
Arguments
typeName

.Net full type name.

ptr

externalptr of the .Net object.

...

Property setters

Details

You can use 2 ways to instanciate a .NetObject. If you specify the externalptr through the ptr parameter, this pointer will be wrapped and stored into the Ptr active binding. Otherwise if your .Net class has a default constructor you can specify the typeName as netNew(typeName) does.

The ellipsis parameter can be use to setup the .Net properties. This feature works with both building ways. You can use it as follow o <- NetObject$new(typeName, Name = "My name", Id = 1L)

Returns

A new wrapped .Net object.


Method get()

Gets a property value

Usage
NetObject$get(propertyName, wrap = TRUE)
Arguments
propertyName

Property name

wrap

Specify if you want to wrap a externalptr .Net object into a NetObject object. 'TRUE“ by default.

Details

Allows you to get a property value for a .Net object. The result will be converted if the type mapping is defined. All native C# types are mapped to R types but you can define custom converters in C# for that see the C# RDotNetConverter class.

By default wrap is set to TRUE. If there is no native convertion found between .Net type and R type, the result is wrapped into NetObject instance. A best R6 type can be chosen if this type exists. This R6 class has to inherit from NetObject and has to have the same name than the C# class. You can generate automatically this inherited class. For more details about this feature please see the netGenerateR6 function. If you prefer get a raw externalptr to the .Net object,

This function is aquivalent to call netGet(o$Ptr, propertyName)

Returns

Returns the .Net property value. If a converter has been defined between the .Net type and a R type, the R type will be returned. Otherwise an externalptr or a NetObject if wrap is set to TRUE.


Method set()

Sets a property value

Usage
NetObject$set(propertyName, value)
Arguments
propertyName

Property name.

value

value to set

Details

Allows you to set a property value of a .Net object. The input value will be converted from R type to a .Net type.

If the property value isn't a native C# type or a mapped conversion type, you have to use an externalptr on .Net object or a NetObject R6 instance.

You can define custom converters in C# for that see RDotNetConverter C# class.

This function is aquivalent to call netSet(o$Ptr, propertyName, value).


Method call()

Call a .Net method member.

Usage
NetObject$call(methodName, ..., wrap = TRUE, out_env = parent.frame())
Arguments
methodName

Method name

...

Method arguments

wrap

Specify if you want to wrap externalptr .Net object into NetObject R6 object. 'TRUE“ by default.

out_env

In case of a .Net method with out or ref argument is called, specify on which environment you want to out put this arguments. By default it's the caller environment i.e. parent.frame().

Details

Call a method member for a given .Net object. Ellipses has to keep the .Net arguments method order, the named arguments are not yet supported. If there is conflicts with a method name (many definition in .Net), a score is computed from your argument's order and type. We consider a higher score single value comparing to collection of values.

If you decide to set wrap to TRUE, the function returns a NetObject instead of a raw externalptr. To remind an externalptr is returned only if no one native converter has been found. The NetObject R6 object wrapper can be an inherited R6 class. For more details about inherited NetObject class please see netGenerateR6 function.

The out_env is usefull when the callee .Net method has some out or ref argument. Because in .Net this argument set the given variable in the caller scope. We reflect this mechanism in R. By default the given varable is modify in the parent R environment which means the caller or parent.frame(). You can decide where to redirect the outputed value by specifying another environment. Of course be sure that the variable name exists in this targetd environment.

This function is aquivalent to call netCall(o$Ptr, methodName).

Returns

Returns the .Net result. If a converter has been defined between the .Net type and a R type, the R type will be returned. Otherwise an externalptr or a NetObject if wrap is set to TRUE.


Method as()

Cast the current R6 class to another.

Usage
NetObject$as(className)
Arguments
className

R6 class name to cast.

Details

If the R6 className already exists the wrapped externalptr which represents a .Net object will be transfer to a new R6 instance of type className.

Returns

a new R6 instance of type className.


Method getType()

Gets NetType description of wrapped .Net object

Usage
NetObject$getType()

Method print()

Print the object

Usage
NetObject$print(...)
Arguments
...

.


Method clone()

The objects of this class are cloneable with this method.

Usage
NetObject$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## Not run: 
library(sharper)
package_folder <- path.package("sharper")
netLoadAssembly(file.path(package_folder, "tests", "AssemblyForTests.dll"))

x <- netNew("AssemblyForTests.DefaultCtorData")
object <- NetObject$new(ptr = x)
object$set("Name", "My Name")
object$get("Name")

## End(Not run)

fdieulle/sharper documentation built on Aug. 1, 2020, 4:19 p.m.