Interaction.CallByName(Object, String, CallType, Object[]) 方法

定義

在物件上執行方法,或是設定或傳回物件上的屬性。

public:
 static System::Object ^ CallByName(System::Object ^ ObjectRef, System::String ^ ProcName, Microsoft::VisualBasic::CallType UseCallType, ... cli::array <System::Object ^> ^ Args);
public static object? CallByName (object? ObjectRef, string ProcName, Microsoft.VisualBasic.CallType UseCallType, params object?[] Args);
public static object CallByName (object ObjectRef, string ProcName, Microsoft.VisualBasic.CallType UseCallType, params object[] Args);
static member CallByName : obj * string * Microsoft.VisualBasic.CallType * obj[] -> obj
Public Function CallByName (ObjectRef As Object, ProcName As String, UseCallType As CallType, ParamArray Args As Object()) As Object

參數

ObjectRef
Object

必要。 Object. 公開屬性或方法之物件的指標。

ProcName
String

必要。 String. 包含物件之屬性或方法名稱的字串運算式。

UseCallType
CallType

必要。 型別 CallType 的列舉型別成員,其表示正在呼叫的程序型別。 CallType 的值可以是 MethodGetSet

Args
Object[]

選擇性。 ParamArray. 參數陣列,其中包含要傳遞給屬性或正在呼叫的方法之引數。

傳回

在物件上執行方法,或是設定或傳回物件上的屬性。

例外狀況

無效的 UseCallType 值,必須是 MethodGetSet

範例

在下列範例中,第一行使用 CallByName 來設定 Text 文本框的 屬性,第二行會擷取 屬性的值 Text ,而第三行會 Move 叫用 方法來移動文字框。

' Imports statements must be at the top of a module.
Imports Microsoft.VisualBasic.CallType
Sub TestCallByName1()
    'Set a property.
    CallByName(TextBox1, "Text", CallType.Set, "New Text")

    'Retrieve the value of a property.
    MsgBox(CallByName(TextBox1, "Text", CallType.Get))

    'Call a method.
    CallByName(TextBox1, "Hide", CallType.Method)
End Sub

下一個範例會 CallByName 使用 函式來叫 Add 用集合物件的 和 Item 方法。

Public Sub TestCallByName2()
    Dim col As New Collection()

    'Store the string "Item One" in a collection by 
    'calling the Add method.
    CallByName(col, "Add", CallType.Method, "Item One")

    'Retrieve the first entry from the collection using the 
    'Item property and display it using MsgBox().
    MsgBox(CallByName(col, "Item", CallType.Get, 1))
End Sub

備註

CallByName 式會在運行時間用來取得屬性、設定屬性或叫用方法。

適用於

另請參閱