C # compile COM components
1. Create a Windows class library ApplicationProgramIn the assemblyinfo file of the properties directory, set comvisible (false) to comvisible (true ).
2. Select register for com InterOP in the Project Properties and build option.
3. Compile comCode(Sensorevents defines the event interface ).
As follows:
Code
Namespace Imdcom
{
/// <Summary>
/// Event Interface
/// </Summary>
[Interfacetype (cominterfacetype. interfaceisidispatch)]
Public Interface Icomevents
{
/// <Summary>
/// Define the event to be exposed
/// </Summary>
Void Onhellobegin ( Object Sender, eventargs E );
}
Public Class Helloevents: eventargs
{}
/// <Summary>
/// Custom Delegation
/// </Summary>
Public Delegate Void Comdel ( Object Sender, eventargs E );
[Comvisible ( True )]
[Comsourceinterfaces ( Typeof (Icomevents)]
Public Class Imdcomtest
{
Public String Hello ()
{
If (Onhellobegin ! = Null )
Onhellobegin ( This , New Helloevents ());
Return " Asdasdasd " ;
}
/// <Summary>
/// Implement event interfaces
/// </Summary>
Public Event Comdel onhellobegin;
}
}
4. Compile the DLL and use the command c: \ windows \ Microsoft. NET \ framework \ v4.0.30319 \ regasm.exe sensorscom. DLL to register COM.
Now C # Write COM is OK!
Call COM in Silverlight
1. Set the Silverlight program to the highest permission in OOB mode. Otherwise, Silverlight cannot call COM.
2. Call Code
As follows:
Code
Using (Dynamic imdcom = Automationfactory. Createobject ( " Imdcom. imdcomtest " ))
{
Automationevent Eve = Automationfactory. getevent (imdcom, " Onhellobegin " );
Eve. eventraised + = New Eventhandler < Automationeventargs > (S, E1) => {
MessageBox. Show (e1.arguments [ 0 ]. Tostring ());
});
Dynamic sensorlist = Imdcom. Hello ();
MessageBox. Show (sensorlist );
}