110,477
社区成员
发帖
与我相关
我的任务
分享
//基类中需要用到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();
}
}
//基类中需要用到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>();
}
}
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();
}
}
//基类中需要用到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();
}
}
XmlSerializer xmlSerializer = new XmlSerializer(o.GetType());
TextWriter writer = new StreamWriter(path);
try
{
xmlSerializer.Serialize(writer, items);
}
finally
{
writer.Close();
}