I want to clone a generic object and preserve its type.
run.Append(style.Clone(BlackFont)); //run object accepts only RunProperties objects
public T Clone(T what) {
if (what is RunProperties)
return (T) what.Clone();
}
It doesn’t work since T type does not have a Clone method, how can I overcome this without casting in the first statement.
run.Append((RunProperties) style.Clone(BlackFont)); //I do not want this
//not that this will work since you can't convert T to RunProperties
Thanks for any help.
—EDIT—
It seems that it would be better for my not to use generics in this case. I’ll split up the data.
You could always constrain the method to only accept types that implement the ICloneable interface:
But since your method really only works with one type, you could change it slightly and use the
asoperator also: