编译器错误 CS1545
更新:2007 年 11 月
错误消息
属性、索引器或事件“property”不受该语言支持;请尝试直接调用访问器方法“set accessor”或“get accessor”
代码正在使用具有非默认索引器的对象并试图使用索引语法。若要解决此错误,请调用属性的 get 或 set 访问器方法。
示例
// CPP1545.cpp
// compile with: /clr /LD
// a Visual C++ program
using namespace System;
public ref struct Employee {
Employee( String^ s, int d ) {}
property String^ name {
String^ get() {
return nullptr;
}
}
};
public ref struct Manager {
property Employee^ Report [String^] {
Employee^ get(String^ s) {
return nullptr;
}
void set(String^ s, Employee^ e) {}
}
};
下面的示例生成 CS1545。
// CS1545.cs
// compile with: /r:CPP1545.dll
class x {
public static void Main() {
Manager Ed = new Manager();
Employee Bob = new Employee("Bob Smith", 12);
Ed.Report[ Bob.name ] = Bob; // CS1545
Ed.set_Report( Bob.name, Bob); // OK
}
}