TargetException异常的处理

问题描述

我是C#的初学者。下面代码是我复制自MSDN中的。运行时,在“Objectret=m.Invoke(o,newObject[]{42});”处有错误提示:未处理的TargetException:非静态方法需要一个目标这个问题,怎么解决?谢谢usingSystem;usingSystem.Configuration;usingSystem.Security.Permissions;usingSystem.Reflection;namespaceConsoleApplication1{[assembly:AssemblyVersionAttribute("1.0.2000.0")]publicclassExample{privateintfactor;publicExample(intf){factor=f;}publicintSampleMethod(intx){Console.WriteLine("nExample.SampleMethod({0})executes.",x);returnx*factor;}publicstaticvoidMain(){Assemblyassem=Assembly.GetExecutingAssembly();Console.WriteLine("AssemblyFullName:");Console.WriteLine(assem.FullName);//TheAssemblyNametypecanbeusedtoparsethefullname.AssemblyNameassemName=assem.GetName();Console.WriteLine("nName:{0}",assemName.Name);Console.WriteLine("Version:{0}.{1}",assemName.Version.Major,assemName.Version.Minor);Console.WriteLine("nAssemblyCodeBase:");Console.WriteLine(assem.CodeBase);//Createanobjectfromtheassembly,passinginthecorrectnumber//andtypeofargumentsfortheconstructor.Objecto=assem.CreateInstance("Example",false,BindingFlags.ExactBinding,null,newObject[]{2},null,null);//Makealate-boundcalltoaninstancemethodoftheobject.MethodInfom=assem.GetType("ConsoleApplication1.Example").GetMethod("SampleMethod");Objectret=m.Invoke(o,newObject[]{42});//出错的地方Console.WriteLine("SampleMethodreturned{0}.",ret);Console.WriteLine("nAssemblyentrypoint:");Console.WriteLine(assem.EntryPoint);Console.ReadLine();}}}

解决方案

解决方案二:
Objecto=assem.CreateInstance("Example",false,你的o是null吧,没有创建成功
解决方案三:
Objecto=assem.CreateInstance("Example",false,BindingFlags.ExactBinding,null,newObject[]{2},null,null);Objecto=assem.CreateInstance("ConsoleApplication1.Example",false,BindingFlags.ExactBinding,null,newObject[]{2},null,null);你通过反射创建对象的时候,没有成功,返回的是Null,原因是你的typeName错了.要写全,谢谢.切记.
解决方案四:
本帖最后由 bdmh 于 2012-03-12 10:29:43 编辑

时间: 2024-09-20 00:20:10

TargetException异常的处理的相关文章

C#将老对象赋值新对象时更改老对象的问题解决

通过这个简单的例子引出我所出现的问题: int j; j = 1; int k; k = j; k = 2; Console.WriteLine(string.Format("i={0},k={1}", j, k)); 代码很简单,输出的结果也很容易看出来,结果当然输入的是:i=1,k=2 这样的输出结果当然无需置疑,那么我们现在按照这个逻辑进入下面的代码,结果除非人的意料.  首先是一个实体对象类: namespace ConsoleApplication {     /// <

websphere-IBM WebSphere6.1 停止服务时出现异常

问题描述 IBM WebSphere6.1 停止服务时出现异常 Current trace specification = =info:com.ibm.=all ************* End Display Current Environment ************* [13-6-5 14:43:34:109 CST] 0000000a BundleResolve 3 found resource bundle com.ibm.ejs.resources.RasMessages in

process-Widget启动报异常。请求解决

问题描述 Widget启动报异常.请求解决 Unable to launch app or broadcast Intent process is bad act=android.appwidget.action.APPWIDGET_ENABLED 怎么回事啊!!! 开始还好好的,后来不晓得怎么了,装widget一直报这个错,不晓得抽什么风, 网上说神马卸掉重装,卸掉关机重启重装,统统都不行.... 解决方案 http://stackoverflow.com/questions/3253676/

处理The content of the adapter has changed but ListView did not receive a notification异常

istView在更新时报异常: ERROR/AndroidRuntime(15260): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only

ef 通过DbCommandInterceptor 修改数据库连接 添加数据异常

问题描述 ef 通过DbCommandInterceptor 修改数据库连接 添加数据异常 我想实现数据库的读写分离 ef6里面加了DbCommandInterceptor的功能可是修改以后 在添加新数据的时候 抛出异常 "基础提供程序在 Commit 上失败 值不能为 null. 参数名: connection"但是数据已经成功添加那位前辈读到过这个问题呢 请指导 public class EntityCommandInterceptor: DbCommandInterceptor

tomcat启动的问题,报异常

问题描述 tomcat启动的问题,报异常 解决方案 和tomcat没关系.是你web.xml中的配置的servlet加载的时候报错了 解决方案二: 采用flex+struts+spring+hibernate框架,启动tomcat出现异常,寻求解决方法中..... 严重: StandardWrapper.Throwablejava.lang.NullPointerException?at flex.management.BaseControl.getObjectName(BaseControl.

tomcat启动报错-eclipse 启动 tomcat 异常

问题描述 eclipse 启动 tomcat 异常 tomcat: apache-tomcat-7.0.54-windows-x86 绿色版 eclipse: eclipse-jee-luna-R-win32 . eclipse-jee-indigo-SR2-win32 直接在 ${TOMCAT_HOME}/bin 下双击 startup.bat 可以正常启动,无任何问题, 但是在 eclipse 中配置好之后,加入web工程,启动tomcat,就出现了下面的错误信息: Error occurr

myeclipse配置了weblogic,启动服务报异常

问题描述 myeclipse配置了weblogic,启动服务报异常 1C 解决方案 认证没有通过,估计是myeclipse配置weblogic,忘了设置 解决方案二: weblogic用户名密码不对~

使用osgi api启动osgi时,用java -jar命令运行报异常,用eclipse运行正常

问题描述 使用osgi api启动osgi时,用java -jar命令运行报异常,用eclipse运行正常 我使用osgi的api写了osgi框架的启动程序,然后使用java -jar 命令运行这个jar包报空指针异常,我在eclipse中运行一切正常,请问这是为什么呀