Solved

Referencing .net class (Late Binding)

Posted on 2009-05-05
8
335 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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 video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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