
КОП-Примеры / Версия
.docПример 10.1. Использование версии компонента
///////////////
// C# Компонент версии 1.0.2801.34470,
// сгенерированной системой
using System;
using System.ComponentModel;
using System.Reflection;
namespace ccVerComp_1_Dll
{
public class VerComp: Component
{ private int number;
public VerComp (int num)
{ number= num;
Console.WriteLine ("Создан компонент number= " + number);
}
public void F1 ( ) {Console.WriteLine ("VerComp.F1");}
}
}
///////////////
// C++/CLI Компонент версии 1.0.2801.34577,
// сгенерированной системой
#using <mscorlib.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Reflection;
namespace ccVerComp_2_Dll
{
public ref class VerComp: public Component
{ private: int number;
public: VerComp (int num)
{ number= num;
Console::WriteLine("Создан компонент number= {0}",number.ToString ( ));
}
void F1 ( ) {Console::WriteLine ("VerComp.F1");}
};
}///////////////
// C++/CLI Компонент версии 2.0.5.0,
// указанной разработчиком с помощью атрибута assembly
#using <mscorlib.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Reflection;
namespace ccVerComp_3_Dll
{
[assembly:System::Reflection::AssemblyVersionAttribute("2.0.5")];
public ref class VerComp: public Component
{ private: int number;
public: VerComp (int num)
{ number= num;
Console::WriteLine ("Создан компонент number= {0}", number.ToString ( ));
}
void F1 ( ) {Console::WriteLine ("VerComp.F1");}
void F2 ( ) {Console::WriteLine ("VerComp.F2");}
};
}
///////////////
// C#
// Подключить библиотеки ccVerComp_1, ccVerComp_2 и ccVerComp_3
using System;
using System.Reflection;
using System.Reflection.Emit;
using ccVerComp_1_Dll;
using ccVerComp_2_Dll;
using ccVerComp_3_Dll;
class VerTest
{ static void Main ( )
{ // Создать объекты компонентов
ccVerComp_1_Dll.VerComp verComp1= new
ccVerComp_1_Dll.VerComp (1);
ccVerComp_2_Dll.VerComp verComp2= new
ccVerComp_2_Dll.VerComp (2);
ccVerComp_3_Dll.VerComp verComp3= new
ccVerComp_3_Dll.VerComp (3);
// Вызвать общую функцию F1
verComp1.F1 ( );
verComp2.F1 ( );
verComp3.F1 ( );
// Выдать версии компонентов на консоль
Type type1= typeof (ccVerComp_1_Dll.VerComp);
Assembly ass1= Assembly.GetAssembly (type1);
Console.WriteLine ("FullName=" + ass1.FullName);
Type type2= typeof (ccVerComp_2_Dll.VerComp);
Assembly ass2= Assembly.GetAssembly (type2);
Console.WriteLine ("FullName=" + ass2.FullName);
Type type3= typeof (ccVerComp_3_Dll.VerComp);
Assembly ass3= Assembly.GetAssembly (type3);
Console.WriteLine ("FullName=" + ass3.FullName);
// Убедиться в наличии объекта компонента, содержащего
// функцию F2 и вызвать эту функцию
string st= (string) ass3.FullName;
Console.WriteLine ("GetName=" + st);
for (int i=0; i < st.Length; i++)
if (st[i] == '=')
{
Console.WriteLine (st[i+1]);
if (st[i+1] == '2') verComp3.F2 ( );
break;
}
}
}