문제

EDIT: It seems to be confirmed that covariant generics are not yet supported. To make sure it makes it in the next release please email Xamarin and request this feature.

The following code gives me an error (at the linq statement). Identical code works fine in Microsoft clr c# 4.0.

Error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<LinqTest.Person>' to 'System.Collections.Generic.IEnumerable<LinqTest.Entity>'. An explicit conversion exists (are you missing a cast?)

Code:

[Activity(Label = "LinqTest", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity {

    protected override void OnCreate(Bundle bundle) {
        base.OnCreate(bundle);

        var names = new List<string> {
                                         "Joe",
                                         "Bob",
                                         "Jim",
                                         "Jane"
                                     };

        IEnumerable<Entity> query =
            from e in names
            select new Person() {Name = e};

        SetContentView(Resource.Layout.Main);

        var button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += delegate {
                            button.Text = string.Format("{0}", query.Count());
                        };
    }
}

public class Entity {
    public string Name { get; set; }
}

public class Person : Entity {
    public string Workplace { get; set; }
}

public class Animal : Entity {
    public string FurColour { get; set; }
}

Am I correct or am I missing something? If so recommendations on refactoring this?

Also, if so, consider this an official feature request for monodroid - support for covariant generic support. :)

Thanks.

도움이 되었습니까?

해결책

Neither MonoTouch nor Mono for Android has support for covariance right now. It will be supported in a future release (probably some time in the second half of this year).

다른 팁

Didn't you forget to set the framework version to 4.0? Works for me with Mono 2.10.8. Although it may also be some android specific limitation (partial generic support listed here, however not explicitly stated).

Not having covariant support for generics was resulting in ugly code so I went back to this problem and came up with a simple hack to allow a simulated covariant effect.

I would like any feedback on making this even simpler. Also I am concerned about performance - any thoughts. Its still not perfect but allows me to clean up other code.

Here is the new code sample that simulates covariant generics on monodroid.

    [Activity(Label = "CovariantTest", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity {
    int count = 1;

    protected override void OnCreate(Bundle bundle) {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);

        Button button = FindViewById<Button>(Resource.Id.MyButton);

        var names = new List<string> {
                                         "Joe",
                                         "Bob",
                                         "Jim",
                                         "Jane"
                                     };

        IEnumerable<Entity>query =
            (from e in names
            select (new Person() { Name = e }) as Entity).ToList();

        Person p =  query[2] as Person;

        button.Click += delegate { button.Text = string.Format("{0}", p.Test()); };
    }
}

public class Entity {
    public string Name { get; set; }
}

public class Person : Entity {
    public string Workplace { get; set; }
    public string Test() {
        return "I am a person";
    }
}

public class Animal : Entity {
    public string FurColour { get; set; }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top