I have a custom control. This custom control uses Reflection heavily. Inside the custom control assembly I have a class, which is a reflection manager. It handels everything reflection related in the control. One of the things it does is cache the PropertyInfo objects for each property of the objects in the DataSource. I would like to make it static so I can easily use it inside the custom control class and also other support classes inside the assembly. The problem is that this control is used in several places, and with different datasources, and each has it’s own collection of PropertyInfo objects.
What would be the best way to create this class so that it has an instance per instance of the control, and is also available to other classes within the custom control assembly.
One thing I thought of is to insert the instance of the reflection manager I created in the custom control class in to a property / though the constructor of the supporting classes (ala DI), but maybe there is a better pattern I’m just not aware of.
Edit: Sorry for being unclear. The project is an ASP.NET WebForms project. The control is a CompositeDataBoundControl if that matters.
Edit 2: I will elaborate on the design I have and what flaws I see in it.
I have a custom control class which is being used (or planned to be used in several pages).
Inside this control I have this class (sorry if I have some syntax mistakes, I`m writing this from memory). This is not the entire class, just what is relevant here.
public static class DataReflectionManager
{
private static Dictionary<string, PropertyInfo> _propertyInfos = new Dictionary<string, PropertyInfo>();
public static void RegisterDataSource(IEnumerable dataSource)
{
//Get the property info of each property of and
//stick it in the dictionary.
}
public static string GetValueByPropertyKey(object o, string key)
{
//Takes o and gets the value of property key by the PropertyInfo object.
return "";
}
}
Now this class cannot remain static, or the dictionary will be the same for all instances of the custom controls, and they each have their own datasource, which holds different types of objects. So… static is out. But what then?
If I used DataReflectionManager in just the custom control class, I would just create an instance there. The problem is that the custom control uses column types, which all inherit from ColumnBase and ColumnBase has an abstract function, which uses the DataReflection manager in most of the inheriting column types. So I could pass the instance around, but I’m looking for maybe a more intelligent solution.
I could not find a “perfect” solution here, since every way of doing this I could come up with has some drawbacks for our specific design.
The thinking that helped me arrive at my current solution was that the list of
ProperyInfoobjects is tied to a specific instance of the control, and thus theDictionary<string, PropertyInfo>used to store it should also be a member of the control class. This allowed me to keep the reflection manager static and make minimal code changes in supporting classes.Supporting classes get
PropertyInfoobjects as needed in their respective constructors.The reflection manager created the dictionary through reflection in the control constructor and from there on, the control stores it.