MethodInfo.Invoke方法出现TargetInvocationException异常问题。。。

wuhongyao3 2008-02-19 09:21:40
现有一程序
我做了两个类库ClassLibrary1,ClassLibrary2
其中ClassLibrary2引用ClassLibrary1,并使用ClassLibrary1中的方法。。

然后我建立一程序,将ClassLibrary2添加到程序中
程序没有添加对ClassLibrary1的引用
Dim b() As Byte = resource.GetObject("ClassLibrary2")
ass = System.Reflection.Assembly.Load(b)
Dim mytype As Type = ass.GetType("ClassLibrary2.Class1")
Dim Method As MethodInfo = mytype.GetMethod("Method2")
Dim obj As Object = ass.CreateInstance("ClassLibrary2.Class1")
Dim result As String = Method.Invoke(obj, Nothing).ToString()//这句出现异常(因为method方法需要调用类库1中的方法)

程序执行错误

我如果将类库1的引用添加进来,则程序可以运行

很明显 程序异常是因为没有添加对类库1的引用

我现在就是向问一下,在添加类库2的时候,能不能自己包涵对类库1的引用。。

望高手进来解答一下,我在线等。。。
...全文
1144 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuhongyao3 2008-02-19
  • 打赏
  • 举报
回复
哦,这样啊
谢谢你了
lake_cx
结贴了。。
看看怎么把分送你,呵呵,我刚进来的。。。
lake_cx 2008-02-19
  • 打赏
  • 举报
回复
环境路径呗,exe同级目录下也行,或者在exe的配置文件中的<configuration>节点加上
<runtime>
<gcConcurrent enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<publisherPolicy apply="yes" />
<probing privatePath="这个是你的相对路径(exe的)" />
</assemblyBinding>
</runtime>
wuhongyao3 2008-02-19
  • 打赏
  • 举报
回复

那意思是 我必须吧这个classlibrary1存放到当前程序中,他的自动处理机制就是再当前程序中查找
我吧classlibrary1放到当前程序中可以使用,原来还可以用呢,刚才又不可以用了

那我需要将这个类库存放到什么位置呢。。 它找得着的位置是什么地方。。。
谢谢。。
lake_cx 2008-02-19
  • 打赏
  • 举报
回复
有自动处理的机制,但是你要把它放到它找得到的位置,如果ClassLibrary1在另一台机器上,你叫它怎么给你找
wuhongyao3 2008-02-19
  • 打赏
  • 举报
回复
ClassLibrary1没有
我刚才试了试程序,如果将ClassLibrary1添加到项目中,则程序会自动再当前程序中查找ClassLibrary1,程序运行不会有问题



如果没有Classlibrary1
Catch ex As TargetInvocationException
Throw ex.InnerException
End Try
错误信息
File notfound exception
Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

我想在就是想问一下 vs中有没有自动处理的机制,将ClassLibrary2中涉及的类库,自动给包涵到里边()。。
我感觉是不可能的,如果这样的话,我如果classlibrary2调用很多其他类库,那classlibrary2则会很大

解决方法是不是就是将classlibray2中调用的类库,要事先加入到程序中。。

还是有其他的解决方法呢。。。
lake_cx 2008-02-19
  • 打赏
  • 举报
回复
ClassLibrary2是通过WebService远程传过来的?
那ClassLibrary1呢?
wuhongyao3 2008-02-19
  • 打赏
  • 举报
回复
谁知道该怎么解决呢,急等回复,谢谢。。
wuhongyao3 2008-02-19
  • 打赏
  • 举报
回复
当前项目中根本就没有这个classlibiry1,只有classlibiary2(添加了对classlibiary1的引用,需要调用classlibiary1中的方法)


程序是这样的
我是将这两个类库存放到WEB SERVICES中
然后,我将类库2通过资源管理器,返回一个BYTE数组

web services中的方法是
Dim ass As Assembly = Assembly.GetExecutingAssembly
Dim resource As Resources.ResourceManager = New Resources.ResourceManager("WebService2.Resources", ass)
Dim b() As Byte = resource.GetObject("ClassLibrary2")
Return b

然后程序中通过反射调用这个byte数组的内容方法。。
ass = System.Reflection.Assembly.Load(b) /b就是返回的那个byte数组
Dim mytype As Type = ass.GetType("ClassLibrary2.Class1")
Dim Method As MethodInfo = mytype.GetMethod("Method2")
Dim obj As Object = ass.CreateInstance("ClassLibrary2.Class1")
Dim result As String = Method.Invoke(obj, Nothing).ToString()//这句出现异常(因为method方法需要调用类库1中的方法)

整个程序是这样的

如果您有时间的话,请指教一下,谢谢。。
lake_cx 2008-02-19
  • 打赏
  • 举报
回复
ClassLibrary1是不是跟exe在一个目录?
wuhongyao3 2008-02-19
  • 打赏
  • 举报
回复
More Information

System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. ---> System.IO.FileNotFoundException:
Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' or one of its dependencies.
The system cannot find the file specified.
File name: 'ClassLibrary1, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'
at ClassLibrary2.Class1..ctor()

这是更多提示。。
谁能给说一下呢。。。

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧