I am porting an existing .NET class library to a Portable Class Library. The .NET library makes extensive use of the ICloneable interface, which is not included in the portable subset.
Typically, I am faced with class definitions like this in the .NET class library:
public class Foo<T> where T : ICloneable
{
public void Bar(T item) {
var x = item.Clone();
...
}
}
What can I do to successfully port this code to a Portable Class Library? Do I have to rewrite the Clone
method invocations, or is there a less intrusive workaround?
I cannot simply remove the generic type constraint where T : ICloneable
because then the Bar
method will not compile.
I could write up a replacement interface to use instead of ICloneable
in the ported code:
public interface IPCLCloneable {
object Clone();
}
This would work as long as I only instantiate Foo<T>
with classes that implement IPCLCloneable
, but it will not work for example with types from the core libraries that implement ICloneable
, such as Array:
var x = new Foo<int[]>(); // Compilation error in PCL library
(For completeness, it should be pointed out that the ICloneable
interface is not explicitly implemented in the portable subset core libraries since it does not exist, but the object Clone()
method does exist in the portable subset Array
class, and consequentially for array implementations such as int[]
.)
What other options do I have?
int[].Clone()
is available in PCL, it is only theICloneable
interface that isn't there. And thanks for the suggestions. The "prettier" the better, of course, but in the end I might have to do some major refactoring after all... – NonsuitICloneable
yourself? – Hypervitaminosisint[].Clone()
and similar. – NonsuitICloneable
interface, and in theory I agree that usage should be avoided. In practice however ... it will be quite a lot of work to replace it in the third party library I am trying to port. Hence the request for efficient workarounds. – Nonsuit