Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IList<IMyDataInterface> list = query.ToList() as IList<IMyDataInterface>;
- class Program
- {
- static void Main(string[] args)
- {
- IMyFunctionalInterface myObject = new MyClass();
- //myObject.Get() returns null for some reason...
- IList<IMyDataInterface> list = myObject.Get();
- Debug.Assert(list != null, "Cast List is null");
- }
- }
- public interface IMyFunctionalInterface
- {
- IList<IMyDataInterface> Get();
- }
- public class MyClass : IMyFunctionalInterface
- {
- public IList<IMyDataInterface> Get()
- {
- string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
- var query = from n in names
- where n.Contains("a")
- select new MyDataClass
- {
- Name = n.ToString()
- };
- //There IS data in the query result
- Debug.Assert(query != null, "result is null");
- //but the cast here makes it return null
- IList<IMyDataInterface> list = query.ToList() as IList<IMyDataInterface>;
- return list;
- }
- }
- public interface IMyDataInterface
- {
- string Name { get; set; }
- }
- public class MyDataClass : IMyDataInterface
- {
- public string Name { get; set; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var names = new[] { "Tom", "Dick", "Harry", "Mary", "Jay" };
- var query = from n in names
- select new C
- {
- S = n
- };
- //There IS data in the query result
- Debug.Assert(query != null, "result is null");
- //but the conversion here makes it return null
- var list = query.ToList() as IList<I>;
- Console.WriteLine(query.ToList().GetType());
- // this assert fires.
- Debug.Assert(list != null, "Cast List is null");
- }
- }
- interface I
- {
- string S { get; set; }
- }
- class C : I
- {
- public string S { get; set; }
- }
- var list = (IList<I>)query.ToList();
- var query = from n in names
- where n.Contains("a")
- select new MyDataClass
- {
- Name = n.ToString()
- } as IMyDataInterface;
- IList<IMyDataInterface> list = query.ToList() as IList<IMyDataInterface>;
- List<MyDataClass> tmp = query.ToList();
- IList<IMyDataInterface> list = tmp as IList<IMyDataInterface>;
- IList<IMyDataInterface> list = query.ToList().Cast<IMyDataInterface>();
Add Comment
Please, Sign In to add comment