简体   繁体   English

将非泛型类型转换为通用类型

[英]Casting a non-generic type to a generic one

I've got this class: 我有这门课:

class Foo { 
    public string Name { get; set; }
}

And this class 而这堂课

class Foo<T> : Foo {
    public T Data { get; set; }
}

Here's what I want to do: 这就是我想要做的事情:

public Foo<T> GetSome() {
    Foo foo = GetFoo();

    Foo<T> foot = (Foo<T>)foo;
    foot.Data = GetData<T>();
    return foot;
}

What's the easiest way to convert Foo to Foo<T>? 将Foo转换为Foo <T>的最简单方法是什么? I can't cast directly InvalidCastException) and I don't want to copy each property manually (in my actual use case, there's more than one property) if I don't have to. 我无法直接转换InvalidCastException)并且我不想手动复制每个属性(在我的实际用例中,有多个属性),如果我不需要。 Is a user-defined type conversion the way to go? 是用户定义的类型转换方式吗?

You can create an explicit conversion from Foo within Foo<T> . 您可以在Foo<T>从Foo创建显式转换。

class Program
{
    static void Main()
    {
        Foo foo = new Foo();
        foo.Name = "Blah";
        Foo<int> newfoo = (Foo<int>)foo;
        Console.WriteLine(newfoo.Name);
        Console.Read();
    }
}

class Foo
{
    public string Name { get; set; }
    public object Data { get; set; }
}

class Foo<T>
{
    public string Name { get; set; }
    public T Data { get; set; }
    public static explicit operator Foo<T>(Foo foo)
    {
        Foo<T> newfoo = new Foo<T>();
        newfoo.Name = foo.Name;
        return newfoo;
    }
}

Edit: This only works without inheritance. 编辑:这只适用于没有继承。 It appears you are not able to do a user-defined conversion from a base to a derived class. 您似乎无法执行从基础到派生类的用户定义转换。 See comments from Mads Torgersen here http://social.msdn.microsoft.com/forums/en-US/csharplanguage/thread/14cf27cf-b185-43d6-90db-734d2ca3c8d4/ : 请参阅Mads Torgersen的评论http://social.msdn.microsoft.com/forums/en-US/csharplanguage/thread/14cf27cf-b185-43d6-90db-734d2ca3c8d4/

We have taken the liberty of predefining conversions (casts) between base classes and derived classes, and to make the semantics of the language predictable we don't allow you to mess with it . 我们冒昧地在基类和派生类之间预定义转换(强制转换),并使语言的语义可预测, 我们不允许你搞乱它

It looks like you may be stuck with defining a method to turn a Foo into a Foo<T> . 看起来您可能会遇到定义将Foo变为Foo<T> That, or drop the inheritance. 那,或者放弃继承。 Neither solution sounds particularly ideal. 两种解决方案都听起来不太理想

If you are getting an InvalidCastException , the Foo type returned by GetFoo() is not Foo<T> . 如果您收到InvalidCastException ,则GetFoo()返回的Foo类型不是Foo<T> You will need to either pass T or typeof(T) to that function so it can return an instance of Foo<T> . 您需要将Ttypeof(T)传递给该函数,以便它可以返回Foo<T>的实例。

Use copy constructors. 使用复制构造函数。 Foo implements: Foo实现:

class Foo {
     Foo(Foo copy) { ... }
}

while Foo shall be constructed using the following: 而Foo应使用以下内容构建:

class Bar<T> : Foo {
      Bar(Foo copy) : base(copy) { ... }
}

...Yes. ...是。 You need to copy member by member, and this should be done in "copy constructor". 您需要按成员复制成员,这应该在“复制构造函数”中完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM