问题描述
我正在试图找出如何动态加载程序集并获得
an IClientPlugin接口到一个类。在执行Activator.CreateInstance之后,我可以获得一个对象
引用它。打电话,但是
有没有办法获得IClientPlugin参考?
尝试做一个CType(obj,IClientPlugin)抛出异常。
这里是加载程序集的代码,并调用一个名为
" GetPluginName"的方法。没有参数并返回一个字符串:
////////////////////开始代码/////// //////////////////////
Dim szPluginPath As String
Dim objPluginAssembly As System.Reflection.Assembly
Dim objIClientPlugin As IClientPlugin
Dim obj As Object
Dim objResult As Object
Dim szError As String
Dim T作为类型
Dim T2作为类型
szPluginPath =" C:\ PLUGINS \\ANET_SystemNews.dll"
objPluginAssembly = System.Reflection.Assembly.LoadFile(szPluginPath)
每个T in objPluginAssembly.GetTypes()
如果T.IsClass()= True则
''类实现IClientPlugin接口?
如果不是IsNothing(T。 GetInterface(" IClientPlugin"))然后
''是的,所以创建这个类的实例
obj = Activator.CreateInstance(T)
''**** objPlug in = CType(obj,IClientPlugin)
''调用IClientPlugin.GetPluginName()
objResult = T.InvokeMember(" GetPluginName",_
Reflection.BindingFlags.Default或_
Reflection.BindingFlags.InvokeMethod,_
没什么,_
obj,_
没什么)
Debug.WriteLine(" Result:{0}",objResult)
结束如果
结束如果
下一页
///////////////// ///结束代码/////////////////////////////
我已经注释掉导致错误的行,CType(obj,
IClientPlugin)调用。以下是客户端的实现方式:
公共类AppleNET_BBS_PLUGIN
实现IClientPlugin
。 ..定义IClientPlugin函数...
结束类
有什么方法可以得到IClientPlugin。参考
实例化的类?调用方法会更容易:
Dim objIClientPlugin作为IClientPlugin
..通过反射创建类
objIClientPlugin = ????(obj)
感谢您提供的任何帮助!
// CHRIS
Hi,
I''m trying to figure out how to dynamically load an assembly and get
an IClientPlugin interface to a class. I can get an "Object"
reference to it after doing an "Activator.CreateInstance" call, but is
there any way to get an IClientPlugin reference to it?
Trying to do a "CType(obj, IClientPlugin)" throws an exception.
Here''s the code that loads the assembly and calls a method called
"GetPluginName" that takes no parameters and returns a string:
//////////////////// BEGIN CODE /////////////////////////////
Dim szPluginPath As String
Dim objPluginAssembly As System.Reflection.Assembly
Dim objIClientPlugin As IClientPlugin
Dim obj As Object
Dim objResult As Object
Dim szError As String
Dim T As Type
Dim T2 As Type
szPluginPath = "C:\PLUGINS\\ANET_SystemNews.dll"
objPluginAssembly = System.Reflection.Assembly.LoadFile(szPluginPath)
For Each T In objPluginAssembly.GetTypes()
If T.IsClass() = True Then
''Class implements IClientPlugin interface?
If Not IsNothing(T.GetInterface("IClientPlugin")) Then
''YES, so create an instance of this class
obj = Activator.CreateInstance(T)
'' **** objPlugin = CType(obj, IClientPlugin)
''Invoke IClientPlugin.GetPluginName()
objResult = T.InvokeMember("GetPluginName", _
Reflection.BindingFlags.Default Or _
Reflection.BindingFlags.InvokeMethod, _
Nothing, _
obj, _
Nothing)
Debug.WriteLine("Result: {0}", objResult)
End If
End If
Next
//////////////////// END CODE /////////////////////////////
I''ve commented out the line that causes an error, the CType(obj,
IClientPlugin) call. Here is how the client is implemented:
Public Class AppleNET_BBS_PLUGIN
Implements IClientPlugin
... IClientPlugin functions defined ...
End Class
Is there any way I can get an "IClientPlugin" reference to the
instantiated class? It would make it a lot easier to call methods:
Dim objIClientPlugin As IClientPlugin
.. create class through reflection
objIClientPlugin = ????(obj)
Thanks for any help you can offer!
// CHRIS
推荐答案
您应该能够转换为接口类型。请发布异常
消息。
You should be able to cast to the interface type. Please post exception
message.
obj = Activator.CreateInstance(T)
Debug.WriteLine(obj.GetType()。ToString())
''此行失败:
objPlugin = CType(obj,IClientPlugin)
抛出InvalidCastException消息
指定演员表无效。但obj是我班级的一个实例
,名为AppleNET_BBS_PLUGIN。它实现了IClientPlugin。
obj.GetType()。ToString()返回" ANET_SystemNews.AppleNET_BBS_PLUGIN"
哪个是正确的类,一切:
公共类AppleNET_BBS_PLUGIN
实现IClientPlugin
..此处定义的函数
结束课
有什么想法吗?我想我会创建一组新的项目来尝试和工作
这个..如果它不起作用,也许有人可以看看它(?)
谢谢,
// CHRIS
obj = Activator.CreateInstance(T)
Debug.WriteLine(obj.GetType().ToString())
''THIS LINE FAILS:
objPlugin = CType(obj, IClientPlugin)
It throws an "InvalidCastException" with the message
"Specified cast is not valid." But obj is an instance of my class
called "AppleNET_BBS_PLUGIN" which implements IClientPlugin.
obj.GetType().ToString() returns "ANET_SystemNews.AppleNET_BBS_PLUGIN"
Which is the right class and everything:
Public Class AppleNET_BBS_PLUGIN
Implements IClientPlugin
.. functions defined here
End Class
Any ideas? I think I''ll create a new set of projects to try and work
this out.. If it doesn''t work, maybe someone can take a look at it (?)
Thanks,
// CHRIS
说..我想知道是否重要的是我试图使用的IClientPlugin接口是否只是在Interfaces.vb中定义的。两个项目使用的文件
。除了名称之外。界面,.NET
怎么知道它们真的是同一个界面?除非它完成类型比较
..不确定...
尝试共享界面时有什么特别的事吗
两个项目的
定义?
谢谢,
// CHRIS
Say.. I''m wondering if it matters that the IClientPlugin interface I
am trying to use is simply defined in an "Interfaces.vb" file that
both projects use. Besides the "name" of the interface, how can .NET
know that they are really the same interface? Unless it does a
complete type comparison.. Not sure about that..
Is there anything special to do when trying to share interface
definitions across 2 projects?
Thanks,
// CHRIS
这篇关于反射:支持已知接口的类的CreateInstance(Type) - 获取对象的实际接口引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!