.net反射技术的应用—如何调用Java的COM接口

        前两天做了一个银行的电子支付接口,我用的是C#开发语言,银行给我们的是Java的COM组件(名字是:RSASig.class),按照银行的说明,一步一步做了配置,大家都知道Java的COM在windows上注册之后是没有动态链接库的,那该怎么在C#中调用其中的方法呢?如果用过.Net反射技术的朋友们很快就会想到使用反射。下面是C#实现反射代码:
 1 None.gif Assembly assembly  =   null ;
 2 None.gifType type  =   null ;
 3 None.gif string  dllPath  =   @" c:\Reflected.dll "
 4 None.gif try
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    assembly = Assembly.LoadFile(dllPath);
 7InBlock.gif    type = assembly.GetType(typeName,true,true);//cMyResult
 8ExpandedBlockEnd.gif}

 9 None.gif catch (FileNotFoundException)
10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
11InBlock.gif    errMsg = "Could not load Assembly: \""+ dllPath +"\"";
12InBlock.gif    return null;
13ExpandedBlockEnd.gif}

14 None.gif catch (TypeLoadException)
15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
16InBlock.gif    errMsg = "Could not load Type: \"string\" \n from assembly: \"" + dllPath + "\"";
17InBlock.gif    return null;
18ExpandedBlockEnd.gif}

19 None.gif
20 None.gifMethodInfo method  =  type.GetMethod(methodName); // 提取方法信息
21 None.gif object  obj  =  assembly.CreateInstance(typeName); // 创建实例
22 None.gif
23 None.gif object  s  =  method.Invoke(obj,parameters); // new object[]{"jiangli"});
24 None.gif assembly  =   null ;
25 None.giftype  =   null ;
26 None.gifmethod  = null ;
27 None.gif


        从这段代码中可以看出,这是需要动态链接库的。而Java却没有,怎么用呢?这是摆在面前的一个问题。再仔细看看他的说明,他们在ASP中是这么调用的,Set Obj = Server.CreateObject("ccb.pub.RSAsig"),灵机一动,在VB中是不是也可以这么用呢?马上就就在VB中作了一个实验,结果成功了,VB中的代码是这样实现的:

ExpandedBlockStart.gif ContractedBlock.gif Namespace test Namespace test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Class cJavaClass cJava  
InBlock.gif        
Dim obj As Object = Nothing
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Sub New()Sub New()
InBlock.gif            obj 
= GetObject("java:RSASig")
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function generateKeys()Function generateKeys() As Object
InBlock.gif            
Return obj.generateKeys()
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function getPrivateKey()Function getPrivateKey() As String
InBlock.gif            
Return obj.getPrivateKey()
ExpandedSubBlockEnd.gif        
End Function

ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function getPublicKey()Function getPublicKey() As String
InBlock.gif            
Return obj.getPublicKey()
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif        
'签名结果信息
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Function generateSigature()Function generateSigature(ByVal singstr As StringAs String
InBlock.gif            
Return obj.generateSigature(singstr)
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function setPublicKey()Function setPublicKey(ByVal pubKey As StringAs String
InBlock.gif            
Return obj.setPublicKey(pubKey)
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function verifySigature()Function verifySigature(ByVal orginstr As StringByVal sigStr As StringAs Object
InBlock.gif            
Return obj.verifySigature(sigStr, orginstr)
ExpandedSubBlockEnd.gif        
End Function

ExpandedSubBlockEnd.gif    
End Class

ExpandedBlockEnd.gif
End Namespace


        于是就把它封装成类库,然后在C#中调用,OK,成功了。苦苦寻找了一个星期.net怎么调用Java类中的方法,终于在此画上了一个句号。

        不知道这算不算是.net和java互操作的一个案例。希望这篇文章能对还正在苦苦寻觅.net和java互操作的朋友有点启发。

        如果各位大虾还有别的更简便的方法,请指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值