2013-03-12 53 views
3

有两个项目,一个C++ CLI与另一个C#项目。
在C#项目中引用C++ CLI程序集。为什么C++ CLI索引属性在C#中不起作用?

一切都很好,除了索引属性不起作用。
C++ CLI代码:

property Nullable< int> PVarInt[System::String^] { 
    Nullable<int> get(System::String^ inx){ 
    } 
    void set(System::String^ inx, Nullable< int> newx){ 
    } 
} 

在C#中该代码显示为两个set和get方法类似如下:

get_PVarInt(..) 
set_PVarInt(..) 

这是一个错误?有没有解决这个问题的方法?为什么发生这一切?

+0

>使用该主叫机制,但很少不安全。 System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName =“App.exe”; process.StartInfo.Arguments =“arg1 arg2 arg3”;的Process.Start(); – XEENA 2013-03-12 11:04:04

+0

我写了一篇关于在将VB.NET应用程序转换为C#时创建的类的博客文章,它可能对您有用。 [PropertyWrapper](http://4a47.blogspot.se/2013/03/propertywrapper.html) – 2013-03-12 12:17:24

+0

@JensGranlund有用,谢谢 – 2013-03-12 12:28:25

回答

2

C++/CLI已索引的属性,这将是可从C#: 示例:

public ref class ClassWithIndexer 
{ 
private: 
    array<int> ^x; 
public: 
    property int default[int] 
    { 
     int get(int index) 
     { 
      return x[index]; 
     } 
     void set(int index, int value) 
     { 
      x[index] = value; 
     } 
    } 
}; 
+0

感谢您使用这种方式。我已经将我的属性移植到类中。 – 2013-03-12 14:41:52

3

C#不支持索引属性,除了this属性。

然而,.NET框架的确如此,所以当用支持它的语言创建索引属性时,它会在C#中变成一个get_Method和一个。

+0

有没有解决方案来解决这个问题? – 2013-03-12 10:31:31

+3

@Mahdi,这取决于你的意思是修复。只要您正在访问由另一种语言编号创建的C#中的索引属性。但是你可以通过用一个对象创建一个属性并使用索引该属性的对象来伪造C#中的索引属性。 – 2013-03-12 10:37:06

相关问题