[英]Cast generic class object to non-generic
I have 2 classes:我有 2 节课:
public class GenericClass<T>
{
public T Item {get;set;}
}
public class StringClass
{
public string Item {get;set;}
}
now i have a GenericClass object and i need to cast it to StringClass object:现在我有一个 GenericClass object,我需要将它转换为 StringClass object:
var genericObj = new GenericClass<string>();
var stringObj = (StringClass)genericObj; // <---
How to cast / convert from generic class to specific one?如何从通用 class 转换/转换为特定的?
You can't cast one type to the other because the types are unrelated.您不能将一种类型转换为另一种类型,因为这些类型是不相关的。
You could, however, define a conversion operator :但是,您可以定义一个转换运算符:
public class StringClass
{
public string Item { get; set; }
public static explicit operator StringClass(GenericClass<string> generic)
=> new StringClass { Item = generic.Item };
}
Which would allow this syntax:这将允许这种语法:
var genericObj = new GenericClass<string>();
var stringObj = (StringClass)genericObj;
You can't.你不能。 You would need common inherited type or implement an interface.
您将需要通用继承类型或实现接口。
With inheritance:使用 inheritance:
public class GenericClass<T>
{
public T Item {get;set;}
}
public class StringClass : GenericClass<string>
{
}
if your really need it, you can do this way for examle如果你真的需要它,你可以这样做例如
var stringObj = new StringClass(genericObj);
public class StringClass
{
public string Item { get; set; }
public StringClass(GenericClass<string> genericClass)
{
Item=genericClass.Item;
}
public StringClass(){}
}
or this is more flexible或者这更灵活
public interface IGenericClass<T>
{
public T Item { get; set; }
}
public class GenericClass<T>:IGenericClass<T>
{
public T Item { get; set; }
}
public class StringClass
{
public string Item { get; set; }
public StringClass(IGenericClass<string> genericClass)
{
Item=genericClass.Item;
}
public StringClass(){}
}
Finally i solved using ICloneable,最后我用ICloneable解决了,
Here i have a base class named GenericClass , a generic class named GenericClassT , and a class named StringClass .这里我有一个名为GenericClass的基础 class ,一个名为GenericClassT的通用 class 和一个名为 StringClass 的class 。
Inheritance is: Inheritance 是:
GenericClass <- GenericClassT <- StringClass GenericClass <- GenericClassT <- StringClass
Using ICloneable implementation on GenericClass and GenericClassT, adding a CreateObject and CopyTo methods i reach the final goal:在 GenericClass 和 GenericClassT 上使用 ICloneable 实现,添加 CreateObject 和 CopyTo 方法,我达到了最终目标:
var genericObj = new GenericClass<string>();
var stringObj = (StringClass)genericObj.Clone<StringClass>();
class definitions: class 定义:
public class GenericClass: ICloneable
{
public string Id {get;set;}
protected virtual ApiRequestResult CreateObject()
{
return new GenericClass();
}
protected virtual void CopyTo(GenericClass obj)
{
obj.Id = Id;
}
public virtual object Clone()
{
var obj = CreateObject();
CopyTo(obj);
return obj;
}
public virtual object Clone<T>() where T: GenericClass
{
var obj = (GenericClass)Activator.CreateInstance(typeof(T));
CopyTo(obj);
return obj;
}
}
public class GenericClass<T>: GenericClass
{
public T Data {get; set;}
protected override GenericClass CreateObject()
{
return new GenericClass<T>();
}
protected override void CopyTo(GenericClass obj)
{
base.CopyTo(obj);
((GenericClass<T>)obj).Data = Data;
}
}
public class StringClass: GenericClass<string>
{
}
Using this answer :使用这个答案:
var genericObj = new GenericClass<string>();
var stringObj = (StringClass)Convert.ChangeType(genericObj, typeof(StringClass));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.