Этот вопрос расширяет существующий вопрос здесь:
Передача объекта из C ++ в C # через COM
Предыдущий вопрос касается простого объекта, но я бы хотелсделать то же самое для сложного объекта.
Таким образом, вместо того, чтобы TestEntity1 имел одно свойство, если у него есть другое свойство типа TestEntity2, как я могу назначить свойство типа TestEntity2 объекта TestEntity1 в приемнике c ++?
C #:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ClassLibrary1
{
[ComVisible(true)]
public interface ITestEntity1
{
string Name { get; set; }
TestEntity2 Entity2 { get; set; }
}
[ComVisible(true)]
public class TestEntity1 : ITestEntity1
{
public string Name { get; set; }
}
[ComVisible(true)]
public interface ITestEntity2
{
string Description { get; set; }
}
[ComVisible(true)]
public class TestEntity2 : ITestEntity2
{
public string Description { get; set; }
}
[ComVisible(true)]
public interface ITestGateway
{
void DoSomething(
[MarshalAs(UnmanagedType.Interface)]object comInputValue);
}
[ComVisible(true)]
public class TestGateway : ITestGateway
{
public void DoSomething(object comInputValue)
{
if (!(comInputValue is TestEntity1))
{
throw new ArgumentException("com input value", "comInputValue");
}
TestEntity1 entity = comInputValue as TestEntity1;
//entity.Name
//entity.Entity2
}
}
}
C ++:
// ComClient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#import "..\Debug\ClassLibrary1.tlb" raw_interfaces_only
int _tmain(int argc, _TCHAR* argv[])
{
ITestGatewayPtr spTestGateway;
spTestGateway.CreateInstance(__uuidof(TestGateway));
ITestEntity1Ptr spTestEntity1;
spTestEntity1.CreateInstance(__uuidof(TestEntity1));
_bstr_t name(L"name");
spTestEntity1->put_Name(name);
ITestEntity2Ptr spTestEntity2;
spTestEntity2.CreateInstance(__uuidof(TestEntity2));
//spTestEntity1->putref_Entity2(spTestEntity2); //error C2664: 'ClassLibrary::ITestEntity1::putref_Entity2' : cannot convert parameter 1 from 'ClassLibrary::ITestEntity2Ptr' to 'ClassLibrary::_TestEntity2 *'
spTestGateway->DoSomething(spTestEntity1);
Спасибо.