我基本上希望做的是设计一个通用接口,在实现时,会产生一个类似于T的类,除了它有一些额外的功能.这是我正在谈论的一个例子:
public interface ICoolInterface<T>
{
T Value { get; set; }
T DoSomethingCool();
}
public class CoolInt : ICoolInterface<int>
{
private int _value;
public CoolInt(int value)
{
_value = value;
}
public int Value
{
get { return _value; }
set { _value = value; }
}
public int DoSomethingCool()
{
return _value * _value;
// Ok, so that wasn't THAT cool
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但为了使用CoolInt,我需要做这样的事情:
CoolInt myCoolInt = new CoolInt(5);
int myInt = myCoolInt.Value;
Run Code Online (Sandbox Code Playgroud)
至少在分配方面,我更倾向于CoolInt就像int一样工作.换一种说法:
CoolInt myCoolInt = 5;
int …Run Code Online (Sandbox Code Playgroud)