![]() |
Common Language Extension(CLE) |
|||
For c# calls java, here presents a new method, which ues cle developed by srplab.A function “Add” is defined in lua which returns the sum of two input integers, the function also calls c# method “Show” to print the result. There are three steps: 1. Init cle, create service group, and create service. 2. Define extends class of StarObjectClass, and defines a new function “Add” of it. 3. Create interface object, named “TestClass”.
import com.srplab.www.starcore.*;
//--Step2 : Define extends class of
StarObjectClass, and defines a new function “Add” of it.
class MyObjectClass extends StarObjectClass
{
public int Add(StarObjectClass self, int x, int y)
{
//--Callback
to script function
self._Call("Show", x + y);
return x + y;
}
public MyObjectClass(StarObjectClass srcobj)
{
super(srcobj);
}
}
public class AddFunctionService{
public
static void main(String[] args){
//--Step1 : Init cle, create service group,import sharelibrary, and create service
StarCoreFactory
starcore= StarCoreFactory.GetFactory();
StarServiceClass
Service=starcore._InitSimple("AddFunctionService","123",0,0);
//--Step3 : Create interface object, named “TestClass”.
MyObjectClass
Obj = new MyObjectClass(Service._New("TestClass"));
}
}
For c# code calls the above java code, there are four steps; 1. Init cle, create service group, import java file, and create service. 2. Define extends class of StarObjectClass, which implement callback function “Show”. 3. Create Instance of object “TestClass” defined in java file. 4. Call function “Add”.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using star_csharp;
namespace csharp_call
{
class MyObjectClass : StarObjectClass{
//--Step2
: Define extends class of StarObjectClass and implements callback function “Show”.
public void
Show( StarObjectClass self,int para )
{
Console.WriteLine("print from java call back "+para);
return;
}
public MyObjectClass(StarObjectClass
srcobj):base(srcobj){
}
}
class Program
{
static
void
{
//--Step1
: Init cle, create service
group,import java file, and create
service
StarCoreFactory
starcore= StarCoreFactory.GetFactory();
StarServiceClass
Service=starcore._InitSimple("test","123",0,0," AddFunctionService.lua?script=lua");
//--Step3 : Create Instance of
object “TestClass” defined in java.
MyObjectClass
a = new MyObjectClass( Service._GetObject("TestClass")._New() );
//--Step4
: call function “Add”.
Console.WriteLine("Add
returns : " + a._Call("Add", 12, 34));
starcore._ModuleExit();
}
}
}
|
| examples code download |
| Copywright (C) 2005 www.SRPLab.com(Star-River Platform Lab.) All Rights Reserved | Email:support@srplab.com |