簡體   English   中英

cli接口,其值類型為return和C#接口實現

[英]cli interface with value type as return and C# interface implementation

我有1.具有聲明的interface( InterfaceCLI )和實現的值類型( PointD )的CLI庫。2 .具有class( PointD )的C#庫,該類從1開始實現接口

問題是在C#上奇怪的接口實現。 它需要這樣的代碼public ValueType GetPoint()而不是public PointD GetPoint()

示例代碼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();
};

示例代碼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();
    }
     */
}

為什么要在Class1類中使用ValueType而不是PointD?

PointD^ GetPoint();

值類型不是引用類型。 所以,你應該在這里使用^帽子。 不幸的是,這種語法在C ++ / CLI中是允許的,它成為一個帶框的值。 非常浪費。 在C#中,除了使用您發現的ValueType進行模擬之外,沒有直接等效的方法。

取下帽子可以解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM