错误:处理来自另一个程序集的值类事件 [英] Bug: handling value class events from another assembly

查看:61
本文介绍了错误:处理来自另一个程序集的值类事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经发生在MC ++中了,但由于对这个

NG的响应非常少,我也在这里报告,希望有人能找到我一个

解决方法,并将其报告给MS。


如果带有事件的__value类被放入程序集和__gc类中,则需要另外一个程序集,有一个__value类的实例作为数据

成员,尝试将自己的事件处理程序附加到__value类的

事件,一切都正确编译但链接器输出错误LINK:

错误LNK2020:未解析的令牌(0A000003)AValueClassEvent接下来是

LINK:致命

错误LNK1120:1个未解析的外部因素。如果__value类与__gc类在同一个

程序集中,那么一切都很好并且没有链接器错误。

以下是说明此错误的代码。 />

---------------------------------------- --------------------------------


在一个集会中:


// ValueClass.h

#pragma一次

命名空间ValueClass

{

public __value class AValueClass

{

public:

AValueClass();

__event System :: EventHandler * AValueClassEvent;

int AnInt;

};

}


// ValueClass。 cpp

#include" stdafx.h"

#include" ValueClass.h"

ValueClass :: AValueClass :: AValueClass() :AnInt(1){}


在第二个引用第一个程序集的程序集中:


// HandleValueClassEvent.h

#pragma一次

名称空间HandleValueClassEvent

{

public __value class AnotherValueClass

{

public:

AnotherValueClass();

__event System :: EventHandler * AnotherValueClassEvent;

int AnotherInt;

};

public __gc class HandleEvent

{

public:

HandleEvent();

void AMemberFunction();

void AnotherMemberFunction ();

私人:

ValueClass :: AValueClass avc;

HandleValueClassEvent :: AnotherValueClass anvc;

void AnEventHandler(System :: Object *,System :: EventArgs *);

};

}


// HandleValueClassEvent。 cpp

#include" stdafx.h"

#include" HandleValueClassEvent.h"

HandleValueClassEvent :: HandleEvent :: HandleEvent() {}

void HandleValueClassEvent :: HandleEvent :: AMemberFunctio n()

{

avc.AValueClassEvent + = new System :: EventHandler(this ,AnEventHandler);

}

void HandleValueClassEvent :: HandleEvent :: AnotherMemberF unction()

{

anvc.AnotherValueClassEvent + = new

System :: EventHandler(this,AnEventHandler);

}

void HandleValueClassEvent :: HandleEvent :: AnEventHandler(System :: Object *

sender,System :: EventArgs * e){}

HandleValueClassEvent :: AnotherValueClass :: AnotherV alueClass():

AnotherInt(1){}


------------------------------------------ ----------------------------------

-


尝试将事件处理程序添加到另一个程序集中的avc.AValueClassEvent,会导致链接器错误。如果你评论它没有

错误。尝试将事件处理程序添加到anvc.AnotherValueClassEvent,

是同一个程序集,不会导致错误。


因为我设计了一些__value程序集中的类具有

事件,必须由其他程序集中的__gc类捕获,我很难

需要解决此问题,否则我必须废弃我自己的逻辑

在我想要使用它们的每个

程序集中,名称略有不同的设计或重复值类,这是一个真正的PITA。

解决方案

Edward Diener< ed ****** @ tropicsoft.com>写道:

这已发生在MC ++中,但由于对NG的回复非常少,我也在这里报告,希望有人能找到我一个
解决方法,然后向MS报告。




< snip>


Eek。不幸的是,我对MC ++并不熟悉。它看起来像一个

的bug,当然,但我不知道为什么。


如果带有事件的类是__gc类,那么它工作? (没有尝试

来找到这里的解决方案,只是问题的界限。)我想

想象一下,因为否则Windows窗体中的所有事件

会失败。


您是否看过包含ildasm事件的程序集?它可能是有意义的,看看它有什么...


-

Jon Skeet - < sk *** @ pobox.com>
http:// www。 pobox.com/~skeet

如果回复小组,请不要给我发邮件


Jon Skeet [C#MVP ]写道:

Edward Diener< ed ****** @ tropicsoft.com>写道:

这是在MC ++中发生的,但由于对NG的响应非常少,我也在这里报告,希望有人能找到我解决方法,然后向MS报告。
< snip>




你找到了它。

Eek。不幸的是,我对MC ++并不熟悉。它看起来像一个
bug,当然,但我不知道为什么。

如果带有事件的类是__gc类,它是否有效?


当然可以。

(不试图在这里找到解决方案,只是问题的界限。)我会

是的,

您是否看过包含ildasm事件的程序集?它可能有助于了解它的内容......




我会看一下。


Edward Diener< ed ****** @ tropicsoft.com>写道:

< snip>



你找到了它。




是的:)

你看过包含ildasm事件的程序集吗?它可能有助于了解它的内容......



我会看一下。



我会尝试将您的代码转换为C#并查看它是否有效。

下一步(如果有)是查看您的C ++调用代码

链接我的C#DLL,如果我的C#调用代码对你的C ++ DLL起作用,如果你明白我的意思。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件


This has occurred in MC++, but since there is very little response on that
NG, I am also reporting it here in the hope that someone can find me a
workaround, and report it to MS.

If a __value class with an event is put into an assembly and a __gc class in
another assembly, which has an instance of the __value class as a data
member, attempts to attach its own event handler to the __value class''s
event, everything compiles correctly but the Linker puts out error "LINK :
error LNK2020: unresolved token (0A000003) AValueClassEvent" followed by
"LINK : fatal
error LNK1120: 1 unresolved externals". If the __value class is in the same
assembly as the __gc class, everything is fine and there is no linker error.
What follows is code illustrating this bug.

------------------------------------------------------------------------

In one assembly:

// ValueClass.h
#pragma once
namespace ValueClass
{
public __value class AValueClass
{
public:
AValueClass();
__event System::EventHandler * AValueClassEvent;
int AnInt;
};
}

// ValueClass.cpp
#include "stdafx.h"
#include "ValueClass.h"
ValueClass::AValueClass::AValueClass() : AnInt(1) {}

In a second assembly which references the first assembly:

// HandleValueClassEvent.h
#pragma once
namespace HandleValueClassEvent
{
public __value class AnotherValueClass
{
public:
AnotherValueClass();
__event System::EventHandler * AnotherValueClassEvent;
int AnotherInt;
};
public __gc class HandleEvent
{
public:
HandleEvent();
void AMemberFunction();
void AnotherMemberFunction();
private:
ValueClass::AValueClass avc;
HandleValueClassEvent::AnotherValueClass anvc;
void AnEventHandler(System::Object *,System::EventArgs *);
};
}

// HandleValueClassEvent.cpp
#include "stdafx.h"
#include "HandleValueClassEvent.h"
HandleValueClassEvent::HandleEvent::HandleEvent() {}
void HandleValueClassEvent::HandleEvent::AMemberFunctio n()
{
avc.AValueClassEvent += new System::EventHandler(this,AnEventHandler);
}
void HandleValueClassEvent::HandleEvent::AnotherMemberF unction()
{
anvc.AnotherValueClassEvent += new
System::EventHandler(this,AnEventHandler);
}
void HandleValueClassEvent::HandleEvent::AnEventHandler (System::Object *
sender,System::EventArgs * e) {}
HandleValueClassEvent::AnotherValueClass::AnotherV alueClass() :
AnotherInt(1) {}

----------------------------------------------------------------------------
--

The attempt to add an event handler to avc.AValueClassEvent, which is in
another assembly, causes the linker error. If you comment it out there is no
error. The attempt to add an event handler to anvc.AnotherValueClassEvent,
which is the same assembly, causes no error.

Since I have designed a number of __value classes in an assembly which have
events which must be caught by __gc classes in other assemblies, I badly
need a workaround to this problem else I have to scrapped my own logical
design or duplicate value classes under slightly different names in each
assembly in which I want to use them, which is a real PITA.

解决方案

Edward Diener <ed******@tropicsoft.com> wrote:

This has occurred in MC++, but since there is very little response on that
NG, I am also reporting it here in the hope that someone can find me a
workaround, and report it to MS.



<snip>

Eek. Unfortunately I''m far from familiar with MC++. It looks like a
bug, certainly, but I''m not sure why.

If the class with the event is a __gc class, does it work? (Not trying
to find a solution here, just the boundaries of a problem.) I would
imagine it does, because otherwise all the events in Windows Forms
would fail.

Have you looked at the assembly containing the event with ildasm? It
might be instructive to see what it''s got in it...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jon Skeet [C# MVP] wrote:

Edward Diener <ed******@tropicsoft.com> wrote:

This has occurred in MC++, but since there is very little response
on that NG, I am also reporting it here in the hope that someone can
find me a workaround, and report it to MS.
<snip>



You found it.

Eek. Unfortunately I''m far from familiar with MC++. It looks like a
bug, certainly, but I''m not sure why.

If the class with the event is a __gc class, does it work?
Yes, of course.
(Not trying
to find a solution here, just the boundaries of a problem.) I would
imagine it does, because otherwise all the events in Windows Forms
would fail.
Yes,

Have you looked at the assembly containing the event with ildasm? It
might be instructive to see what it''s got in it...



I will look at it.


Edward Diener <ed******@tropicsoft.com> wrote:

<snip>



You found it.



Yup :)

Have you looked at the assembly containing the event with ildasm? It
might be instructive to see what it''s got in it...



I will look at it.



I''ll try converting your code into C# and see whether it works then.
The next step (if it does) is to see whether your C++ calling code
links against my C# DLL, and then if my C# calling code works against
your C++ DLL, if you see what I mean.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于错误:处理来自另一个程序集的值类事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆