ConditionalWeakTable<TKey,TValue>.CreateValueCallback 代理人

定義

代表方法,這個方法會建立非預設值,以作為索引鍵/值組的一部分加入 ConditionalWeakTable<TKey,TValue> 物件。

C#
public delegate TValue ConditionalWeakTable<TKey,TValue>.CreateValueCallback(TKey key) where TKey : class where TValue : class;

參數

key
TKey

屬於所要建立之值的索引鍵。

傳回值

TValue

參考類型的執行個體,代表要附加至所指定索引鍵的值。

範例

下列範例會 MainClass 定義 類別和 MainInfo 類別。 MainInfo 提供實例的相關信息 MainClass 。 它也定義 Visual Basic) 方法中的靜態 (SharedCreateAttachedValue ,可以指派給 ConditionalWeakTable<TKey,TValue>.CreateValueCallback 委派並傳遞至 GetValue 方法。 此範例會GetValue呼叫 方法,將 物件及其對應的 MainInfo 物件新增MainClassConditionalWeakTable<TKey,TValue>數據表。 此範例也會說明對 和 GetOrCreateValue 方法的呼叫Add,以將索引鍵/值組新增至數據表,以及呼叫 TryGetValue 方法來擷取屬於現有索引鍵的值。

C#
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

public class Example
{
   string Name; 
   
   public Example(string name)
   {
      this.Name = name;
   }
   
   public override string ToString()
   {
      return this.Name;
   }
}

// Define a class to contain information about each Example instance.
public class ExampleInfo
{
   public string Name;
   public int Methods;
   public int Properties;
   
   public override string ToString()
   {
      return String.Format("{0}: {1} Methods, {2} Properties", 
                           this.Name, this.Methods, this.Properties);
   }
}

public class ExampleTest
{
   private static BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

   public static void Main()
   {
      Example ex1 = new Example("ex1");
      Example ex2 = new Example("ex2");
      Example ex3 = new Example("ex3");
      
      ExampleInfo exInfo1 = new ExampleInfo(); 
      exInfo1.Name = ex1.ToString();
      exInfo1.Methods = ex1.GetType().GetMethods(flags).Length;
      exInfo1.Properties = ex1.GetType().GetProperties(flags).Length;
      
      ExampleInfo exInfo3 = new ExampleInfo(); 
      exInfo3.Name = ex3.ToString();
      exInfo3.Methods = ex3.GetType().GetMethods(flags).Length;
      exInfo3.Properties = ex3.GetType().GetProperties(flags).Length;

      var attached = new ConditionalWeakTable<Example, ExampleInfo>();
      ExampleInfo value = null;

      // Attach a property to ex1 using the Add method, then retrieve it.
      attached.Add(ex1, exInfo1);
      if (attached.TryGetValue(ex1, out value))
         Console.WriteLine("{0}, {1}", ex1, value);
      else
         Console.WriteLine("{0} does not have an attached property.", ex1);

      // Attempt to retrieve the value attached to ex2.
      value = attached.GetValue(ex2, ExampleTest.CreateAttachedValue);      
      if (attached.TryGetValue(ex2, out value))
         Console.WriteLine("{0}, {1}", ex2, value);
      else 
         Console.WriteLine("{0} does not have an attached property.", ex2);
      
      // Attempt to retrieve the value attached to ex3.
      value = attached.GetOrCreateValue(ex3);
      Console.WriteLine("{0}, {1}", ex3, value);
   }

   public static ExampleInfo CreateAttachedValue(Example ex)
   {
      ExampleInfo info = new ExampleInfo();
      info.Name = ex.ToString();
      info.Methods = ex.GetType().GetMethods(flags).Length;
      info.Properties = ex.GetType().GetProperties(flags).Length;
      return info;
   }
}
// The example displays the following output:
//       ex1, ex1: 4 Methods, 0 Properties
//       ex2, ex2: 4 Methods, 0 Properties
//       ex3, : 0 Methods, 0 Properties

備註

委派ConditionalWeakTable<TKey,TValue>.CreateValueCallback會封裝在 物件中找不到傳遞至 方法的索引鍵時,由方法叫用的ConditionalWeakTable<TKey,TValue>回呼方法ConditionalWeakTable<TKey,TValue>.GetValue。 方法 GetValue 會將回呼方法傳遞索引鍵,代表要動態附加屬性值的Managed物件。 方法負責將屬性值傳回給其呼叫端,進而將索引鍵/值組新增至數據表。

方法 ConditionalWeakTable<TKey,TValue>.CreateValueCallback 可用來傳回可附加至指定索引鍵之參考型別的實例。 它可讓該實例使用非預設值初始化。 如果數據表中找不到索引鍵,此方法會 ConditionalWeakTable<TKey,TValue>.GetOrCreateValue 新增索引鍵/值組,其中會使用預設值初始化值。

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱