DynamicObject.TryInvoke(InvokeBinder, Object[], Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供呼叫物件操作的實作。 從該 DynamicObject 類別衍生的類別可以覆寫此方法,以指定像是調用物件或代理等操作的動態行為。
public:
virtual bool TryInvoke(System::Dynamic::InvokeBinder ^ binder, cli::array <System::Object ^> ^ args, [Runtime::InteropServices::Out] System::Object ^ % result);
public virtual bool TryInvoke(System.Dynamic.InvokeBinder binder, object[] args, out object result);
abstract member TryInvoke : System.Dynamic.InvokeBinder * obj[] * obj -> bool
override this.TryInvoke : System.Dynamic.InvokeBinder * obj[] * obj -> bool
Public Overridable Function TryInvoke (binder As InvokeBinder, args As Object(), ByRef result As Object) As Boolean
參數
- binder
- InvokeBinder
提供有關呼叫操作的資訊。
- args
- Object[]
在呼叫操作中傳遞給物件的參數。 例如,對於運算 sampleObject(100) ,其中 sampleObject 是從類別 DynamicObject 推導出來的, args[0] 等於 100。
- result
- Object
物件召喚的結果。
傳回
如果作業成功,則為 true,否則為 false。 若此方法回傳 false,該語言的執行時綁定器決定行為。 (大多數情況下,會拋出語言特定的執行時例外。
範例
假設你需要一個資料結構來儲存數字的文字和數字表示。 你希望能夠分別指定每個屬性的值,並且能在單一語句中初始化所有屬性。
以下程式碼範例展示了 DynamicNumber 由該 DynamicObject 類別衍生而來的類別。
DynamicNumber 覆寫方法 TryInvoke ,使所有屬性同時初始化。 它也會覆寫 和 TryGetMember 方法TrySetMember,以啟用對個別物件屬性的存取。
// The class derived from DynamicObject.
public class DynamicNumber : DynamicObject
{
// The inner dictionary to store field names and values.
Dictionary<string, object> dictionary
= new Dictionary<string, object>();
// Get the property value.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
return dictionary.TryGetValue(binder.Name, out result);
}
// Set the property value.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
dictionary[binder.Name] = value;
return true;
}
// Initializing properties with arguments' values.
public override bool TryInvoke(
InvokeBinder binder, object[] args, out object result)
{
// The invoke operation in this case takes two arguments.
// The first one is integer and the second one is string.
if ((args.Length == 2) &&
(args[0].GetType() == typeof(int)) &&
(args[1].GetType() == typeof(String)))
{
// If the property already exists,
// its value is changed.
// Otherwise, a new property is created.
if (dictionary.ContainsKey("Numeric"))
dictionary["Numeric"] = args[0];
else
dictionary.Add("Numeric", args[0]);
if (dictionary.ContainsKey("Textual"))
dictionary["Textual"] = args[1];
else
dictionary.Add("Textual", args[1]);
result = true;
return true;
}
else
{
// If the number of arguments is wrong,
// or if arguments are of the wrong type,
// the method returns false.
result = false;
return false;
}
}
}
class Program
{
static void Test(string[] args)
{
// Creating a dynamic object.
dynamic number = new DynamicNumber();
// Adding and initializing properties.
// The TrySetMember method is called.
number.Numeric = 1;
number.Textual = "One";
// Printing out the result.
// The TryGetMember method is called.
Console.WriteLine(number.Numeric + " " + number.Textual);
// Invoking an object.
// The TryInvoke method is called.
number(2, "Two");
Console.WriteLine(number.Numeric + " " + number.Textual);
// The following statement produces a run-time exception
// because in this example the method invocation
// expects two arguments.
// number(0);
}
}
// This code example produces the following output:
// 1 One
// 2 Two
備註
從該 DynamicObject 類別衍生的類別可以覆寫此方法,以指定如何對動態物件執行呼叫物件的操作。 當方法未被覆寫時,語言的執行時綁定器決定行為。 (大多數情況下會拋出執行時例外。)
如果此方法被覆寫,當你有像 sampleObject(100)這樣的操作時,該方法會自動被呼叫,其中 sampleObject 是從 DynamicObject 類別衍生出來的。
呼叫物件的操作在 C# 中支援,但在 Visual Basic 中則不支援。 Visual Basic編譯器從未產生使用此方法的程式碼,且Visual Basic語言不支援像 sampleObject(100) 這樣的語法。