A library written in C#
Someone wrote a library in C# — a colleague, or a NuGet package. This lesson builds a tiny C# library and shows how to wrap it and use it from Ring like any other class.
What you will learn#
- the steps to use any .NET library from Ring,
- what a C# class looks like, so you can read the ones you meet,
- how properties, methods, overloads, static methods,
outparameters, events and enums arrive in Ring.
The steps#
- Get the library as a
.dll(build it, or copy it from a NuGet package). - Wrap it:
ring wrapassembly.ring Library.dll classeswrites a Ring class for every public C# class. - Load it:
dnLoad("Library.dll")anddnImportFrom("classes/","Namespace"). - Use it:
new ClassName(), exactly like the built-in classes.
The C# library#
You do not have to write C#, but reading a little helps. This library is in samples/UsingDotNet/tutorials/Greeter:
using System;
namespace Tutorial.Greetings
{
// An enum: Ring uses its values as texts ("Polite", "Casual")
public enum Style { Polite, Casual, Shout }
// A class written in C#: the properties, methods and events below are what Ring can use
public class Greeter
{
// a property that can be read and written
public string Name { get; set; } = "World";
// a property that can only be read
public int Greetings { get; private set; }
// a method without parameters, and a method with parameters (an overload)
public string Hello()
{
return Hello(Name, Style.Polite);
}
public string Hello(string name, Style style = Style.Polite)
{
Greetings++;
string text = style == Style.Casual ? "Hey " + name : "Hello, " + name;
if (style == Style.Shout) text = text.ToUpper() + "!";
Greeted?.Invoke(this, text);
return text;
}
// a static method: no object needed
public static int Add(int a, int b)
{
return a + b;
}
// a method with an out parameter: Ring gets [result, value]
public bool TryDivide(double a, double b, out double result)
{
result = 0;
if (b == 0) return false;
result = a / b;
return true;
}
// an event: Ring sets its handler with setGreetedEvent("...")
public event EventHandler<string> Greeted;
}
}The project file lists what to build:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<AssemblyName>Greeter</AssemblyName>
<DebugType>none</DebugType>
</PropertyGroup>
</Project>To build it you need the .NET SDK (dotnet --version prints its version):
dotnet build Greeter -c Release -o Greeter/binThe Ring program#
# using a library written in C# - build it, wrap it, call it
# (the library is in the folder Greeter: Greeter.cs and Greeter.csproj)
load "dotnet.ring"
nTimes = 0 # event code can only see globals
cHere = currentdir()
cDll = cHere + "/Greeter/bin/Greeter.dll"
cClasses = cHere + "/Greeter/classes"
# STEP 1. Build the C# project (the .NET SDK is needed for this step only)
if not fexists(cDll)
? "building the library..."
system("dotnet build Greeter -c Release -o Greeter/bin -nologo -v q > nul")
ok
# STEP 2. Make Ring classes for it. The generator is a Ring program of this package: tools/dotnettools/wrapassembly.ring
chdir("../../../tools/dotnettools")
system(exefilename() + " wrapassembly.ring " + cDll + " " + cClasses + " > nul")
chdir(cHere)
# STEP 3. Load the assembly and the generated classes
dnLoad(cDll)
dnImportFrom(cClasses + "/","Tutorial.Greetings")
func main
# STEP 4. Use the class like any other
oGreeter = new Greeter()
? oGreeter.Hello(NULL) # the overload without parameters
oGreeter.Name = "Ring" # a property
? oGreeter.Hello(NULL)
? oGreeter.Hello([ :name = "Ann", :style = "Casual" ]) # named arguments; the enum is a text
? oGreeter.Hello([ :name = "Bob", :style = "Shout" ])
? "Greetings so far: " + oGreeter.Greetings # a property that can only be read
# a static method
? "Add(20,22) : " + new Greeter().Add(20,22)
# an out parameter: the answer is a list [worked, result]
aAnswer = oGreeter.TryDivide(10,4)
? "TryDivide(10,4) : worked=" + aAnswer[1] + " result=" + aAnswer[2]
aAnswer = oGreeter.TryDivide(1,0)
? "TryDivide(1,0) : worked=" + aAnswer[1]
# an event: Ring code runs each time the greeter greets
oGreeter.setGreetedEvent("nTimes++ ? ' (event: someone was greeted: ' + aDotNetArgs[2] + ')'")
oGreeter.Hello([ :name = "Cy" ])
? "The event ran " + nTimes + " time(s)"Hello, World
Hello, Ring
Hey Ann
HELLO, BOB!
Greetings so far: 4
Add(20,22) : 42
TryDivide(10,4) : worked=1 result=2.50
TryDivide(1,0) : worked=0
(event: someone was greeted: Hello, Cy)
The event ran 1 time(s)How C# members look in Ring#
| In C# | In Ring |
|---|---|
public string Name { get; set; } | oGreeter.Name and oGreeter.Name = "Ring" |
public int Greetings { get; private set; } | oGreeter.Greetings — read only |
Hello() and Hello(string name, Style style) | Hello(NULL) and Hello([ :name = "Ann", :style = "Casual" ]) |
public enum Style { Polite, Casual, Shout } | the texts "Polite", "Casual", "Shout" |
public static int Add(int a, int b) | new Greeter().Add(20,22) |
bool TryDivide(double a, double b, out double result) | a list [worked, result] |
public event EventHandler<string> Greeted; | oGreeter.setGreetedEvent("code") |
public static string Shout(this Greeter g, string end) (an extension method) | oGreeter.Shout("!"): a method of the Ring class of the type that it extends |
Overloads and named arguments#
Hello has two versions. The plain name Hello takes one parameter: NULL calls the version without parameters; a list of :name = value calls the version with those parameter names. The style parameter is optional in C# (Style style = Style.Polite), so you may leave it out: Hello([ :name = "Cy" ]).
Events#
setGreetedEvent("nTimes++ ? ...") gives Ring code to run each time the C# code raises Greeted. The code reads aDotNetArgs, which is [sender, eventArgs]: here the event argument is the greeting text. Remember that event code sees only global variables (nTimes is global at the top of the program).
What the wrapper made#
Open Greeter/classes/Tutorial.Greetings.classes.ring: it has a plain Ring class for each C# class, with one method per C# method. Nothing is hidden.
Using a NuGet package#
A package is a set of .dll files, and the .NET SDK can get it and its dependencies for you with a project file and dotnet publish. The tutorial A NuGet package shows it for a real package, and then the steps are the same: wrapassembly.ring, dnLoad, dnImportFrom. See also Your own assemblies.
What you learned#
- wrap → load → use,
- C# properties, overloads, statics,
out, events and enums map to Ring as shown, - you can use any .NET library without writing C#.
Exercises#
- Add a property
Prefix(default"Hello") toGreeterand use it inHello. Rebuild, rewrap, run. - Add a method
Repeat(string text, int times)and call it with named arguments. - Find a small NuGet package (for example a text or math helper), copy its DLL, wrap it and call one method.