I wont to use only one method to unbox like this:
public interface IModule<T, U> where T : BaseBox where U : BaseItem { U[] GetItems<T>( int id ); } public sealed partial class Module<T, U> : IModule<T, U> where T : BaseBox where U : BaseItem { U[] IModule<T, U>.GetItems<T>( int id ) { return T.Unboxing(); // It is wrong! } } But I can't. How do I have to write right generics?
Next code to understand.I have item's types:
public abstract class BaseItem { protected int _id; protected string _description; } public sealed class TriangleItem : BaseItem { public int TriangleId { get { return _id; } set { _id = value; } } public string TriangleDescription { get { return _description; } set { _description = value; } } public Color color { get; set; } } public sealed class CircleItem : BaseItem { public int CircleId { get { return _id; } set { _id = value; } } public string CircleDescription { get { return _description; } set { _description = value; } } public int Radius { get; set; } } Then I have boxes for items:
public abstract class BaseBox { public string ItemsXml { get; set; } public abstract BaseItem[] Unboxing(); } public sealed class TriangleBox : BaseBox { public TriangleItem[] Unboxing() { return Util.FromXml( ItemsXml ).Select( i => new TriangleItem { TriangleId = int.Parse( i ), TriangleDescription = i, Color = Color.Red } ).ToArray(); } } public sealed class CircleBox : BaseBox { public CircleItem[] Unboxing() { return Util.FromXml( ItemsXml ).Select( i => new CircleItem { CircleId = int.Parse( i ), CircleDescription = i, Radius = 5 } ).ToArray(); } } Here I have different implementations Unboxing-method.
T.Unboxing()as though it is a static method, you need an instance to useTas argument, or use yourint idto get it.