나는 것 고려 일반적으로 Lazy<T>
이 작업을 수행 :
public static class Test // this is your ClassB
{
private static Lazy<string> m_Property = new Lazy<string>(()=>
{
Console.WriteLine("Lazy invoked");
return "hi";
},true);
public static string Property
{
get
{
Console.WriteLine("value request");
return m_Property.Value;
}
}
}
//....consuming code, this is in your ClassA somewhere
var test1 = Test.Property; // first call, lazy is invoked
var test2 = Test.Property; // cached value is used, delgate not invoked
var test3 = Test.Property; // cached value is used, delgate not invoked
var test4 = Test.Property; // cached value is used, delgate not invoked
var test5 = Test.Property; // cached value is used, delgate not invoked
이 난을 출력 :
value request
Lazy invoked
value request
value request
value request
value request
아마도 가장 좋은 디자인입니다. 나는 당신이 무엇을 찾고 있는지 분명하지 않다. –