-->

Stuck with “Cannot find type” error using CreateIn

2019-06-04 08:34发布

问题:

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.