Assume I have a table called User. Using LINQ desinger, I will end up with the following:
- A file called User.dbml
- A data context class called UserDataContext which subclasses from System.Data.Linq.DataContext
- A class called User which is mapped from the User table. A UserDataContext object will have a property called Users which is of type System.Data.Linq.Table<User>.
So far so good. Now I want to define a generic base class that converts the Linq.Table property to JSON string for all of its subclasses. So I would have:
using Newtonsoft.Json;
class BasePlugin<T> where T : System.Data.Linq.DataContext, new()
{
protected T DataContext = new T();
protected string GetJSONData()
{
//*** DataContext if of type System.Data.Linq.DataContext, therefore it won't know the Linq Table property of its subclasses
return JsonConvert.SerializeObject(DataContext.Cannot_get_subclass_property_Linq_table);
}
}
To complete the code in the question, here's an example of a subclass:
class UserPlugin : BasePlugin<UserDataContext>
{
//The protected member DataContext inherited from BasePlugin
//has a property called Users of type System.Data.Linq.Table<User>.
//The point to to avoid implementing GetJSONData() in all subclasses
}
To summarize, the question is how to avoid implementing GetJSONData() in all subclasses by letting the base class do it.