Google Answers Logo
View Question
 
Q: Late bind COM objects in C# ( No Answer,   4 Comments )
Question  
Subject: Late bind COM objects in C#
Category: Computers > Programming
Asked by: rob99brown-ga
List Price: $50.00
Posted: 15 Mar 2004 18:23 PST
Expires: 14 Apr 2004 19:23 PDT
Question ID: 317118
I need to write a C# application that uses some 3rd party, vendor
supplied COM objects.  I do not have access to the COM object source
code.

I have had some success using the objects from VB.Net, and have used
them many times from VBA (Excel).  I need to use C# as it is the
strategic platform for my client, in this case.

The problem is that C# does not seem to support late binding very
easily, and I need to late bind because as we need to be able to
upgrade/update the COM objects without breaking my application.

Here is an example of the COM object being used from VB.Net:
------------------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim oSession As Object
        Dim cf As Object
        Dim Parms() As String = {"user", "password", "env"}
        Dim transRef as Long

        oSession = New MSRV.Session  ' this is a COM obj I have referenced
        oSession.logon(Parms(0), Parms(1), Parms(2))

        cf = oSession.sessionItem("TRANS") ' TRANS is another COM
object I have referenced
        cf.NewTrans()
        cf.Trans.Properties.Item("Description").Value = "purchase"
        cf.Trans.Properties.Item("Date").Value = "16-sep-2003"
        cf.Trans.Properties.Item("Amount").Value = "1000"
        ' there are various other properties....
        transRef = cf.SaveTrans()

        Me.RichTextBox1.Text = "Trans No: " + CStr(transRef)

End Sub
------------------------------------------------------------------

As you can see, there is a master object which handles logins etc.,
and can "spawn" transaction input type objects via the SessionItem
method.

This all works just fine, VB.Net is late binding by default and I have
tested changing versions of the COM objects (where the members and
properties stay the same) but as I said, I want to do this in C#.

I used Lutz Roeders Reflector in an attempt to do a conversion from VB
to C#, but the decompilation shows that the assembly uses LateBinding
and related methods in the Microsoft.VisualBasic.CompilerServices
namespace.  This namepsace does not appear to be available from C#.

Here is a sample from the decompilation (I added the fully qualified
namepsaces manually):
------------------------------------------------------------------
// this is the decompilation of the cf = oSession.sessionItem("TRANS")
// from the above VB example
array3 = new object[1];
array3[0] = "TRANS";
obj1 = System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(obj2,
null, "sessionItem", array3, null, null));
------------------------------------------------------------------

I have made many attempts at this problem using reflection and Type
objects etc, but have had no luck so far.

Can anyone please help?

Request for Question Clarification by studboy-ga on 17 Mar 2004 16:12 PST
Can you tell me more about your attempt using reflection?

Request for Question Clarification by studboy-ga on 17 Mar 2004 16:18 PST
If you post your C# source code (your reflection/GetType attempt)
here, I think I can help you.  Reflection is the "standard" way to go:

http://www.devarticles.com/c/a/C-Sharp/Using-Late-Bound-COM-Objects/1/
http://www.codeguru.com/Csharp/Csharp/cs_syntax/reflection/article.php/c5881
Answer  
There is no answer at this time.

Comments  
Subject: Re: Late bind COM objects in C#
From: mrclark-ga on 15 Mar 2004 18:46 PST
 
The Visual Basic .NET Compiler natively generates IL that makes use of
a variety of services provided by the Microsoft.VisualBasic.dll.
However, you can explicitly use these methods from any .NET Language
by including an assembly reference to this DLL. If you go to the "add
references" dialog in VS.Net the dll is listed as "Microsoft
VisualBasic .NET Runtime". But it is really the
Microsoft.VisualBasic.dll assembly, and should work with any managed
code. You also should not need to redistribute the assembly as it is
part of the platform itself. There are a variety of useful methods in
this assembly, for example, Interaction.Beep... the only way that I am
aware of to make a noise from the v1.1 clr without interoping to the
OS.
Subject: Re: Late bind COM objects in C#
From: rob99brown-ga on 15 Mar 2004 19:21 PST
 
Thanks for the comment.

I think I read that the use of the Microsoft.Visualbasic namespace
from anywhere other than VB.Net code was considered poor programming
form.  Any comment?
Subject: Re: Late bind COM objects in C#
From: mrclark-ga on 16 Mar 2004 15:12 PST
 
There is no technical problem with doing this. These are public API's
(although they are undocumented). So this is not the same as using
some internal API. That being said, I can see the assertion of
"bad-form" given potential maintenance problems with using API's that
have no documentation. You might go out of your way to wrap the use of
any of these APIs in classes of your own, and include heavier
commenting in that code.

Incidentally, I posted the comment, not because I am sure this is the
best way in C# to late-bind to COM objects. That is not an area that I
have much experience with. I just posted the comment so that you
could, if you wanted, write your C# code to emulate VB-logic in this
respect.
Subject: Re: Late bind COM objects in C#
From: rob99brown-ga on 16 Mar 2004 19:49 PST
 
Thanks mrclark.

Your comments are appreciated, I'm just trying to make sure I'm going
to use the best, most "proper" solution.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy