Custom generic class doesn't benefit from the autocomplete when not defined in the current scope
When you refer to a generic class defined outside of the current scope, the autocomplete feature will not work.
The problem can be reproduced under :
v4.55.03 Premium edition
v4.56.06 Premium edition
Download the linq
The problem can be reproduced under :
v4.55.03 Premium edition
v4.56.06 Premium edition
void Main()
{
//check CustomGenericClassTest and GenericClassTest
//mouseover the entitylist's member or type '.' after the entitylist[n]
}
void CustomGenericClassTest()
{
var entitylist1 = new EntityList<Employee>();
var entitylist2 = new CompanyContext().Employees;
var entitylist3 = EntityLocator.Resolve<Employee>();
//entitylist1 has full autocomplete support
entitylist1.Update(null);
//entitylist1/3 doesnt have autocomplete support beside Dump() and DumpTrace() methods
entitylist2.Insert(null);
entitylist3.GetTable().Dump();
//explicitly casting it, will fix the problem. however it is rather inconvenient
(entitylist2 as EntityList<Employee>).Update(null);
}
void GenericClassTest()
{
var list1 = new List<Employee>();
var list2 = new CompanyContext().Employees2;
var list3 = EntityLocator.Resolve2<Employee>();
//autocomplete fully supported with List<T>
list1.Count.Dump();
list2.Count.Dump();
list3.Count.Dump();
}
// Define other methods and classes here
class EntityList<T> where T : new()
{
public void Update(T item) { }
public void Insert(T item) { }
public List<T> GetTable() { return Enumerable.Repeat(new T(), 10); }
}
class Employee
{
public int EmployeeID { get; set; }
public int FirstName { get; set; }
public int LastName { get; set; }
}
class CompanyContext
{
public EntityList<Employee> Employees { get; set; }
public List<Employee> Employees2 { get; set; }
public CompanyContext()
{
Employees = new EntityList<Employee>();
Employees2 = new List<Employee();
}
}
static class EntityLocator
{
public static EntityList<T> Resolve<T>() where T : new()
{
return new EntityList<T>();
}
public static List<T> Resolve2<T>() where T : new()
{
return new List<T>();
}
}
Download the linq
Comments
-
The issue is with autocomplete figuring out the type with var, and defaulting to object. As a work around, you could avoid using var. I ran into an issue with var, and autocomplete as well that might be related:
http://forum.linqpad.net/discussion/752/issue-with-auto-complete-when-you-use-a-var-a-custom-class-and-a-context#latest -
The
varis only used here for illustrate the problem. Actually, I want to avoid using intermediary variable in some case. As they don't add any value or readability to the code, other than 1 extra line of code for the sake of using autocomplete.
Here is a piece of code I'm worked on recently :
pastebin.com/5E5tH5N3
see line 9~15
-
Yes
((EntityList<Employee>)EntityLocator.Resolve<Employee>())works when you autocomplete, butEntityLocator.Resolve<Employee>()fails.
What I find odd is what is still showing in autocomplete,Dump(), andDumpTrace(). It's missing the menu items for object, such asToString(), but it still has those two, even though they extend object. Also, I found that if you put the classes in MyExtensions, and make them public, then try to access them from a new query autocomplete works.
