Solved

Referencing .net class (Late Binding)

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

 

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

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)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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