是否有内置的FxCop规则可以分析不必要的Boxing / UnBoxing?
以下示例显示了一个违反规则的方法WeaklyTyped和一个满足规则的方法StronglyTyped。
using System;
namespace PerformanceLibrary
{
public interface IWork
{
object DoWork();
}
public class Work : IWork
{
object IWork.DoWork()
{
return 3;
}
public int DoWork()
{
return 3;
}
}
public class NeedsWork
{
public void WeaklyTyped()
{
IWork iwork = new Work();
// The following call violates the rule.
int x = (int)iwork.DoWork();
}
public void StronglyTyped()
{
Work work = new Work();
int x = work.DoWork();
}
}
}