Stuck with “Cannot find type” error using CreateInstance()

淺唱寂寞╮ 提交于 2019-12-23 01:11:25

问题


I am trying to use .CreateInstance() in a connection utility in a C# project, while serializing XML. .Unwrap() is used to unwrap the serializable return type and get an instance of the type I'm trying to create.

String fileToLoad = @"D:\RPMOpen\svnCobra\conversion\aui\Model\bin\Debug\RPM_Model";
String file = Path.GetFileName(fileToLoad);
AbstractResponseMessageData response = 
    (AbstractResponseMessageData)Activator.CreateInstance(file, responseName).Unwrap();

My assembly RPM_Model at @"D:\RPMOpen\svnCobra\conversion\aui\Model\bin\Debug\RPM_Model" loads fine, but as a result of the type ARC_LOGONRS in responseName I get the following error:

Error in method ConnectUtil.Execute: Could not load type ARC_LOGONRS from assembly RPM_Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.

Now, this is why I am stuck on the error:

1. I did reference the assembly specified in the error.
2. There is not anything in my GAC overriding the .dll in my /Debug folder.
3. I have ensured that I have the correct version of the .dll.
4. The .dll has no other dependencies.
5. Both my project and the .dll have the same target platform.
6. I know that I am referencing the assembly properly, as if I place ARCLOGON_RS in my code and hover over it I see

class RPM.Model.Data.ARCLOGON_RS

and I can f12 focus on it to see it's properties.
7. I have cleaned and rebuilt both solutions.
8. The type does have a public default constructor, and the class is public.
9. The .dll is in the reference node.
10. I do have the right Assembly Name passed as a parameter.

My question is if anyone has any other idea as to why my project may be throwing this error, and how I may go about fixing it, as I have completely run out of diagnoses.

Many thanks for any help.


回答1:


When supplying the typeName to CreateInstance() I had to hardcode the full type. Despite my error looking as though it is looking within RPM.Model.Data as I would like it to, it apparently wasn't. Editing responseName to be RPM.Model.Data.ARC_LOGONRS instead of simply ARC_LOGONRS was all that was necessary to overcome the error.



来源:https://stackoverflow.com/questions/28684666/stuck-with-cannot-find-type-error-using-createinstance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!