DotNet Example: Using a Private Assembly


Download and unzip the attached assembly, which corresponds to the C# source code in the first snippet. Paste the Xbasic code in the second snippet into an Alpha Anywhere script, edit the path to the location of the downloaded DLL, and run the script.


The C# source code for the .NET class being used. Compile the example below into a DLL named "MyCSLib.dll" or download the DLL for the source below:

using System;
using System.Text;
namespace MyCSLib
    public class MyCSClass
        public String MyProperty
                return "Hello from MyCSClass";
        public String DoSomething()
            return "MyCSClass did something";

The Xbasic code below loads the MyCSLib.dll (code shown above) and registers classes available in the DLL. If class registration succeeds, it creates a MyCSClass object and performs several operations using the class:

dim Sv as DotNet::Services
dim assy as DotNet::AssemblyReference

'Update assy.FileName to use the location where the 
'MyCSLib.dll file is located on your system:

if .not. file.exists(assy.FileName)
    ui_msg_box("can't find file",assy.FileName)
end if

rc = sv.RegisterClass("My","Class","MyCSLib.MyCSClass",assy)

if rc then
    dim cl as My::Class
    dim result as c
    result= result + crlf() + cl.MyProperty
    ui_msg_box("MyCSLib Demo","MyCSClass class not registered")
    'Note: if you get here, try closing and restarting Alpha Anywhere
    'It usually means a previous registration failed
    'It can also mean that you put a downloaded file in a protected spot, such as C:\
    'You can right-click on the DLL file from Windows Explorer and unblock it.
end if

If the custom .NET class is successfully registered, a dialog will be shown with the following information:

MyCSClass did something
Hello from MyCSClass

See Also