CallByName函数在VB.NET缺点? [英] Disadvantages of CallbyName Function in VB.NET?

查看:151
本文介绍了CallByName函数在VB.NET缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何性能上的劣势,通过使用 CallByName 在VB.NET功能?有没有更好的办法按名称做呼叫.NET 2.0起。

Are there any disadvantages in performance by using the CallByName function in VB.NET? Is there any better way to do the call by Name in .NET 2.0 onwards.

推荐答案

CallByBame基本上给你后期绑定,这是搞清楚方法在运行时,而不是早期绑定里的编译器计算出来给你。

CallByBame basically gives you "late binding" which is "figuring out the method at run-time" as opposed to "early binding" where the compiler figures it out for you.

使用早期绑定,您可以类型安全,您将有更好的表现,因为您的code会去正确的方法。编译器将看着它为您的时间提前。

With early binding you can be type safe and you'll have better performance since your code will go right to the method. The compiler will have "looked it up" for you ahead of time.

使用后期绑定的性能会降低,因为该方法查找在运行时,你没有类型安全 - 这意味着方法实际上可能不存在,你可以得到一个异常。但是,这可能是方便的,如果您不知道由于某种原因,对象的类型。我还用它来调用COM对象,如果我不想惹互操作程序集。

With late binding performance is slower since the method is looked up at run time and you don't have type safety - which means the method may not actually exist and you could get an exception. But this might be handy if you don't know the type of the object for some reason. I also use it to call COM object if I don't want to mess with an interop assembly.

CallByName很可能调用Type.InvokeMember。如果您想直接做,这里的一些code,我想出了:

CallByName most likely calls Type.InvokeMember. If you want to do it directly, here's some code I came up with:

Imports System.Reflection   ' For access to BindingFlags '

Friend NotInheritable Class LateBinding

    Private Const InvokePublicMethod As BindingFlags = BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.InvokeMethod

    Private Const GetPublicProperty As BindingFlags = BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.GetProperty

    Public Shared Function InvokeFunction(ByVal oObject As Object, ByVal sName As String, ByVal ParamArray yArguments() As Object) As Object

        Return oObject.GetType().InvokeMember(sName, InvokePublicMethod, Nothing, oObject, yArguments)

    End Function

    Public Shared Function GetProperty(ByVal oObject As Object, ByVal sName As String, ByVal ParamArray yArguments() As Object) As Object

        Return oObject.GetType().InvokeMember(sName, GetPublicProperty, Nothing, oObject, yArguments)

    End Function

End Class

这篇关于CallByName函数在VB.NET缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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