다른 .NET 언어와의 상호 운용성(C++/CLI)

이 섹션의 항목에서는 C# 또는 Visual Basic으로 작성된 어셈블리에서 사용하거나 기능을 제공하는 Visual C++에서 어셈블리를 만드는 방법을 보여 줍니다.

C# 인덱서 사용

Visual C++에는 인덱서가 없습니다. 인덱싱된 속성이 있습니다. C# 인덱서 사용하려면 인덱서가 인덱싱된 속성인 것처럼 인덱서에 액세스합니다.

인덱서에 대한 자세한 내용은 다음을 참조하세요.

예시

다음 C# 프로그램에서는 인덱서가 정의됩니다.

// consume_cs_indexers.cs
// compile with: /target:library
using System;
public class IndexerClass {
   private int [] myArray = new int[100];
   public int this [int index] {   // Indexer declaration
      get {
         // Check the index limits.
         if (index < 0 || index >= 100)
            return 0;
         else
            return myArray[index];
      }
      set {
         if (!(index < 0 || index >= 100))
            myArray[index] = value;
      }
   }
}
/*
// code to consume the indexer
public class MainClass {
   public static void Main() {
      IndexerClass b = new IndexerClass();

      // Call indexer to initialize elements 3 and 5
      b[3] = 256;
      b[5] = 1024;
      for (int i = 0 ; i <= 10 ; i++)
         Console.WriteLine("Element #{0} = {1}", i, b[i]);
   }
}
*/

이 C++/CLI 프로그램에서는 인덱서가 사용됩니다.

// consume_cs_indexers_2.cpp
// compile with: /clr
#using "consume_cs_indexers.dll"
using namespace System;

int main() {
   IndexerClass ^ ic = gcnew IndexerClass;
   ic->default[0] = 21;
   for (int i = 0 ; i <= 10 ; i++)
      Console::WriteLine("Element #{0} = {1}", i, ic->default[i]);
}

이 예에서는 다음과 같은 출력을 생성합니다.

Element #0 = 21
Element #1 = 0
Element #2 = 0
Element #3 = 0
Element #4 = 0
Element #5 = 0
Element #6 = 0
Element #7 = 0
Element #8 = 0
Element #9 = 0
Element #10 = 0

C# 키워드로 구현

이 항목에서는 Visual C++에서 Cas# 키워드(keyword) 기능을 is 구현하는 방법을 보여 줍니다.

예시

// CS_is_as.cpp
// compile with: /clr
using namespace System;

interface class I {
public:
   void F();
};

ref struct C : public I {
   virtual void F( void ) { }
};

template < class T, class U >
Boolean isinst(U u) {
   return dynamic_cast< T >(u) != nullptr;
}

int main() {
   C ^ c = gcnew C();
   I ^ i = safe_cast< I ^ >(c);   // is (maps to castclass in IL)
   I ^ ii = dynamic_cast< I ^ >(c);   // as (maps to isinst in IL)

   // simulate 'as':
   Object ^ o = "f";
   if ( isinst< String ^ >(o) )
      Console::WriteLine("o is a string");
}
o is a string

잠금 C# 키워드 구현

이 항목에서는 Visual C++에서 C# lock 키워드(keyword) 구현하는 방법을 보여줍니다.

C++ 지원 라이브러리에서 클래스를 사용할 lock 수도 있습니다. 자세한 내용은 동기화(잠금 클래스)를 참조하세요.

예시

// CS_lock_in_CPP.cpp
// compile with: /clr
using namespace System::Threading;
ref class Lock {
   Object^ m_pObject;
public:
   Lock( Object ^ pObject ) : m_pObject( pObject ) {
      Monitor::Enter( m_pObject );
   }
   ~Lock() {
      Monitor::Exit( m_pObject );
   }
};

ref struct LockHelper {
   void DoSomething();
};

void LockHelper::DoSomething() {
   // Note: Reference type with stack allocation semantics to provide
   // deterministic finalization

   Lock lock( this );
   // LockHelper instance is locked
}

int main()
{
   LockHelper lockHelper;
   lockHelper.DoSomething();
   return 0;
}

참고 항목

C++/CLI를 사용한 .NET 프로그래밍 (Visual C++)