C# 在类内替换类实例
我有一个抽象类C# 在类内替换类实例,c#,class,runtime,this,C#,Class,Runtime,This,我有一个抽象类A,它可以将自身序列化到字节[] Class C <T> where T : A, new() { ... } 另一个类C用类型T参数化,该类型应为A或从A继承,并具有无参数构造函数C需要在T和byte[]之间进行双向转换 Class C <T> where T : A, new() { ... } 我可以工作,但在许多情况下,我只能将字节转换为T的新对象。 有没有更好的解决方案或方法来做某事,比如: public class SomeTClass :
A
,它可以将自身序列化到字节[]
Class C <T> where T : A, new() { ... }
另一个类C
用类型T
参数化,该类型应为A
或从A继承,并具有无参数构造函数C
需要在T
和byte[]
之间进行双向转换
Class C <T> where T : A, new() { ... }
我可以工作,但在许多情况下,我只能将字节转换为T
的新对象。
有没有更好的解决方案或方法来做某事,比如:
public class SomeTClass : A
{
public SomeTClass(){...}
public void LoadFromBytes(byte[] bytes)
{
SomeTClass newT = Sth(bytes); /* new instance of SomeTClass
is created from bytes */
this = newT; /* can't do this, but I need to replace
current instance with the new one */
}
}
看看UpdateReference
方法和反序列化实现。我认为您应该将反序列化方法设置为farbic方法
。它应该将byte[]
作为输入参数,并返回所需类型的新实例。
这是你想要的吗
class C <T> where T : IA, new()
{
public T Data { get; set; }
.....
public UpdateReference()
{
byte[] data = GetBytesFromSomewhere();
Data = AImpl.Deserialize(data);
Data.UserfulMethod();
Data.AnotherUserfulMethod();
data = GetBytesFromSomewhere();
Data = AImpl.Deserialize(data)
Data.UserfulMethod();
Data.AnotherUserfulMethod();
}
}
public interface IA
{
public byte[] Serialize();
public A Deserialize(byte[] data);
public string UsefuleMethod1();
public int AnotherUsefulMethod();
}
public class AImpl : IA
{
public byte[] Serialize()
{
//Concrete implementation serialization
}
public static IA Deserialize(byte[] data)
{
//Concrete implementation deserialization
}
}
C类,其中T:IA,new()
{
公共T数据{get;set;}
.....
公共更新引用()
{
字节[]数据=GetBytesFromSomewhere();
数据=AImpl.反序列化(数据);
Data.UserfulMethod();
Data.AnotherUserfulMethod();
data=GetBytesFromSomewhere();
数据=AImpl.反序列化(数据)
Data.UserfulMethod();
Data.AnotherUserfulMethod();
}
}
公共接口
{
公共字节[]序列化();
公共A反序列化(字节[]数据);
公共字符串UsefuleMethod1();
公共int另一个有用的方法();
}
公共类AImpl:IA
{
公共字节[]序列化()
{
//具体实现序列化
}
公共静态IA反序列化(字节[]数据)
{
//反序列化的具体实现
}
}
查看更新引用
方法和反序列化实现。我认为您应该将反序列化方法设置为farbic方法
。它应该将byte[]
作为输入参数,并返回所需类型的新实例。
这是你想要的吗
class C <T> where T : IA, new()
{
public T Data { get; set; }
.....
public UpdateReference()
{
byte[] data = GetBytesFromSomewhere();
Data = AImpl.Deserialize(data);
Data.UserfulMethod();
Data.AnotherUserfulMethod();
data = GetBytesFromSomewhere();
Data = AImpl.Deserialize(data)
Data.UserfulMethod();
Data.AnotherUserfulMethod();
}
}
public interface IA
{
public byte[] Serialize();
public A Deserialize(byte[] data);
public string UsefuleMethod1();
public int AnotherUsefulMethod();
}
public class AImpl : IA
{
public byte[] Serialize()
{
//Concrete implementation serialization
}
public static IA Deserialize(byte[] data)
{
//Concrete implementation deserialization
}
}
C类,其中T:IA,new()
{
公共T数据{get;set;}
.....
公共更新引用()
{
字节[]数据=GetBytesFromSomewhere();
数据=AImpl.反序列化(数据);
Data.UserfulMethod();
Data.AnotherUserfulMethod();
data=GetBytesFromSomewhere();
数据=AImpl.反序列化(数据)
Data.UserfulMethod();
Data.AnotherUserfulMethod();
}
}
公共接口
{
公共字节[]序列化();
公共A反序列化(字节[]数据);
公共字符串UsefuleMethod1();
公共int另一个有用的方法();
}
公共类AImpl:IA
{
公共字节[]序列化()
{
//具体实现序列化
}
公共静态IA反序列化(字节[]数据)
{
//反序列化的具体实现
}
}
我设法解决了这个问题,但我不喜欢我创建的代码
其思想是使用T参数化类A,并创建抽象方法,如果不是从模板类型使用该方法,该方法将是静态的:
public abstract class A <T>
{
public abstract byte[] Serialize();
public abstract T Deserialize(byte[]); //should be static
}
以及一些T
实现:
public class SomeTClass : A<SomeTClass>
{
public SomeTClass Deserialize(byte[])
{
//deserialization code
}
}
公共类SomeTClass:A
{
公共SomeTClass反序列化(字节[])
{
//反序列化代码
}
}
我设法解决了这个问题,但我不喜欢我创建的代码
其思想是使用T参数化类A,并创建抽象方法,如果不是从模板类型使用该方法,该方法将是静态的:
public abstract class A <T>
{
public abstract byte[] Serialize();
public abstract T Deserialize(byte[]); //should be static
}
以及一些T
实现:
public class SomeTClass : A<SomeTClass>
{
public SomeTClass Deserialize(byte[])
{
//deserialization code
}
}
公共类SomeTClass:A
{
公共SomeTClass反序列化(字节[])
{
//反序列化代码
}
}
您使用的序列化类型是什么?这很重要…你的第二句话有点让人困惑,C
需要是T
,但是你要约束的是A
。你有一个类工厂,每个类型都需要一个实例吗?所以Sth
是一个从SomeTClass
继承的类吗?@Ari好吧,总是可以选择使用反射来映射你的属性。或者,如果您不介意使用第三方库,那么总会有像/这样的工具是为这个特定目的而构建的?这很重要…你的第二句话有点让人困惑,C
需要是T
,但是你要约束的是A
。你有一个类工厂,每个类型都需要一个实例吗?所以Sth
是一个从SomeTClass
继承的类吗?@Ari好吧,总是可以选择使用反射来映射你的属性。或者,如果您不介意使用第三方库,那么总会有像/这样的工具用于此特定目的。在您的代码中,仅允许将AImpl
用作IA。我的意思是,从IA
派生的所有类都可以有自己的[de]序列化程序。您希望您的代码如何决定从字节[]
反序列化哪个类?例如,您有AImpl1、AImpl2和AImpl3。你的代码只知道byte[]
-应该调用谁的反序列化方法?你可以在byte[]
前面加上某种标记字节,用fabric方法将接口转换为抽象类。这种方法将分析标记字节并调用相应的反序列化程序。C
实例的实际类为T
,并且可以使用从A
派生的方法。问题是它不能使用t
中的静态方法。如果可以的话,这个问题可以通过T
中的静态方法轻松解决,获取byte[]
并在您的代码中返回T
(创建这样的方法没有问题),只允许将AImpl
用作IA。我的意思是,从IA
派生的所有类都可以有自己的[de]序列化程序。您希望您的代码如何决定从字节[]
反序列化哪个类?例如,您有AImpl1、AImpl2和AImpl3。您的代码只知道字节[]
-应该调用谁的反序列化方法?您可以在字节[]
前面加上某种标记字节,将接口转换为