Why do I (sometimes) have to reference assemblies referenced by the assembly I reference?

后端 未结 5 1123
名媛妹妹
名媛妹妹 2020-12-06 10:53

I have an assembly A that defines an interface with some overloads:

public interface ITransform
{
    Point InverseTransform(Point point);
    Rect InverseTr         


        
5条回答
  •  天命终不由人
    2020-12-06 11:30

    namespace ClassLibrary1
    {
       public interface ITransform
       {
          dynamic InverseTransform(dynamic point);
       }
    }
    
    using ClassLibrary1;
    using Moq;
    namespace ConsoleApplication9
    {
       interface IPoint { }
       class Point : IPoint { }
    
       class Program
       {
          static void Main(string[] args)
          {
             var transform = new Mock();
             IPoint x = transform.Object.InverseTransform(new Point());
          }
       }
    }
    

    Instead of telling you what you can't do...

    A way to fix this would entail introducing IPoint Transform(IPoint x) as the only method in your interface, together with IPoint interface. This would mean that System.Drawing would have to comply to your IPoint as well.

    If you want that level of decoupling, dynamic keyword comes to mind, since you can't get Drawing.Point to implement an interface after-the-fact. Just be sure to have really great unit test coverage on this part of code, and expect it to perform somewhat slower.

    This way, you'd only have to reference System.Drawing only in assemblies where you're actually using it.

    EDIT Reflector says that the signature of System.Drawing.Point is

    [Serializable, StructLayout(LayoutKind.Sequential), TypeConverter(typeof(PointConverter)), ComVisible(true)]
    public struct Point { }
    

提交回复
热议问题