值类型为返回和 C# 接口实现的 cli 接口
本文关键字:接口 实现 cli 类型 返回 | 更新日期: 2023-09-27 18:32:10
我有1. 具有声明接口(接口CLI)和实现值类型(PointD)的CLI库2. 带有类(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 模拟它之外。
摘下帽子来解决您的问题。