急:Type如何转变为泛型T?

niss 2010-09-09 11:40:58
示例:

//基类中需要用到GenClass,但myProperty却必须赋值为子类
public class GenClass<T>{
public T myProperty;
public GenClass(T o){
this.myProperty = o;
}
}

//基类
public class baseClass{
//定义一个基类方法,为子类调用
public void doIt(){
Type t = this.GetType();
object o = new GenClass<t>(this);
}
}

//子类
public class childClass:baseClass{
public void method(){
//调用基类方法
this.doIt();
}
}


报错,未知的t类型,我知道T和t是类和对象的关系,可现在我怎么实现上面的想法呢?我需要在基类中实现doIt,但doIt需要传入子类,而子类类型是动态的啊.
另外,我不希望doIt<T>(this),毕竟调用自身方法居然还要把this做参数,是不是太失败了
...全文
788 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
niss 2010-09-09
  • 打赏
  • 举报
回复
这么说吧,我需要把子类进行xml序列化,而不是基类...
//基类
public class baseClass{
//定义一个基类方法,为子类调用
public void doIt(){
Type t = this.GetType();
object o = new GenClass<t>(this);
//xml序列化子类
......
}
}
zhangluotian 2010-09-09
  • 打赏
  • 举报
回复


来学习学习
Michael193 2010-09-09
  • 打赏
  • 举报
回复

object o = new GenClass<baseClass>(this);

按照你的情况, 之前的语句是 object o = new GenClass<this.GetType()>(this); 这里传给GenClass的T永远都是baseClass或者是baseClass的子类, 所以, 直接使用baseClass作为约束就可以了;
sjhcsdn 2010-09-09
  • 打赏
  • 举报
回复
用dynamic
边城的刀声 2010-09-09
  • 打赏
  • 举报
回复
或者是这样

//基类中需要用到GenClass,但myProperty却必须赋值为子类
public class GenClass<T> where T : baseClass
{
public T myProperty;
public GenClass(T o)
{
if (o.GetType() == typeof(baseClass)) throw new NotSupportedException("不支持基类");
this.myProperty = o;
}
}

//基类
public abstract class baseClass
{
protected baseClass()
{
}

//定义一个基类方法,为子类调用
public void doIt<T>() where T : baseClass
{

object o = new GenClass<T>((T) this);
}
}

//子类
public class childClass : baseClass
{
public void method()
{
//调用基类方法
this.doIt<childClass>();
}
}
ChrisAK 2010-09-09
  • 打赏
  • 举报
回复
using System;
//基类中需要用到GenClass,但myProperty却必须赋值为子类
public class GenClass<T>{
public T myProperty;
public GenClass(T o){
this.myProperty = o;
}
}

//基类
public class baseClass{
//定义一个基类方法,为子类调用
public void doIt(){
Type t = this.GetType();
object o = Activator.CreateInstance(typeof(GenClass<>).MakeGenericType(t), this);
}
}

//子类
public class childClass:baseClass{
public void method(){
//调用基类方法
this.doIt();
}
}

不过按lz的情况,
如ls所说object o = new GenClass<baseClass>(this);足够了
边城的刀声 2010-09-09
  • 打赏
  • 举报
回复

//基类中需要用到GenClass,但myProperty却必须赋值为子类
public class GenClass<T> where T : baseClass
{
public T myProperty;
public GenClass(T o)
{
if (o.GetType() == typeof(baseClass)) throw new NotSupportedException("不支持基类");
this.myProperty = o;
}
}

//基类
public abstract class baseClass
{
protected baseClass()
{
}

//定义一个基类方法,为子类调用
public void doIt()
{
Type t = this.GetType();
object o = new GenClass<baseClass>(this);
}
}

//子类
public class childClass : baseClass
{
public void method()
{
//调用基类方法
this.doIt();
}
}
边城的刀声 2010-09-09
  • 打赏
  • 举报
回复
//基类中需要用到GenClass,但myProperty却必须赋值为子类
加个判断不就行了
或者是把基类的构造函数弄成private的,然后

public class GenClass<T> where T : new(){
public T myProperty;
public GenClass(T o){
this.myProperty = o;
}
}
兔子-顾问 2010-09-09
  • 打赏
  • 举报
回复
T只是一个运行时的类型替换,而替换后的T类型也只约束myProperty这个引用,基于面向对象的设计中,这个类型是基类还是子类并不影响调用,而传一个基类进去反而更好的抽象了。
兔子-顾问 2010-09-09
  • 打赏
  • 举报
回复
object o = new GenClass<baseClass>(this);
niss 2010-09-09
  • 打赏
  • 举报
回复
想简化代码编写量啊,子类直接调用基类的方法就行了,但没想到基类方法不认识子类的类型(用typeof或gettype都不能转化成T啊)
ztenv 2010-09-09
  • 打赏
  • 举报
回复
曾经也想过办法,但后来想想这是不行的,一个是运行时确定的类型,一个是编译时确定的类型,你怎么搞到一起呢?
niss 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wuyazhe 的回复:]
xml序列化,应该用不着这么写泛型


C# code
XmlSerializer xmlSerializer = new XmlSerializer(o.GetType());
TextWriter writer = new StreamWriter(path);
try
{
xmlSerializer.Serialize(writer, items); ……
[/Quote]
恩,但我是想把所有继承基类的子类的xml序列化方法放在基类里实现,这样就直接用一个this.doXML()就实现了自身序列化了,方便省事
兔子-顾问 2010-09-09
  • 打赏
  • 举报
回复
xml序列化,应该用不着这么写泛型

XmlSerializer xmlSerializer = new XmlSerializer(o.GetType());
TextWriter writer = new StreamWriter(path);
try
{
xmlSerializer.Serialize(writer, items);
}
finally
{
writer.Close();
}
niss 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bclz_vs 的回复:]
或者是这样

C# code

//基类中需要用到GenClass,但myProperty却必须赋值为子类
public class GenClass<T> where T : baseClass
{
public T myProperty;
public GenClass(T o)
{
i……
[/Quote]

这个正解,但是还弄不太明白....

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧