我会尝试将您的代码转换为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 thatNG, I am also reporting it here in the hope that someone can find me aworkaround, and report it to MS.If a __value class with an event is put into an assembly and a __gc class inanother assembly, which has an instance of the __value class as a datamember, attempts to attach its own event handler to the __value class''sevent, everything compiles correctly but the Linker puts out error "LINK :error LNK2020: unresolved token (0A000003) AValueClassEvent" followed by"LINK : fatalerror LNK1120: 1 unresolved externals". If the __value class is in the sameassembly 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 oncenamespace 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 oncenamespace 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 += newSystem::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 inanother assembly, causes the linker error. If you comment it out there is noerror. 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 haveevents which must be caught by __gc classes in other assemblies, I badlyneed a workaround to this problem else I have to scrapped my own logicaldesign or duplicate value classes under slightly different names in eachassembly in which I want to use them, which is a real PITA. 解决方案<snip>Eek. Unfortunately I''m far from familiar with MC++. It looks like abug, certainly, but I''m not sure why.If the class with the event is a __gc class, does it work? (Not tryingto find a solution here, just the boundaries of a problem.) I wouldimagine it does, because otherwise all the events in Windows Formswould fail.Have you looked at the assembly containing the event with ildasm? Itmight be instructive to see what it''s got in it...--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me tooYou 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. You found it.Yup :) 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 codelinks against my C# DLL, and then if my C# calling code works againstyour C++ DLL, if you see what I mean.--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too 这篇关于错误:处理来自另一个程序集的值类事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-07 06:18