Solved

Referencing .net class (Late Binding)

Posted on 2009-05-05
8
342 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
 

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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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