网站建设的需要是什么,做云教育集群网站,手机网站开发环境搭建,萌宝宝投票网站怎么做已知#xff0c;子类B继承自父类A#xff0c;但是在代码运行时#xff0c;B类强制转换为A类#xff0c;却报代码转换异常。 很奇怪的问题吧#xff0c;不过这个也是难得机会#xff0c;去研究C#运行的底层原理。
下面是报错的代码片段。
string className _shapeRefle…已知子类B继承自父类A但是在代码运行时B类强制转换为A类却报代码转换异常。 很奇怪的问题吧不过这个也是难得机会去研究C#运行的底层原理。
下面是报错的代码片段。
string className _shapeReflectMap[typeName].ClassName;
Assembly assem _shapeReflectMap[typeName].Assem;
Object obj assem.CreateInstance(className); // 在dll程序集中 通过className实例化获取子类
Type type1 obj.GetType().BaseType; // 获取父类类型
Type type2 typeof(Shape);
Assembly assembly1 type1.Assembly;
Assembly assembly2 type2.Assembly;
string codeBase1 assembly1.CodeBase;
string codeBase2 assembly2.CodeBase;
try
{shape (Shape)obj;
}
catch (Exception e)
{throw new Exception(反射创建Shape失败 \n类型直接比较: (type1 type2) \n程序集直接比较: (assembly1 assembly2) \n类型全名比较: (type1.FullName type2.FullName) : type1.FullName type2.FullName \n程序集全名比较: (assembly1.FullName assembly2.FullName) : assembly1.FullName assembly2.FullName \ncodeBase1: codeBase1 \ncodeBase2: codeBase2 \n程序集路径比较: (codeBase1 codeBase2) \ncodeBase1.hash: codeBase1.GetHashCode() \ncodeBase2.hash: codeBase2.GetHashCode() \n, e);
}下面是报错结果其中的敏感字符串被替换成了xxx
System.Exception: 反射创建Shape失败
类型直接比较: False
程序集直接比较: False
类型全名比较: True: xxx.WpfPlugin.Shape xxx.WpfPlugin.Shape
程序集全名比较: True: xxx.WpfPlugin, Version1.0.0.0, Cultureneutral, PublicKeyTokennull xxx.WpfPlugin, Version1.0.0.0, Cultureneutral, PublicKeyTokennull
codeBase1: file:///D:/project/XXX/xxx.xxxClient.UI/xxx.xxxClient.UI/bin/Debug/Plugins/xxx.WpfPlugin.dll
codeBase2: file:///D:/project/XXX/xxx.xxxClient.UI/xxx.xxxClient.UI/bin/Debug/Plugins/xxx.WpfPlugin.dll
程序集路径比较: True
codeBase1.hash: -336973287
codeBase2.hash: -336973287--- System.InvalidCastException: 无法将类型为“xxx.WpfPlugin.Shapes.ImageButton”的对象强制转换为类型“xxx.WpfPlugin.Shape”。在 xxx.WpfPlugin.ctlUI.LoadXml(String xmlPath) 位置 D:\project\XXX\dll\PluginsSources\xxx.WpfPlugin\ctlUI.cs:行号 107--- 内部异常堆栈跟踪的结尾 ---在 xxx.WpfPlugin.ctlUI.LoadXml(String xmlPath) 位置 D:\project\XXX\dll\PluginsSources\xxx.WpfPlugin\ctlUI.cs:行号 111在 xxx.WpfPlugin.ctlUI.DisplayInit() 位置 D:\project\XXX\dll\PluginsSources\xxx.WpfPlugin\ctlUI.cs:行号 177通过上面的代码可以看出从子类中获取的父类type和父类直接获取的type是完全一样的命名空间类名称程序集和对应的dll文件均相同。但是通过判断其在内存中并非同一个对象。
经过排查发现代码中对该dll加载了两次获得了两个程序集而子类和父类分别来自不同的程序集导致了无法进行类型转换。在修复该dll加载逻辑后问题便得到了解决。
可以推测出C#判断两个类是否完全相同除了看命名空间和类名以外主要是判断两个类是否在同一个程序集实体中内存中的同一个实体。若一个dll加载了两遍获得两个程序集对象虽然两个程序集中的类完全相同但是依然无法相互转换。
所以程序集最好有一个公共的存放处统一的加载逻辑不要养成需要某个类时直接去加载一遍dll的坏习惯。 也可以通过Assembly.GetExecutingAssembly()直接获取当前代码所在的程序集避免重复加载。 文章转载自: http://www.morning.pwmm.cn.gov.cn.pwmm.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.nggbf.cn.gov.cn.nggbf.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.jcbmm.cn.gov.cn.jcbmm.cn http://www.morning.kgxrq.cn.gov.cn.kgxrq.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.zfwjh.cn.gov.cn.zfwjh.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.nbrdx.cn.gov.cn.nbrdx.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.rkrl.cn.gov.cn.rkrl.cn http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.yrbq.cn.gov.cn.yrbq.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.kpzrf.cn.gov.cn.kpzrf.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.mnwb.cn.gov.cn.mnwb.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.lhxkl.cn.gov.cn.lhxkl.cn http://www.morning.xxwfq.cn.gov.cn.xxwfq.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.bqppr.cn.gov.cn.bqppr.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.glxmf.cn.gov.cn.glxmf.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn