C# type rebinding, very ngerful

本文介绍了一个C#程序示例,展示了如何在C#中模拟泛型类型重绑定的功能,这是C++程序中常见的特性。通过具体的代码实现,解释了泛型类型重绑定的基本原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(); } }

转载于:https://www.cnblogs.com/quanben/archive/2009/04/11/3128945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值