事件声明和 FieldOffsetAttribute 使用
我有一个使用 Attribute StructLayout 属性的 CLR 类:
[StructLayout(LayoutKind::Explicit)]
public ref class Matrix4 : System::ComponentModel::INotifyPropertyChanged
所有字段都使用 FieldOffset 属性。现在我需要添加一个事件,特别是我想实现 INotifyPropertyChanged 接口,因此我需要该
[FieldOffset(16*sizeof(Real))]
virtual event System::ComponentModel::PropertyChangedEventHandler^ PropertyChanged;
事件。编译器告诉我需要将 FieldOffset 属性分配给此事件,但执行此操作后,编译器会抛出错误消息:
Error 34 error C1093: API call 'DefineCustomAttribute' failed '0x801311c0'
我不允许将 StructLayout 更改为 Sequential 那么我该如何解决此问题?
任何帮助将不胜感激,
最好,apo。
通过分离解决:
protected:
[field:FieldOffset(16*sizeof(Real))]
System::ComponentModel::PropertyChangedEventHandler^ _pc;
public:
virtual event System::ComponentModel::PropertyChangedEventHandler^ PropertyChanged
{
void add(System::ComponentModel::PropertyChangedEventHandler^ p)
{
_pc += p;
}
void remove(System::ComponentModel::PropertyChangedEventHandler^ p)
{
_pc -= p;
}
void raise(Object ^sender, System::ComponentModel::PropertyChangedEventArgs ^ args)
{
_pc->Invoke(sender, args);
}
};
void OnPropertyChanged(String^ info)
{
PropertyChanged(this, gcnew System::ComponentModel::PropertyChangedEventArgs(info));
}
i have a CLR class which uses the Attribute StructLayout attribte:
[StructLayout(LayoutKind::Explicit)]
public ref class Matrix4 : System::ComponentModel::INotifyPropertyChanged
All fields make use of the FieldOffset attribute. Now i need to add an event, in particular i want to implment the INotifyPropertyChanged interface and hence i need the
[FieldOffset(16*sizeof(Real))]
virtual event System::ComponentModel::PropertyChangedEventHandler^ PropertyChanged;
event. The compiler tells me I need to assign the FieldOffset attribute to this event, but after doing that the compiler throws the error message:
Error 34 error C1093: API call 'DefineCustomAttribute' failed '0x801311c0'
I am not allowed to chage StructLayout to Sequential so how do i solve this issue?
Any help would be appreciated,
Best, apo.
SOLVED by separating:
protected:
[field:FieldOffset(16*sizeof(Real))]
System::ComponentModel::PropertyChangedEventHandler^ _pc;
public:
virtual event System::ComponentModel::PropertyChangedEventHandler^ PropertyChanged
{
void add(System::ComponentModel::PropertyChangedEventHandler^ p)
{
_pc += p;
}
void remove(System::ComponentModel::PropertyChangedEventHandler^ p)
{
_pc -= p;
}
void raise(Object ^sender, System::ComponentModel::PropertyChangedEventArgs ^ args)
{
_pc->Invoke(sender, args);
}
};
void OnPropertyChanged(String^ info)
{
PropertyChanged(this, gcnew System::ComponentModel::PropertyChangedEventArgs(info));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您添加的属性是否适用于该事件?或到字段?我不是 C++ 专家,但这看起来就像“类字段事件”的 C++ 实现。
[FieldOffset]
属性仅适用于支持字段 - 不适用于事件
。在 C# 中,您可以通过以下方式定位该字段:so: 确保您正在定位该字段。不过,我无法就 C++ 语法提供建议。也许没有,您需要使用显式事件实现和您自己添加的字段(然后可以装饰)。
这是通过快速搜索来支持的,该搜索显示链接到的错误号:
如果针对事件而不是字段,这正是我期望看到的。
顺便说一句 - 结构上的事件是棘手的野兽......
Does the attribute you've added apply to the event? or to the field? I'm not a C++ guru, but that looks like a C++ implementation of a "field-like event". The
[FieldOffset]
attribute only applies to the backing field - not theevent
. In C#, you would target the field via:so: make sure you are targetting the field. I can't advise on the C++ syntax for that, though. Maybe there isn't one and you need to use an explicit event implementation with a field you add yourself (and can then decorate).
This is supported by a quick search that shows that error number linked to:
which is exactly what I would expect to see if targeting the event rather than the field.
As an aside - events on structs are tricky beasts...