0

I have some different objects that all of them have a integer field called Place. Is there a way to sort out this list without knowing what is the actual object? I mean just accessing the Place field and sort the list based on this number. possibly using linq or something?

some example objects:

public class Car
{
    public int Place;
    //Other related fields
}

public class Human
{
    public int Place;
    //Other related fields
}

//Somwhere in program
List<object> GameObjects;

What you just discovered is relationship between those types. Both Car and Human seem to have a Place property, so you should extract an interface à la IGameObject.

4

7 に答える 7

5

You should derive your classes from a base class.

public class Base
{
    public int Place;
}

public class Car : Base
{
    // other properties
}

public class Human : Base
{
    // other properties
}

Then you can create a list of your base type, add humans and cars. After that you can use the Linq Sort or OrderBy method.

List<Base> list = new List<Base>();
list.Add(new Human { Place = 2 });
list.Add(new Car { Place = 1 });

var sortedList = list.Sort(x => x.Place);

More Information

于 2012-08-28T10:44:59.597 に答える
4

No because object doesn't have a Place property only Car/Human do.

There are a couple of ways you can solve this problem:

Introduce a base class

public class GameObject
{
    public int Place { get; set; }
}

public class Car : GameObject
{}

public class Human : GameObject
{}

...
List<GameObject> GameObjects

Use a generic interface

public interface IGameObject
{
    int Place { get; }
}

public class Car : IGameObject
{
    public int Place { get; set; }
}

public class Human : IGameObject
{
    public int Place { get; set; }
}

List<IGameObject> GameObjects
于 2012-08-28T10:50:41.867 に答える
1

最良の方法は、インターフェースを使用することです。できない場合でも、dynamicキーワードを使用して遅延バインディングを実行できます。

        var list = new List<object>
        {
            new Car { Place = 3 },
            new Human { Place = 1 },
            new Car { Place = 2 }
        };

        var sortedList = list.OrderBy(o => ((dynamic)o).Place);
于 2012-08-28T10:47:54.127 に答える
1

あなたが今発見したのは、それらのタイプ間の関係です。と の両方CarHumanPlace プロパティがあるように見えるので、インターフェイスを抽出する必要がありますIGameObject

于 2012-08-28T10:44:51.787 に答える
0

はい、リフレクション付きのデリゲートメソッドを使用して可能です。これは私の知る限りですが、他の巨人が反射を使用せずに作成する可能性があります

于 2012-08-28T10:45:17.140 に答える
0

あなたは彼らにインターフェースIPlaceableを実装させ、フィールドだけでなくプロパティを使用させることができます:

public interface IPlaceable
{
    int Place { get; set; }
}

public class Car : IPlaceable
{
    public int Place { get; set; }
    //Other related fields
}

public class Human : IPlaceable
{
    public int Place { get; set; }
    //Other related fields
}


// Somwhere in program
List<IPlaceable> GameObjects;

// Somwhere else
GameObjects.OrderBy(go => go.Place);

List<IPlaceable>リストがの代わりになっていることに注意してくださいList<Object>

于 2012-08-28T10:48:20.227 に答える
0

The best you can do is use an Interface, like this:

public Interface IFoo
{
  int place;
}

And the implement that interface:

public class Car : IFoo
{
    public int Place;
}

public class Human : IFoo
{
    public int Place;
}

And then with linq:

List<IFoo> GameObjects;

GameObjects.OrderBy(g => g.Place);
于 2012-08-28T10:48:51.453 に答える