C#接口实现中的不同类型

发布于 2025-02-06 14:34:29 字数 790 浏览 2 评论 0原文

我真的很难为此努力,但我会尽力解释自己的观点。

说我有这个:

    List<IShape> Shapes = new List<IShape>();

    public interface IShape {
       dynamic shapeAttributes { get; set; }
    };

    public struct SquareAttributes {
      float sizeOfSide;
    };

    public struct CircleAttributes {
      float radius
    };
    
    public class Square : IShape {
      SquareAttributes shapeAttributes { get; set; }
    };

    public class Circle : IShape {
      CircleAttributes shapeAttributes { get; set; }
    };

    Shapes.Add(new Square());
    Shapes.Add(new Circle());
    

如何使这种情况起作用?在这里,在Square和Circle实现时,Ishape中的“动态”关键字无法解决,但是我仍然希望能够在实现时定义正确的类型,而不是到处使用“动态”。是否有正确的方法可以处理同一列表中的各种形状的能力?我希望这很清楚。

我显然简化了整个过程,以直接达到这一点,但是所涉及的所有内容都更加复杂,无法真正安装成一个大型件。

I really struggle entitling this, but i'll try my best at explaining my point.

Say i have this :

    List<IShape> Shapes = new List<IShape>();

    public interface IShape {
       dynamic shapeAttributes { get; set; }
    };

    public struct SquareAttributes {
      float sizeOfSide;
    };

    public struct CircleAttributes {
      float radius
    };
    
    public class Square : IShape {
      SquareAttributes shapeAttributes { get; set; }
    };

    public class Circle : IShape {
      CircleAttributes shapeAttributes { get; set; }
    };

    Shapes.Add(new Square());
    Shapes.Add(new Circle());
    

How Do I make that situation work ? Here the "dynamic" keyword in IShape is not resolved when implemented in Square and Circle, but I'd still want to be able to define the right Type when implementing rather than using "dynamic" everywhere. Is there a right way to deal with this, with the ability re regroup all kind of Shapes in the same list? I hope this is clear.

I obviously simplified the whole thing to get straight to the point, but everything involved is far more complex and cannot really be fitted into a single large piece.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌酱 2025-02-13 14:34:29

如果您的形状属性截然不同,则可以将system.Object用作公共类型。但是,不要忘记检查您是否通过正确的shapeAttributes键入以纠正ishape的实现,因此我建议使用设置方法代替属性设置器:

对象定义:


public interface IShape
{
    object ShapeAttributes { get;  }
    Type ShapeAttributesType { get; }
    void SetAttributes(object shapeAttributes);
}

public class Square : IShape
{
    public object ShapeAttributes { get; private set; }
    public Type ShapeAttributesType => typeof(SquareAttributes);     
    public void SetAttributes(object shapeAttributes)
    {
        // Check if passed correct type
        if (shapeAttributes.GetType() != ShapeAttributesType)
            throw new ArgumentException($"Argument type must be {ShapeAttributesType.FullName}", nameof(shapeAttributes));
        ShapeAttributes = shapeAttributes;
    }
}

public class Circle : IShape
{
    public object ShapeAttributes { get; private set; }
    public Type ShapeAttributesType => typeof(CircleAttributes);        
    public void SetAttributes(object shapeAttributes)
    {
        // Check if passed correct type
        if (shapeAttributes.GetType() != ShapeAttributesType)
            throw new ArgumentException($"Argument type must be {ShapeAttributesType.FullName}", nameof(shapeAttributes));
        ShapeAttributes = shapeAttributes;
    }
}

public struct SquareAttributes
{
    public float SizeOfSide { get; set; }
}

public struct CircleAttributes
{
    public float Radius { get; set; }
}

用法示例:


List<IShape> shapes = new List<IShape>();
var square = new Square();
square.SetAttributes(new SquareAttributes()
{
    SizeOfSide = 4.1f
});

var circle = new Circle();
circle.SetAttributes(new CircleAttributes()
{
    Radius = 2.12f
});

shapes.Add(square);
shapes.Add(circle);

foreach (var shape in shapes)
{
    //Cast ShapeAttributes based on owner class type
    switch (shape)
    {
        case Square s:
            var size = ((SquareAttributes)s.ShapeAttributes).SizeOfSide;
            Console.WriteLine($"Square.ShapeAttributes.SizeOfSide = {size}");
            break;
        case Circle c:
            var radius = ((CircleAttributes)c.ShapeAttributes).Radius;
            Console.WriteLine($"Circle.ShapeAttributes.Radius = {radius}");
            break;
    }
}

If your shapes attributes very different you can use System.Object as common type. But don't forget to check if you pass correct ShapeAttributes type to correct implementation of IShape, so I recommend to use set method instead of property setter:

Objects definition:


public interface IShape
{
    object ShapeAttributes { get;  }
    Type ShapeAttributesType { get; }
    void SetAttributes(object shapeAttributes);
}

public class Square : IShape
{
    public object ShapeAttributes { get; private set; }
    public Type ShapeAttributesType => typeof(SquareAttributes);     
    public void SetAttributes(object shapeAttributes)
    {
        // Check if passed correct type
        if (shapeAttributes.GetType() != ShapeAttributesType)
            throw new ArgumentException(
quot;Argument type must be {ShapeAttributesType.FullName}", nameof(shapeAttributes));
        ShapeAttributes = shapeAttributes;
    }
}

public class Circle : IShape
{
    public object ShapeAttributes { get; private set; }
    public Type ShapeAttributesType => typeof(CircleAttributes);        
    public void SetAttributes(object shapeAttributes)
    {
        // Check if passed correct type
        if (shapeAttributes.GetType() != ShapeAttributesType)
            throw new ArgumentException(
quot;Argument type must be {ShapeAttributesType.FullName}", nameof(shapeAttributes));
        ShapeAttributes = shapeAttributes;
    }
}

public struct SquareAttributes
{
    public float SizeOfSide { get; set; }
}

public struct CircleAttributes
{
    public float Radius { get; set; }
}

Usage example:


List<IShape> shapes = new List<IShape>();
var square = new Square();
square.SetAttributes(new SquareAttributes()
{
    SizeOfSide = 4.1f
});

var circle = new Circle();
circle.SetAttributes(new CircleAttributes()
{
    Radius = 2.12f
});

shapes.Add(square);
shapes.Add(circle);

foreach (var shape in shapes)
{
    //Cast ShapeAttributes based on owner class type
    switch (shape)
    {
        case Square s:
            var size = ((SquareAttributes)s.ShapeAttributes).SizeOfSide;
            Console.WriteLine(
quot;Square.ShapeAttributes.SizeOfSide = {size}");
            break;
        case Circle c:
            var radius = ((CircleAttributes)c.ShapeAttributes).Radius;
            Console.WriteLine(
quot;Circle.ShapeAttributes.Radius = {radius}");
            break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文