?
Solved

Referencing .net class (Late Binding)

Posted on 2009-05-05
8
Medium Priority
?
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1050 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
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 

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
 
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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

741 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