d 想要写入一个 C 类, 以便它有一个静态字段, 该字段一次初始化, 并且对所有 C< & gt; 保持恒定 。
有可能吗?
这里我所合作的例子如下:
void Main()
{
Console.WriteLine (C<A>.Value);
Console.WriteLine (C<B>.Value);
Console.WriteLine (C<A>.Value2);
Console.WriteLine (C<B>.Value2);
}
class C<T>
{
static O O = new O();
public static string Value { get { return "value of C<T> where T : " + typeof(T).Name; } }
public static string Value2 { get { return O.ToString() ?? "value of C<T> where T : " + typeof(T).Name; } }
}
class O
{
Guid guid;
public O()
{
this.guid = Guid.NewGuid();
Console.WriteLine ("O created {0}", this.guid);
}
public override string ToString()
{
return this.guid.ToString();
}
}
class A
{
}
class B
{
}
<强>产出: 强>
value of C<T> where T : A
value of C<T> where T : B
O created 05e6885b-2aa3-4d0d-aec1-fefbe5294f07
05e6885b-2aa3-4d0d-aec1-fefbe5294f07
O created 864554bf-6980-4a0f-9b63-4c0affa21222
864554bf-6980-4a0f-9b63-4c0affa21222