基类里面的一个方法需要能够使用或者返回子类的对象(较难)

lccleo 2016-11-12 03:34:10
上午在写了一个序列化克隆对象的方法


/// <summary>
/// 通过内存序列化的办法克隆一个对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static T MemoryClone<T>(T t) where T : class //已测试
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(stream, t);
stream.Position = 0; //stream.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(stream) as T;
}

这段代码 大家应该很熟悉
然后我有很多类 都需要有这个clone 方法,这些类有自己的基类,所以我想让基类里面有这个clone 方法就行了

[Serializable]
class hehe<T> : EventArgs where T : class
{
public int a { get; set; }
public int b { get; set; }
public T Clone()
{
return MemoryClone<T>(this as T);
}
}
[Serializable]
class hehe1 : hehe<hehe1>
{
public int c { get; set; }
}

所以我写了一个这样的东西,这样 所有的子类 都有了clone 方法,但是我不确定我这种做法还有没改进之处,感觉有点虚,特别是这句:return MemoryClone<T>(this as T); 强制将父类的对象 转成了 子类的对象类型
请大神帮忙改改错 谢谢
我需要做的就是 在父类里面 能引用到子类的对象 返回子类的对象类型 求大神指导下该怎么做?
...全文
234 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lccleo 2016-11-18
  • 打赏
  • 举报
回复
自顶自顶自顶自顶自顶自顶自顶
lccleo 2016-11-13
  • 打赏
  • 举报
回复
难道我的代码已经完美了
lccleo 2016-11-12
  • 打赏
  • 举报
回复
引用 3 楼 tcmakebest 的回复:
把扩展类放到项目中, 下面这句就可以用了.
hehe1 newobj = new hehe1().Clone();
我自己写的代码已经实现了功能 我只是感觉自己写的不好 怕有bug 想让大神挑挑错 有没有更好地实现方法 没有最好的代码 只有更好的代码
tcmakebest 2016-11-12
  • 打赏
  • 举报
回复
把扩展类放到项目中, 下面这句就可以用了.
hehe1 newobj = new hehe1().Clone();
lccleo 2016-11-12
  • 打赏
  • 举报
回复
引用 1 楼 tcmakebest 的回复:
MemoryClone<T> 已经能够克隆楼主需要的类了, 如果一定要在子类上调用克隆方法, 使用下面的"扩展"也是可以的.
public static class CloneExtention
{
    public static T MemoryClone<T>(T t) where T : class    //已测试
    {
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        formatter.Serialize(stream, t);
        stream.Position = 0;   //stream.Seek(0, SeekOrigin.Begin);
        return formatter.Deserialize(stream) as T;
    }
    public static T Clone<T>(this T t) where T : hehe
    {
        return MemoryClone(t); 
    }
}
没看出来你这个 CloneExtention 类是干啥用的。。。
tcmakebest 2016-11-12
  • 打赏
  • 举报
回复
MemoryClone<T> 已经能够克隆楼主需要的类了, 如果一定要在子类上调用克隆方法, 使用下面的"扩展"也是可以的.
public static class CloneExtention
{
    public static T MemoryClone<T>(T t) where T : class    //已测试
    {
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        formatter.Serialize(stream, t);
        stream.Position = 0;   //stream.Seek(0, SeekOrigin.Begin);
        return formatter.Deserialize(stream) as T;
    }
    public static T Clone<T>(this T t) where T : hehe
    {
        return MemoryClone(t); 
    }
}

110,536

社区成员

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

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

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