Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Late Binding In C#

Status
Not open for further replies.

SlantyOD

Programmer
Jun 11, 2002
22
0
0
CA
Hi All,

A quick question for you regarding late binding in C#. I believe I'm very close to getting this to work but not quite there yet. Basically I'm trying to recreate some VB6 code in C#. Here's the VB6 code:

Dim myInterface as (my COM Interface)
Dim myInt as Integer
Set myInterface = CreateObject("MyCurrentlyRunningExecutable")

myInt = myInterface.myMethod(0)

This works just fine.

However C# does not seem to implement Late Binding in the same fashion. I believe I have all the steps down now to reacreate this process, but there is something nitpicky that is wrong, and my suspicion is that it's because I'm trying to use an interface rather than a class. Here's my current code:

Type myType;
object myObject;
object result;

myType = Type.GetTypeFromProgID("MyCurrentlyRunningExecutable");
myObject = Activator.CreateInstance(myType);

result = myType.InvokeMember("myMethod", BindingFlags.Public | BindingFlags.InvokeMethod, null, myObject, new object [] {});

The error I get is "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: Unknown name."

Which isn't very descriptive =)

Any thoughts/suggestions/comments? Am I even close, cause I'm having serious trouble finding anything like this on the net.

Cheers,
SlantyOD
 
I finally got it working (although I'm still a bit vague on how =). In the end I just did a type convertion. I'm sure there's all sorts of inherent problems with this that I don't know since I'm a beginner programmer, but it works so I'm fairly happy. Code below:

Type myType = Type.GetTypeFromProgID("MyCurrentlyRunningExecutable");
object myObject = Activator.CreateInstance(myType);
ICOMInterface myInterface = (ICOMInterface)myObject;

At this point I can then execute the methods directly:
myInterface.myMethod();

Can anyone point out any major problems with this approach?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top