我有一个C#接口,其中某些方法参数声明为object类型.但是,传递的实际类型可能因实现接口的类而异:
public interface IMyInterface
{
    void MyMethod(object arg);
}
public class MyClass1 : IMyInterface
{
    public void MyMethod(object arg)
    {
        MyObject obj = (MyObject) arg;
        // do something with obj...
    }
}
public class MyClass2 : IMyInterface
{
    public void MyMethod(object arg)
    {
        byte[] obj = (byte[]) arg;
        // do something with obj...
    }
}
MyClass2的问题在于转换byte[]和转换object是装箱和拆箱,这是影响性能的计算上昂贵的操作.
用通用接口解决这个问题会避免装箱/拆箱吗?
public interface IMyInterface<T>
{
    void MyMethod(T arg);
}
public class MyClass1 : IMyInterface<MyObject> …