C # compile COM components and call them in Silverlight

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.