2013-06-10 5 views
0

에 사용하는 C++ 클래스를 사용하여 C# & C# (매우 바람직한 기술) 결합 연습을위한 약간의 데모를 직접 만들었습니다.
그것은 다음과 같습니다다른 C++ 클래스의 객체를 C#

Class Diagram

문제는 클래스 Person 유형 Name의 회원을 가지고 있다는 사실에서 발생한다.
이러한 클래스를 C#에 노출시키는 올바른 방법은 무엇입니까? 나는이 생성자를 노출 할 방법 예를 들어

:

Person(Name name); 

이 설명하는 종류의 단단하지만 문제는이 Person 생성자는 기본입니다 Name 객체를 허용하는지, 그리고 나는이 기본을 만들 수 없습니다 C#에서 개체 및 그냥 생성자에서 전달, 오른쪽? 그것은 바람직하지도 않을 것이다.
그리고 래퍼를 쓰더라도 (ManagedName) 받아 들일 수있는 생성자가 없으므로 Person으로 전달할 수 없습니다.

이 상황에 어떻게 대처해야합니까?

Null 허용 : http://pastebin.com/z7zTCrAq
이름 : http://pastebin.com/ALNp5c1a
담당자 : http://pastebin.com/nkWyUv9C

+0

을 할 수있는 하나의 방법입니다 여기

는 요청에 따라 헤더입니다 C++/CLI를 고려해 보셨습니까? – IDWMaster

+0

@ IDWMaster 네,하지만 이것은 설계상의 문제입니다. C++/CLI는 어떻게 대처할 수 있습니까? – MasterMastic

+0

꽤 좋은 방법은 COM을 사용하는 것입니다. 클래스에 대한 인터페이스를 만들고 C#에서 동적으로 COM 메소드를 호출하십시오. – paddy

답변

1

여기 (C++/CLI)

 

class Name { 
public: 
    std::string firstName; 
    std::string middleName; 
    std::string lastName; 
}; 
ref class ManagedName { 
internal: 
    Name* nameptr; 
public: 

    ManagedName() { 
     nameptr = new Name(); 
    } 
    ManagedName(System::IntPtr ptr) { 
     nameptr = (Name*)(void*)ptr; 
    } 
    property System::String^ firstName { 
     System::String^ get() { 
      return gcnew System::String(nameptr->firstName.data()); 
     } 
     void set(System::String^ val) { 
      System::IntPtr strptr = System::Runtime::InteropServices::Marshal::StringToBSTR(val); 
      nameptr->firstName = (char*)(void*)strptr; 
      System::Runtime::InteropServices::Marshal::FreeBSTR(strptr); 
     } 
    }; 
    property System::String^ middleName { 
     System::String^ get() { 
      return gcnew System::String(nameptr->middleName.data()); 
     } 
     void set(System::String^ val) { 
      System::IntPtr strptr = System::Runtime::InteropServices::Marshal::StringToBSTR(val); 
      nameptr->middleName = (char*)(void*)strptr; 
      System::Runtime::InteropServices::Marshal::FreeBSTR(strptr); 
     } 
    }; 
    property System::String^ lastName { 
     System::String^ get() { 
      return gcnew System::String(nameptr->lastName.data()); 
     } 
     void set(System::String^ val) { 
      System::IntPtr strptr = System::Runtime::InteropServices::Marshal::StringToBSTR(val); 
      nameptr->lastName = (char*)(void*)strptr; 
      System::Runtime::InteropServices::Marshal::FreeBSTR(strptr); 
     } 
    }; 
}; 
class Person { 
public: 
    Name* name; 
    std::string birthday; 
    Person(Name* name) { 
     this->name = name; 
    } 
}; 
ref class ManagedPerson { 
public: 
    ManagedPerson(ManagedName^ name) { 
     //create an instance of the native class 
     personPtr = new Person(name->nameptr); 

    } 
    property System::String^ Birthday { 
     System::String^ get() { 
      return gcnew System::String(personPtr->birthday.data()); 
     } 

    }; 
    property ManagedName^ Name { 
     ManagedName^ get() { 
      return gcnew ManagedName(System::IntPtr(personPtr->name)); 
     } 
    }; 
private: 
    Person* personPtr; 
}; 
 
+0

참조 : http://msdn.microsoft.com/en-us/magazine/cc163681.aspx – IDWMaster

관련 문제