简体   繁体   English

您如何在运行时比较具有不同属性的对象?

[英]How do you compare objects with different properties in run-time?

在此处输入图像描述

My code structure is something like this我的代码结构是这样的

class Drawing()
{
ObjectType = "Drawing";
}

class Shape() : Drawing()
{
DrawingType = "Shape";
}

class square() : Shape()
{
ShapeType = "square";
}

class circle() : Shape()
{
ShapeType = "circle";
}

class triangle() : Shape()
{
ShapeType = "triangle";
}

class lines() : Drawing()
{
DrawingType = "lines";
}

class horizontal() : lines()
{
linesType = "horizontal";
}

class vertical() : lines()
{
linesType = "vertical";
}

etc...

I am trying to figure out the right logic in how to solve this problem so the picture is a simple representation of what I have.我试图找出解决这个问题的正确逻辑,所以这张图片是我所拥有的简单表示。

I have a object structure of something like in the picture where they're all inheriting the level above them.我有一个类似于图片中的 object 结构,它们都继承了它们之上的级别。 They all have properties of its type.它们都具有其类型的属性。 They would all have ObjectType, and the 2nd level has DrawingType, and the 3rd level has either ShapeType, LineType, or PointType...它们都会有 ObjectType,第 2 层有 DrawingType,第 3 层有 ShapeType、LineType 或 PointType……

So for example, square would have例如,正方形会有

square {
ObjectType = "drawing";
DrawingType = "shapes";
ShapeType = "square"
} 

However, vertical would have different properties但是,垂直将具有不同的属性

vertical {
    ObjectType = "drawing";
    DrawingType = "lines";
    LineType = "vertical"
    } 

Now my problem is, lets say I wanted a user to select something based on their input, how would i do it?现在我的问题是,假设我想要一个用户 select 基于他们的输入,我该怎么做? Like lets say the user typed "square", how would I select square?就像假设用户输入“正方形”一样,我将如何使用 select 正方形? It would be easy if the properties were the same cause I could just compare them.如果属性相同,那将很容易,因为我可以比较它们。 But how would I do it with a structure like this?但是我该如何使用这样的结构呢?

As these all classes are derived from one base class hence all the child classes contains the property coming from parents.由于这些所有类都是从一个基础 class 派生的,因此所有子类都包含来自父类的属性。 Object level comparison will lead you comparison of one Drawing object to another as all the objects are derived from there only. Object 级别比较将引导您将一张图纸 object 与另一张图纸进行比较,因为所有对象都仅从那里派生。 Hence i have implemented IComparer to get your desired result ->因此,我实施了 IComparer 以获得您想要的结果->

Here is the code ->>这是代码->>

Main Class :主要 Class

Drawing drawing1 = new Drawing();
        Drawing drawing2 = new Drawing();
        drawing2.ObjectType = "newdrawing";

        Shape shape = new Shape();
        triangle triangle1 = new triangle();
        triangle triangle2 = new triangle();
        triangle2.ShapeType = "another";

        //all -1 is not equals and 0 is equals
        int result1 = drawing1.Compare(drawing1, drawing2);
        int result2 = drawing1.Compare(drawing1, drawing1);

        int result3 = drawing1.Compare(drawing1, shape);
        int result4 = shape.Compare(shape, triangle1);

        int result5 = triangle1.Compare(triangle1, triangle2);

Other classes :其他类

public class Drawing : IComparer
{
    public string ObjectType { get; set; } = "Drawing";

    public int Compare(object x, object y)
    {
        //returning 0 is equals
        if (x.GetType().Name == y.GetType().Name)
        {
            switch (x.GetType().Name)
            {
                case "Drawing":
                    return ((Drawing)x).ObjectType == ((Drawing)y).ObjectType ? 0 : -1;
                case "Shape":
                    return ((Shape)x).DrawingType == ((Shape)y).DrawingType ? 0 : -1;
                case "square":
                    return ((square)x).ShapeType == ((square)y).ShapeType ? 0 : -1;
                case "triangle":
                    return ((triangle)x).ShapeType == ((triangle)y).ShapeType ? 0 : -1;
                default:
                    return -1; // Not equal
            }
        }
        return -1; // Not equal
    }
}

class Shape : Drawing
{
    public string DrawingType { get; set; } = "Shape";

}

class square : Shape
{
    public string ShapeType { get; set; } = "square";
}

class circle : Shape
{
    public string ShapeType { get; set; } = "circle";
}

class triangle : Shape
{
    public string ShapeType { get; set; } = "triangle";
}

class lines : Drawing
{
    public string DrawingType { get; set; } = "lines";
}

class horizontal : lines
{
    public string linesType { get; set; } = "horizontal";
}

class vertical : lines
{
    public string linesType { get; set; } = "vertical";
}

Hope this will work for you.希望这对你有用。 please write back with you code snippet specifically not working.请写回您的代码片段,具体不起作用。 Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问在运行时定义了类型的对象? - How do you access an object whose type is defined at run-time? 您如何确保MVC实体在运行时与数据库同步? - How do you ensure that the MVC entities are in-sync with the database at run-time? NET中如何在运行时向对象添加属性? - How to add properties to the object at run-time in .net? 如何在运行时更改“ AnimationClock-timeline”属性? - How to change `AnimationClock-timeline` properties at run-time? 如何比较两个对象的属性? - How do I compare two objects' properties? 在运行时隐藏 PropertyGrid 中的一些属性 - Hide some properties in PropertyGrid at run-time 如何配置 Kestrel 以使用随机动态端口并在运行时使用 ASP.NET Core 3.1 确定端口? - How do you configure Kestrel to use a random dynamic port and determine the port at run-time with ASP.NET Core 3.1? 如何在运行时向XslCompiledTransform添加任意名称空间? - How do I add an arbitrary namespace to an XslCompiledTransform at run-time? 多个数据库,其模型稍有变化。 如何在运行时允许EF与不同的数据库结构一起使用? - Multiple databases with slightly changing models. How do I allow `EF` to work with different database structures at run-time? 如何比较具有相似属性的两个截然不同的对象 - How to compare two distinctly different objects with similar properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM