Reputation: 6510
The below code compiles with .Net Framework 4.0 but not with Silverlight 4.0. I would appreciate if anyone could shed some light on this. Here're the error messages I'm getting:
Error 1 The best overloaded method match for 'ThinkFarAhead.Confounded.Client.Models.Consumer.SetFunctionalAreas(System.Collections.Generic.IEnumerable<ThinkFarAhead.Confounded.Client.Models.IFunctionalArea>)' has some invalid arguments c:\Hanu\SilverlightApplication1\Test.cs 64 13 SilverlightApplication1
Error 3 The best overloaded method match for 'ThinkFarAhead.Confounded.Client.Models.Consumer.SetFunctionalAreas(System.Collections.Generic.IEnumerable<ThinkFarAhead.Confounded.Client.Models.IFunctionalArea>)' has some invalid arguments c:\Hanu\SilverlightApplication1\Test.cs 65 13 SilverlightApplication1
Error 2 Argument 1: cannot convert from 'ThinkFarAhead.Confounded.Web.EntitySet<ThinkFarAhead.Confounded.Web.FunctionalArea>' to 'System.Collections.Generic.IEnumerable<ThinkFarAhead.Confounded.Client.Models.IFunctionalArea>' c:\Hanu\SilverlightApplication1\Test.cs 64 47 SilverlightApplication1
Error 4 Argument 1: cannot convert from 'ThinkFarAhead.Confounded.Web.EntitySet<ThinkFarAhead.Confounded.Web.FunctionalArea>' to 'System.Collections.Generic.IEnumerable<ThinkFarAhead.Confounded.Client.Models.IFunctionalArea>' c:\Hanu\SilverlightApplication1\Test.cs 65 47 SilverlightApplication1
What I'm trying to do: Extend generated entities (RIA) on Silverlight (4.0) side to make multiple entities with common features share the same interface (A control needs to use multiple objects the same way. These objects are pretty much the same).
Thanks in advance.
using System;
using System.Collections;
using System.Collections.Generic;
using ThinkFarAhead.Confounded.Client.Models;
using ThinkFarAhead.Confounded.Web;
namespace ThinkFarAhead.Confounded.Web
{
public class Entity { }
public class EntitySet<T> : IEnumerable<T>, IEnumerable where T : Entity
{
List<T> list = new List<T>();
public IEnumerator<T> GetEnumerator()
{
return (IEnumerator<T>)list;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T newone)
{
list.Add(newone);
}
}
public partial class FunctionalArea : Entity
{
public string Name { get; set; }
}
public partial class FunctionalArea : IFunctionalArea { }
}
namespace ThinkFarAhead.Confounded.Client.Models
{
public interface IFunctionalArea
{
string Name { get; set; }
}
public class Variance
{
public static EntitySet<FunctionalArea> FunctionalAreas
{
get
{
return new EntitySet<FunctionalArea>();
}
}
public static void Main()
{
var abc = new EntitySet<FunctionalArea>();
new Consumer().SetFunctionalAreas(abc);
new Consumer().SetFunctionalAreas(FunctionalAreas);
}
}
public class Consumer
{
public void SetFunctionalAreas(IEnumerable<IFunctionalArea> areas)
{
}
}
}
Upvotes: 0
Views: 336
Reputation: 83
As Austin said in SL4 you can't just pass your collection with type IEnumerable<FunctionalArea>
as parameter when your method expects a IEnumerable<IFunctionalArea>
.
But if you don't want to go 'dynamic', don't forget you can still do this (using System.Linq) :
var abc = new EntitySet<FunctionalArea>();
new Consumer().SetFunctionalAreas(abc.Cast<IFunctionalArea>());
new Consumer().SetFunctionalAreas(FunctionalAreas.Cast<IFunctionalArea>());
Not quite elegant, but hey, that does the trick ;)
Upvotes: 1
Reputation: 3116
The BCLs in Silverlight don't have covariant/contravariant markers (IEnumerable<T>
is not marked as IEnumerable<out T>
as in .NET Framework, for example).
User code can use these features, it's just framework code that doesn't - a limitation of the platform today.
Upvotes: 1