Vzájemná funkční spolupráce s jinými jazyky rozhraní .NET (C++/CLI)

Témata v této části ukazují, jak vytvořit sestavení v Visual C++, která využívají nebo poskytují funkce sestavením napsaným v jazyce C# nebo Visual Basic.

Využití indexeru C#

Visual C++ neobsahuje indexery; má indexované vlastnosti. Chcete-li využívat indexer C#, přistoupit k indexeru, jako by šlo o indexovanou vlastnost.

Další informace o indexerech najdete v tématech:

Příklad

Následující program v jazyce C# definuje indexer.

// 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]);
   }
}
*/

Tento program C++/CLI využívá indexer.

// 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]);
}

Příklad vytvoří tento výstup:

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

Implementovat klíčové slovo is a as jazyka C#

Toto téma ukazuje, jak implementovat funkci isas klíčových slov a jazyka C# v Visual C++.

Příklad

// 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

Implementace klíčového slova lock jazyka C#

Toto téma ukazuje, jak implementovat lock klíčové slovo jazyka C# v Visual C++.

Můžete také použít lock třídu v knihovně podpory jazyka C++. Další informace najdete v tématu synchronizace (třída zámku) .

Příklad

// 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;
}

Viz také

Programování pro .NET v jazyce C++/CLI (Visual C++)