事件声明和 FieldOffsetAttribute 使用

发布于 2024-11-17 10:02:43 字数 1473 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2024-11-24 10:02:44

您添加的属性是否适用于该事件?或到字段?我不是 C++ 专家,但这看起来就像“类字段事件”的 C++ 实现。 [FieldOffset] 属性仅适用于支持字段 - 不适用于事件。在 C# 中,您可以通过以下方式定位该字段:

[field:FieldOffset(yourOffset)]
public event PropertyChangedEventHandler PropertyChanged;

so: 确保您正在定位该字段。不过,我无法就 C++ 语法提供建议。也许没有,您需要使用显式事件实现和您自己添加的字段(然后可以装饰)。

这是通过快速搜索来支持的,该搜索显示链接到的错误号:

failed '0x801311c0'

Description: The custom attribute is not valid for the target object's type.

如果针对事件而不是字段,这正是我期望看到的。

顺便说一句 - 结构上的事件是棘手的野兽......

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 the event. In C#, you would target the field via:

[field:FieldOffset(yourOffset)]
public event PropertyChangedEventHandler PropertyChanged;

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:

failed '0x801311c0'

Description: The custom attribute is not valid for the target object's type.

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文