MethodBuilder — Method: Calling a Stream Constructor
Overview
OPC UA Call The service (Part 4 §5.11) requires that the input parameters be packaged into Variant[], with the order corresponding exactly to the server-side description InputArguments. Writing this by hand can Variant[] easily lead to mistakes:
- Order Misalignment (
speedandaccelReversal) - Type Mismatch (The server expects
Int32, but the client wroteDouble) - Missing Arguments / Extra Arguments
MethodBuilder Provide a list API, string them together in a single line LoadInputArgs / Arg(name, value) / Invoke, and automatically sort them by type .NET Variant.Set*.
Corresponding section of the specification: Part 4 §5.11.2 (Call) / Part 5 §12.3 (Argument).
API
| Methods | Category | Reading and Writing | Note |
|---|---|---|---|
| new MethodBuilder(ua, objectId, methodId) | Structure | — | Bound to a method |
| .LoadInputArgs() | Chain-type | Read | Check whether the InputArguments property exists on the server (do not throw an exception on failure) |
| .Arg(string name, object value) | Chain-type | — | Set Values by Parameter Name |
| .Arg(int index, object value) | Chain-type | — | Set values by index (starting at 0) |
| .Invoke() | Terminal | Write | Call the method, return MethodCallResult |
| .InputArgs | Properties | Read | List of Input Parameter Descriptions |
| .InputArgsLoaded | Properties | Read | Whether the InputArguments property is detected |
MethodCallResult Three members: Status (StatusCode) / Outputs (IReadOnlyList<Variant>, the caller is responsible for Dispose) / IsGood (bool).
Automatic wrapping of .NET Types: bool / sbyte / byte / short / ushort / int / uint / long / ulong / float / double / string / DateTime, and arrays bool[] / int[] / uint[] / long[] / float[] / double[] / string[]; Passed Variant as-is; all other types are ToString() converted to strings.
Code Example
using System;
using System.Threading;
using DarraOpcUa_Client;
using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Connect();
// 1) Set parameters by index (recommended)
var r = new MethodBuilder(ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.LoadInputArgs()
.Arg(0, 100.0)
.Arg(1, 200.0)
.Arg(2, 50)
.Invoke();
foreach (var v in r.Outputs) v.Dispose(); // Output: Must be disposed of by Variant the caller
if (!r.IsGood)
Console.WriteLine($"MoveTo 失败: {r.Status}");
// 2) Read the output
var add = new MethodBuilder(ua, "ns=2;s=Calculator", "ns=2;s=Calculator.Add")
.Arg(0, 3.14)
.Arg(1, 2.71)
.Invoke();
if (add.IsGood && add.Outputs.Count > 0)
Console.WriteLine($"Add → {add.Outputs[0].AsDouble}"); // AsDouble Is it a double?
foreach (var v in add.Outputs) v.Dispose();
// 3) Reuse the builder for multiple calls
var b = new MethodBuilder(ua, "ns=2;s=Pump", "ns=2;s=Pump.SetSpeed")
.LoadInputArgs();
foreach (var rpm in new[] { 100, 500, 1000, 1500 })
{
var s = b.Arg(0, rpm).Invoke();
foreach (var v in s.Outputs) v.Dispose();
if (!s.IsGood) break;
Thread.Sleep(500);
}
Comparison with Call
// Hand-assembled Variant[]
var inputs = new Variant[3];
inputs[0] = new Variant(); inputs[0].Double = 100.0;
inputs[1] = new Variant(); inputs[1].Double = 200.0;
inputs[2] = new Variant(); inputs[2].Int32 = 50;
var outs = ua.Call("ns=2;s=Robot", "ns=2;s=Robot.MoveTo", inputs);
// MethodBuilder
var r = new MethodBuilder(ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.Arg(0, 100.0)
.Arg(1, 200.0)
.Arg(2, 50)
.Invoke();
// The inputs and outputs in both paths Variant belong to the caller; they must be disposed of after use.
Implementation Constraints
LoadInputArgs()Perform existence checks only: The elementsInputArgsin are placeholders (Namein the form ofarg0/arg1,Description/DataTypeis empty,BuiltInTypeisNull,ValueRankis -1), Unable to obtain the actual parameter names and types from the server. Indexing is notArg(int, value)affected.- Alignment with the placeholder names
Arg(string name, value)in andInputArgsis required for this to work; if they do not match, values are filled in based on theArginsertion order in. - Complex structures are not supported as input parameters; only basic types and the arrays listed in the table above are supported.
- Failed server calls will be collapsed into
MethodCallResult.Status+ emptyOutputs, Do not throw exceptions; However, if an input parameter conversion error occurs (a custom typeToString()throws an exception), the exception will still be propagated outward, requiring the caller totry/catchcatch it. - Construction and
ArgThrows parameter exceptions:ua/objectId/methodIdis null →ArgumentNullException;Arg(int)passes a negative index →ArgumentOutOfRangeException;Arg(string)passes an empty name →ArgumentNullException
Best Practices
- Set Parameters by Index — The actual parameter name cannot be obtained
InputArgshere;Arg(int, value)this is a reliable path - LoadInputArgs before the call — Let
Invokeknow the total number of parameters to avoid missing or extra parameters - Reusing the builder — To call the same method multiple times, simply create a builder and modify only the values
Arg. - Judgment
IsGood— Don't assume that a call will always succeed - Do not use thread UI synchronization —
InvokeYes RPC, useawait Task.Run(() => builder.Invoke())
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | DarraUa_MethodBuilder_* |
| .Arg(name, value) | .arg(name, value) | .arg(name, value) | .Arg(name, value) | .arg(name, value) | DarraUa_MethodBuilder_ArgDouble, etc. (by type) |
| .Invoke() | .invoke() | .invoke() | .Invoke() | .invoke() | DarraUa_MethodBuilder_Invoke |