Why is the C# CreateObject so much more verbose than VB.NET?

I am looking to convert some VB6/COM+ code to C#/COM+

However where in VB6 or VB.NET I have:

Dim objAdmin
objAdmin = Server.CreateObject("AppAdmin.GUI")
objAdmin.ShowPortal()

In C# it seems like I have to do the following:

object objAdmin = null;
System.Type objAdminType = System.Type.GetTypeFromProgID("AppAdmin.GUI");
m_objAdmin = System.Activator.CreateInstance(objAdminType);
objAdminType.InvokeMember("ShowPortal", System.Reflection.BindingFlags.InvokeMethod, null, objAdmin, null);

Is there a way of getting c# to not have to use the InvokeMember function and just call the function directly?

Answers


Is there a way of getting c# to not have to use the InvokeMember function and just call the function directly?

Yes, as of C# 4 with dynamic typing:

dynamic admin = Activator.CreateInstance(Type.GetTypeFromProgID("AppAdmin.GUI"));
admin.ShowPortal();

It's still more verbose in the CreateObject part, but you could always wrap that up in a method call if you wanted. (There may be an existing call I'm not aware of, or you could try to find whatever VB is calling in that case - I don't know the details of Server.CreateObject.)

Note that dynamic typing is richer than just making reflection simpler, but it certainly does that. Behind the scenes, the same kind of thing will be happening in both cases though - it's still not going to be as fast as static binding, but it's almost certainly fast enough.


Need Your Help

angularJS textAngular input box loses focus after entering text

angularjs input focus textarea richtextbox

I'm having an issue with an input box losing focus after entering a character. This only happens when the text angular RTB is selected first and then the input box.

Google plus login requires permission on every login

google-plus google-login

I have implemented a Facebook, Twitter and Google plus login to my website. For Facebook and Twitter the user needs to give permission to the app one time, unless i make a change in the required

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.