Solved

Referencing .net class (Late Binding)

Posted on 2009-05-05
8
336 Views
Last Modified: 2013-12-17
I have a c# project that references a com object. The com object can only be installed on one computer. I want to deploy the project on many computers.
Normally I would have used CreateObject on a switch.
// e.g. 

if (GetSetting("Connect")){
 

   mobjConnect = //CreateObject("My.Class");

}

Open in new window

0
Comment
Question by:ClearRock
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24302860
0
 

Author Comment

by:ClearRock
ID: 24303069
Thanks ... seems a bit complicated
I've been looking at 'System.Activator - Activator.CreateInstance(type As System.Type)' which is the eq of create instance. Is there a similar one for .net dlls save having to reference etc...

Create instance appears not to like my .net dll but does like good old com dlls (Word);
John
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 350 total points
ID: 24303166
According to MSDN, CreateInstance can be used to create instances of .NET types. In the following example, createinstance is being used to create object of type StringBuilder which is a .NET type.

http://msdn.microsoft.com/en-us/library/wccyzw83.aspx

There may be some other problem such as no default constructor in the class.
0
 

Author Comment

by:ClearRock
ID: 24304686
obviously more complicated than i thought
See the code below...
Thanks for the help...
//Fine

object cpay = Activator.CreateInstance("SIM", "CPayroll");

//object cpay = Activator.CreateInstance(Type.GetTypeFromProgID("SIM.CPayroll"));

//OK (as expected)

//CPayroll cpay = new CPayroll();

//Compile Error --- 'object' does not contain a definition for 'SetPerson'

cpay.SetPerson(sNiNumber, sTitle, sFirst, sLast, dteDOB,sAddress1,sAddress2, sAddress3, sAddress4, sAddress5, sPostCode,

dteDateStarted, IsMale, sInitials, IsMarried, sEmail,sNiNumber, sHomePhone,sOtherHomePhone, sMobilePhone,sOtherMobilePhone);

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24304736
"This method is provided for COM support. ProgIDs are not used in theMicrosoft .NET Framework because they have been superseded by theconcept of namespace.This method is provided for COM support. ProgIDs are not used in the Microsoft .NET Framework because they have been superseded by the concept of namespace."

This is what Microsoft says about GetTypeFromProgID. Is the type a .NET class? Then try this
GetType(Sim.CPayroll) (VB!)
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24304748
Microsoft said it only once!
0
 

Author Comment

by:ClearRock
ID: 24304890
Hi CodeCruiser

My prob is not the specific really but the general. In the old days (a long time ago) I used to be able to late bind an object (CreateObject) dependant on it being there. I have my interface to SIM in a seperate assembly all working and nice, but it depends entirely on SIM being installed as SIM costs a lot it is only installed on some m/cs. I want to be able to deploy my lot so it works whether or not SIM is installed (and later will pick up SIM if it gets installed). I'm not that bothered in what .net 3.5 no longer does but rather what it could do. It would seem to me that I could instantiate my sim object and then use it as an object rather than a type. If that is not possible then I'll skin the cat another way.
e.g. within a functon....
1 Instantiate the assembly + class with the current code knowing nothing about its existance (not referenced)
2 Use the functions in the class
3 Ditch it
I check wether the installation of SIM is present and run or block the function accordingly (Actually blocking the menu at the moment).

If I reference the class and it cannot see SIM the whole thing pours out errors which is not so nice in front of customers!

If you have a good option I'd be gratefull, otherwise time suggests taking another route.

Thanks
John
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24305001
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now