C# type rebinding, very ngerful

C# type rebinding, a program demonstrating what I can figure out about simulating
 generic type rebinding in C# (which is so common in C++ programs), it may look
 stupid, but it really works as intended.

 

 using System; class BaseG { public class ReboundType<_T> { public virtual _T T {get; set;} public virtual void Do() {} } public virtual ReboundType<_T> Rebind<_T>() { return new ReboundType<_T>(); } } class G : BaseG { public new class ReboundType<_T> : BaseG.ReboundType<_T> { _T t; public override _T T { get {return t;} set{t = value;} } public override void Do() { Console.WriteLine("G.ReboundType<>.t = {0}", t); } } public override BaseG.ReboundType<_T> Rebind<_T>() { return new ReboundType<_T>(); } } class User<_G> where _G : BaseG, new() { BaseG.ReboundType<String> gs = new _G().Rebind<String>(); public void Test() { gs.T = "abc"; gs.Do(); } } class App { static void Main(String[] args) { User<G> ug = new User<G>(); ug.Test(); } }

你可能感兴趣的:(c,String,C#,user,Class)