Domanda

I have 1. CLI library with declared interface(InterfaceCLI) and implemented value type(PointD) 2. C# library with class(PointD) that implements interface from 1

The problem is strange interface implementation on C#. It requires such code public ValueType GetPoint() intstead of public PointD GetPoint()

Sample code CLI:

public value struct PointD
//public ref class PointD
{
public:
    PointD(double x, double y);
    // Some stuff
};

public interface class InterfaceCLI
{
public:
    double Foo();
    PointD^ GetPoint();
};

Sample code C#:

public class Class1 : InterfaceCLI
{
    public double Foo()
    {
        PointD x=new PointD( 1.0 , 2.7 );
        return x.Y;
    }

    public ValueType GetPoint()
    {
        throw new NotImplementedException();
    }

    /*
    public PointD GetPoint()
    {
        throw new NotImplementedException();
    }
     */
}

Why does it want ValueType instead of PointD in the class Class1?!

È stato utile?

Soluzione

PointD^ GetPoint();

Value types are not reference types. So you should not use the ^ hat here. Unfortunately this syntax is permitted in C++/CLI, it becomes a boxed value. Very wasteful. There's no direct equivalent for that in C#, beyond emulating it with ValueType as you found out.

Remove the hat to solve your problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top