C#泛型拷贝构造函数 [英] C# Generic Copy Constructor

查看:162
本文介绍了C#泛型拷贝构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口,以及一个实现接口两班。该班有泛型类型。我想从一类到其他的实例克隆。

I have an interface, and two classes that implements the interface. The classes have generic types. I would like to clone from an instance of one class to the other.

interface IFoo
{
    // stuff
}

class Foo<T> : IFoo
{
    // foo stuff
    // ifoo implementation
}

class Bar<T> : IFoo
{
    // bar stuff
    // ifoo implementation
}

我有一个Foo和想一个吧。酒吧设有一个拷贝构造函数采取的IFoo的参数。
我创建一个扩展方法来实现克隆:

I have a Foo and would like a Bar. Bar has a copy constructor taking a parameter of IFoo. I created an extension method to implement the clone:

public static Bar<T> Clone<T>(this IFoo foo) 
{
    return new Bar<T>(foo);
}



调用该方法需要的类型:

Calling the method requires the type:

someFoo.Clone<T> ...



有没有办法省略通过修改扩展方法调用该方法时声明类型,或任何其他方式,允许实例只是可以不关心其底层类型传递?

Is there a way to omit declaring the type when calling the method by modifying the extension method, or any other way, to allow the the instance to just be passed in without caring about its underlying type?

更新
这里是如何被使用以更好地说明这种情况。

Update Here is how this is being used to better illustrate the situation.

在一个方法我迭代集合并返回的IFoo的枚举。在该方法我看源收集的属性,并确定美孚的类型

In a method I iterate a collection and return an enumeration of IFoo. In the method I look at an attribute of the source collection and determine the type of the Foo.

IFoo foo = null;

string type = element.Attribute("Type").Value;
switch (type)
{
    case "int":
        foo = new Foo<int>();
        break;

    case "string":
        foo = new Foo<string>();
        break;

    // etc
}
// other stuff

yield return foo;



中的调用方法有一个列表。后来我从此列表中选择使用,我想一个酒吧,而不是一个Foo在这一点上的各个项目。从列表中选择当实例类型的IFoo的,如他们只看到了这个IFoo的foo的扩展方法。我不想给IFoo的转换为富,这将需要重新声明我只是想有富告诉酒吧是什么T的类型。这可能吗?

The calling method has a List. Later I select individual items from this list for use, at which point I would like a Bar instead of a Foo. When selecting from the list the instances are of type IFoo, as in they only see the extension methods for "this IFoo foo". I do not want to cast the IFoo to a Foo, that would require re-declaring the type of T. I would just like to have Foo tell Bar what it is. Is this possible?

推荐答案

所以,你必须一切就OK了,但是要能够使用语法

So you have everything ok but you want to be able to use the syntax

someFoo.Clone()

而不是

someFoo.Clone<int>()

你想要的int值暗示,而不是把它疗法明确?

you want the int to be implied rather than having to put it ther explicitly?

究其原因,你可以'T这样做,在你的代码示例是IFoo的不引用,你需要为了创建通用类型T 酒吧< T>

The reason you can't do that in your code example is that IFoo doesn't reference the generic type T that you need in order to create Bar<T>.

我建议你真的需要另一个接口:的IFoo< T>

I would suggest that you really need another interface: IFoo<T>

如果你有这样的,你的扩展方法是这样的:

If you had that, and your extension method looks like this:

public static Bar<T> Clone<T>(this IFoo<T> foo) 
{
    return new Bar<T>(foo);
}



然后,你将有使用没有问题。

Then you will have no problem using

IFoo<int> someFoo = new Foo<int>();
Bar<int> someBar = someFoo.Clone();



希望有所帮助。

Hope that helps

这篇关于C#泛型拷贝构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆