Static Type cant be used as type arguments
BUT
Extension Method should be in a Static class
I have a class piece
so I can create List<piece>
in my code
Now when I try to create an extension and ask me to create the class as static. But if I do that can't create List<piece>
. So I'm in a recursive dilema here.
As you can see I'm trying to clone my piece.
public class piece
{
public int height { get; set; }
public int width { get; set; }
public int[] shape { get; set; }
public int quality { get; set; }
public int x { get; set; }
public int y { get; set; }
public static T CloneJson<T>(this T source)
{
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source));
}
}
So what is the proper way to handle this scenario?
I have my class working fine. I try to add clone functionality and found that extension method. That is when mess start, yes, I'm not really familiar with extension or instance method. I'm trying to make it work
My guess is create the extension as a method instead, but I don't know how to do that.