If a __value class with an event is put into an assembly, and a __gc class
in another assembly attempts to attach its own event handler to the __value
class's event of an embedded object of the __value class type, 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 their own 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 make the __value classes into __gc classes instead.