I am looking for a way to display data in a DataGrid
from types that are unknown at compile-time.
I have the following base class
public abstract class Entity
{
// Some implementation of methods ...
}
In run-time, I load a plug-in DLL and use reflection to get a list of all the types derived from Entity
. For example:
public class A : Entity
{
public LocalAddress Address{ get; set; }
}
public class B : Entity
{
public Vendor Vendor { get; set; }
public string Name { get; set; }
}
Then I retreive a list of their instances from DB
public IEnumerable<Entity> Entities { get; set; } // A list of instances of type A for example
Entities
is the DataGrid's ItemsSource
, But what's the best way I can bind the properties to the DataGrid
?
Since the properties can be complex, I also need to be able to bind to a specific path, for example Address.HomeNum
...
Clarifications
I only need to show a one grid of a type's instances at a time. The complete scenario is this:
- I get a list of types that derive from
Entity
from the plug-in DLL through reflection - I show their names in a List. (in this example that list will contain
A
andB
- When the user clicks on a specific item, let's say
A
, I get a list ofA
instances from DB - so far so good. - I want to display that list of
A
's instances in aDataGrid
. - When the user selects another item from the list (meaning another type, lets say
B
), I get a list ofB
's instances from DB and need to display those in the grid and so on ...
- I get a list of types that derive from
The plug-in DLL is a class library with no xamls (also my users are the ones making this plug-ins and I don't want them to have to write
DataTemplate
s for their entities. I also can't make predifnedDataTemplate
s as I don't know the types I'll need to display until run-time. Each type can have different types and amount of properties. All I know in complie-time is that they all derived fromEntity
.- The grid should also be editable.