我有一个带有构造函数的托管c类/结构,它接受输入.在C#中,我只能“看到”默认构造函数.有没有办法调用其他构造函数,而无需离开托管代码?谢谢.
编辑:事实上,它的所有功能都不可见.
C :
public class Vector4
{
private:
Vector4_CPP test ;
Vector4(Vector4_CPP* value)
{
this->test = *value;
}
public:
Vector4(Vector4* value)
{
test = value->test;
}
public:
Vector4(float x, float y, float z, float w)
{
test = Vector4_CPP( x, y, z, w ) ;
}
Vector4 operator *(Vector4 * b)
{
Vector4_CPP r = this->test * &(b->test) ;
return Vector4( &r ) ;
}
} ;
C#:
// C# tells me it can't find the constructor.
// Also, none of them are visible in intellisense.
Library.Vector4 a = new Library.Vector4(1, 1, 1, 1);
最佳答案 第一个问题是您的类声明是针对非托管C对象的.
如果需要托管C/C++LI对象,则需要以下其中一项:
public value struct Vector4
要么
public ref class Vector4
此外,任何包含本机类型的C/C++LI函数签名都不会对C#可见.因此,任何参数或返回值都必须是C/C++LI托管类型或.NET类型.我不确定操作符*签名的外观,但你可以像这样休息:
public value struct Vector4
{
private:
Vector4_CPP test;
Vector4(Vector4_CPP* value)
{
this->test = *value;
}
public:
Vector4(Vector4 value)
{
test = value.test;
}
Vector4(System::Single x, System::Single y, System::Single z, System::Single w)
{
test = Vector4_CPP( x, y, z, w ) ;
}
}
要么:
public ref class Vector4
{
private:
Vector4_CPP test;
Vector4(Vector4_CPP* value)
{
this->test = *value;
}
public:
Vector4(Vector4^ value)
{
test = value->test;
}
Vector4(System::Single x, System::Single y, System::Single z, System::Single w)
{
test = Vector4_CPP( x, y, z, w ) ;
}
}