none
TypeLoadException while trying to override abstract C# function in C++/CLI RRS feed

  • General discussion

  • I'm writing a ref class in C++/CLI, to be used in a C# project.  The ref class is derived from a class which is in a .NET third party library.  The C# class it is derived from is abstract, and has two abstract functions I'm required to override.  I had quite a bit of trouble trying to get the function signatures to match so the compiler would recognize them as overrides of the two abstract functions.  

    For instance, one of the two abstract functions looks like this in C# when I override it:
     
    public override SoVolumeReader.ReadErrors GetDataChar(out SbBox3f size, out SoDataSet.DataTypes type, out SbVec3i32 dim)
    
    

    ReadErrors and DataTypes are enums, SbBox3f and SbVec3i32 are structs.  After trying and trying to match the function in C++, I finally got it to compile with this:

    virtual SoVolumeReader::ReadErrors GetDataChar([Out] SbBox3f& size, [Out] SoDataSet::DataTypes& type, [Out] SbVec3i32& dim) override
    

    I was surprised that adding the ref operators to the parameter types made it compile, but I am relatively inexperienced with C++/CLI.  Once I got past the compilation errors, I used the ref class in my C# code.  However, when I ran the program, I got the following exception:

    An unhandled exception of type 'System.TypeLoadException' occurred in Viz.Scene.dll
    Additional information: Method 'GetDataChar' ...[assembly details]... does not have an implementation.

    This makes me think that even though the program compiles, I'm still not matching the functions correctly, but that's just a guess.  Anyone have any ideas what I may be doing wrong?
    Thursday, October 15, 2009 6:40 PM

All replies

  • Since these are all value types, you'll need to use a Tracking Reference [%] to handle the out parameters.

    virtual SoVolumeReader::ReadErrors GetDataChar(
        [Out] SbBox3f %size, 
        [Out] SoDataSet::DataTypes %type, 
        [Out] SbVec3i32 %dim) override
    


    Reed Copsey, Jr. - http://reedcopsey.com
    Thursday, October 15, 2009 7:01 PM
    Moderator
  • Hey Reed, thanks for the reply.  I'd actually tried using a tracking reference when I was first overriding the function.  I get the following warning when I do that (VolumeReader is the name of my ref class, and SoVolumeReader is the third party class from which it is being derived):

    warning C4490: 'override' : incorrect use of override specifier; 'Viz::Native::VolumeReader::GetDataChar' does not match a base ref class method.  'new' or 'override' are only allowed when a matching base method from a ref class exists
    
    warning C4570: 'Viz::Native::VolumeReader' : is not explicitly declared as abstract but has abstract functions.  'MC::LDM::Readers::SoVolumeReader::ReadErrors MC::LDM::Readers::SoVolumeReader::GetDataChar(MC::Inventor::SbBox3f&,MC::LDM::Nodes::SoDataSet::DataTypes &,MC::Inventor::SbVec3i32 &)' : is abstract
    If I disable these warnings as errors, I get this error whenever I try to use my ref class:

    error CS0144: Cannot create an instance of the abstract class or interface 'Viz.Native.VolumeReader'



    Thursday, October 15, 2009 7:18 PM
  • The C4570 warning you got definitely shows that you should be using &.  Which is pretty odd, it doesn't sound that this got started as a C# declaration.  I can't explain the exception.

    Hans Passant.
    Saturday, October 17, 2009 11:56 AM
    Moderator
  • Hello bozalina

    I'm writing to check the status of this issue on your side. Have you tried the tracking reference (%)? Do you still get any errors?

    Thanks
    Jialiang Ge
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, October 20, 2009 10:47 AM
    Moderator
  • Hi, Jialiang.  Yes, the tracking reference was one of the first fixes I tried, but it compiles with the warnings I mentioned in my response on October 15th.  If I pragma out the warnings, I get the TypeLoadException.

    The managed library we're using is actually a managed wrapper around a native library.  I'm guessing this has something to do with the origin of the bad function signature, but can't be certain.
    Friday, October 23, 2009 3:23 PM
  • Hello bozalina

    Is it convenient for you to make a simple and reproducible sample of this problem? It will be very helpful for me to dig into the issue if I can see it locally.

    Have a nice day!
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, October 28, 2009 5:49 AM
    Moderator
  • Hey Jialiang,

    I would love to be able to provide a sample, but unfortunately the third party library that provides the class I'm trying to override requires a license to run.

    The code I've provided so far is about as much as I can think to send.  Sorry!
    Friday, October 30, 2009 8:05 PM