C++ 클래스 인스턴스를 C# 프로그램이 넘겨 받을 때 프로퍼티...
아래 내용은 MyDLL.cpp 입니다.
class MYCLASS
{
public:
MYCLASS()
{
this->_foo = -1;
const wchar_t* temp = L"MYCLASS Instance";
int tempsize = (wcslen(temp) + 1) * sizeof(wchar_t);
this->_bar = (wchar_t *)malloc(tempsize);
memset(this->_bar, 0, tempsize);
memmove(this->_bar, temp, tempsize);
}
int fooGet() { return this->_foo; }
void fooSet(int value) { this->_foo = value; }
wchar_t* barGet() { return this->_bar; }
void barSet(wchar_t* value) { this->_bar = value; }
private:
int _foo;
wchar_t* _bar;
};
extern "C" __declspec(dllexport) MYCLASS * CreateMyClassInstance()
{
MYCLASS * retval = new MYCLASS[1];
*retval = MYCLASS();
return retval;
}
---------- 아래는 C# 소스입니다. ----------
public interface MYCLASS
{
int foo { get; set; }
string bar { get; set; }
}
public static class Program
{
[DllImport("MyDLL.dll", EntryPoint = "CreateMyClassInstance")]
public extern static MYCLASS CreateInstance();
public static void Main()
{
MYCLASS instance = CreateInstance();
Console.WriteLine("foo == {0}", instance.foo);
instance.foo = 16;
Console.WriteLine("foo == {0}", instance.foo);
Console.WriteLine("bar == {0}", instance.bar);
instance.bar = "C# Instance";
Console.WriteLine("bar == {0}", instance.bar);
}
}
원래대로라면 C++ 클래스에서 fooGet/Set barGet/Set 으로 멤버함수를 선언했기 때문에
C# 인터페이스에서도 fooGet/Set, barGet/Set으로 선언해야 합니다.
그런데 C#에선 프로퍼티가 지원 되니까 C++ 클래스처럼 그대로 Get/Set 이 들어간 멤버함수를
쓰느니 차라리 두 함수를 C#에서는 프로퍼티로 묶어서 처리 하고 싶습니다.
그렇게 하려면 C# 프로퍼티에서 get, set 작동이 들어올 때 내부적으로 fooGet, fooSet 함수가
호출되도록 무언가 특성을 지정해 주어야 할듯 싶은데
DllImport 특성은 EntryPoint로 외부 함수의 진입점을 명시적으로 지정할 수 있듯이
프로퍼티에서도 마찬가지로
instance.foo = 16; 과 같은 코드가 있을 때 이것을 내부적으로
원래 C++ 클래스에서 선언한 멤버 함수인 fooSet 함수가 호출되도록 할 수 있을까요?
(C++에서 프로퍼티를 구현하는 것이 아닙니다.)
댓글 달기