Deploy (POCO) property of struct property in C #

I want to implement an interface from my C ++ / CLI dll in C #. But I am having problems optimizing the return value in C ++. Consider

// BVHTree.cpp: public value struct Vector3 { float X, Y, Z; }; public value struct TriangleWithNormal { Vector3 A, B, C, Normal; }; public interface class IBVHNode { property TriangleWithNormal Triangle { TriangleWithNormal get(); } // among others property bool IsLeaf { bool get(); } // can implement this }; 

 // BVHNode.cs: public class BVHNode : IBVHNode // Error: member TriangleWithNormal* IBVHNode.get_Triangle(TriangleWithNormal*) not implemented (sth. along those lines) { public TriangleWithNormal Triangle { get { return new TriangleWithNormal(); } } public bool IsLeaf { get { return true; } } } 

He complains that BVNode did not implement IBVHNode . My last resort would be to access it using the usual method or to use unsafe mode, such as visual studio:

 public TriangleWithNormal* get_Triangle(TriangleWithNormal* t) { throw new NotImplementedException(); } 

Is it possible to implement it in the syntax of properties (besides creating a TriangleWithNormal a ref class ...)?

Update 1 It seems that the execution of the TriangleWithNormal GetTriangle() method occurs for the same reasons. However, you can use it as void GetTriangle(TriangleWithNormal%); .

+4
source share
2 answers

It turned out that the error disappeared after I lived through several assembly cycles using the void GetTriangle(TriangleWithNormal%); . Whether it is a miracle or rather a quiet, clean assembly, commenting on an erroneous property trying to reproduce the error condition again, it is made up of a box.

0
source

Your code worked for me as soon as I fixed a couple of minor syntax errors ( ; after class declarations in C ++ / CLI, skipping the word bool as the returned type of IsLeaf.get() ) and defined the type Vector3,

I figured out how you defined Vector3, this is not a standard class that I'm familiar with. Where is this defined? Is it a managed class, a managed structure, or an unmanaged? (I defined it as public value struct Vector3 { double x, y, z; }; for my test.)

As I said, there are minor syntax errors in the C ++ / CLI code that you posted here. These two errors give very clear compilation errors, so I assume these are typos in transcription from Visual Studio on the Internet. Are there any other changes between what you posted and the actual code?


Also, I was not able to get the error message that you reported, member TriangleWithNormal* IBVHNode.get_Triangle(TriangleWithNormal*) not implemented . I always get compilation error CS0535: 'CSharpTest.BVHNode' does not implement interface member 'CppCLITest.IBVHNode.Triangle' .

The property you showed does not accept a parameter, but a compiler error shows the get_Triangle method, which accepts a parameter of type TriangleWithNormal* . Do you have another property declaration somewhere or an explicit declaration of this method?


I think that is possible. If I try to declare a C ++ / CLI property as an indexed property, then I get a method signature similar to what you see.

In your actual code, you have something like this:

 property TriangleWithNormal Triangle[TriangleWithNormal] { TriangleWithNormal get(TriangleWithNormal input); } 

This is an indexed property that C # does not allow to implement. (C # allows one indexed property, this[] , but only one.)

When I try to implement this in C #, I need to explicitly implement the property support method, and not implement it as a C # property.

 public TriangleWithNormal get_Triangle(TriangleWithNormal input) { return new TriangleWithNormal(); } 

Make the indexed property regular, and this should work for you.

+2
source

Source: https://habr.com/ru/post/1440405/


All Articles