I’m developing a Desktop Applicaiton which is akin to a plugin based system. I have a client module, that will load a DLL containing a ‘Machine’ object. Thereafter the ‘Machine’ object is manipulated and used as per well defined interfaces.
The tricky part is that the DLL containing the ‘Machine’ object is generated on the fly by using another program. At it’s core, the DLL generating application accepts user input, generates classes, in c# code files (which contain fields specified by the user, of which I don’t have any prior knowledge of) and compiles those classes to a DLL (machine.dll file).
The client program picks up this dll, dynamically loads it and then operates on this machine bject.
I am facing a lot of trouble modeling a solution to address the problem of passing data between the Machine object and the Client program, essentially because I dont know what data is contained in the machine object.
The client program is to do the following things.
— Load the dll and instantiate the ‘machine’ object.
— Call a series of functions in the ‘machine’ object, that are known to the client via an interface.
— Extract various variables from the ‘machine’ object and display it to the user.
I am not able to perform the last step.
Note: I have programmed a trivial solution where the meta-data about the fields is generated by the dll generating program and stored in xml files. The client program uses these xml files to get information about the fields stored in the machine object. It then uses reflection on the machine object to access the fields of the object.
I feel this is cumbersome and slow. Are there any patterns or methods to this kind of stuff??
A solution that came to mind when I read this was to make use of the built-in support for Attributes in C#. An attribute is a way of tagging a property, field, method, class, etc with some additional meta-data that is then used by some other class, for example during Serialization. You will see it there most often.
I had an application that I was building that needed to be able to take an
IEnumerablecollection of objects and output some data to a file based on user selected choices. I created an attribute class that gave me the ability to read the choices via reflection, and act as directed. Let me show you the example:First the attribute class:
With this class defined like so, I could decorate my data class properties like this (actual properties changed so as to not get lost on the business jargon):
So then in my export routine, instead of hard-coding the property names, which are variable, or passing a complex data structure around which contained the information on which fields to show or hide and what the ordering was, I just ran the following code, using reflection, to loop the properties and output their values, to a CSV file in this case.
The code for the OrderedProperties struct is here:
As you can see, the logic to extract the property values is completely ignorant of the structure of the class. All it does is find the properties that are decorated with the attribute I created, and use that to drive the processing.
I hope this all makes sense, and if you need more help or clarification, please feel free to ask.