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.
As Sayse mentioned in the comment, you are trying to use T as a static method and need an instance. For example,
public sealed partial class Module<T, U> : IModule<T, U>
where T : BaseBox
where U : BaseItem
{
private T _box;
public Module(T box)
{
_box = box;
}
U[] IModule<T, U>.GetItems<T>( int id )
{
// You need to determine how id relates to _box.
return _box.Unboxing();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With