为什么C#CreateObject比VB.NET更冗长? [英] Why is the C# CreateObject so much more verbose than VB.NET?

查看:268
本文介绍了为什么C#CreateObject比VB.NET更冗长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把一些VB6 / COM +代码转换为C#/ COM +

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

但是在VB6或VB.NET中有:

However where in VB6 or VB.NET I have:

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

在C#中,我似乎需要执行以下操作:

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

有一种方法让c#不必使用InvokeMember函数, ?

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

推荐答案


有一种方法让c#不必使用InvokeMember函数

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

是的,从C#4开始,动态类型

Yes, as of C# 4 with dynamic typing:

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

CreateObject ,但你可以总是包装在一个方法调用如果你想要的。 (有可能是一个现有的呼叫我不知道,或者你可以尝试找到任何VB调用在这种情况下 - 我不知道细节服务器.CreateObject 。)

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

请注意,动态类型比< em>更丰富,使反射更简单, 。在后台,同样的事情会发生在这两种情况下 - 它仍然不会像静态绑定一样快,但它几乎肯定是快速的

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.

这篇关于为什么C#CreateObject比VB.NET更冗长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆