2

Hi everyone I am not sure if what I want to do is even remotely possible but I am going to do my best to explain it and would really appreciate any suggestions / ideas,

Imagine I have :-

    public class Attribute{
        object Value;
        **(X1)**IComparer<T> comparer
    }
    public class AttributeValues
    {
        List<Attribute> values;
        SortedList<Attribute> Sort(){
            this uses the comparer defined in **(X1)** to sort the values in the list
        }
    }

These classes allow the user to create user defined attributes and select a pre-defined sort algorithm to sort the values In the simplistic case, I can do AttributesValues.Sort() using one of the comparers that I have created and compiled in source code which has been chosen by the user. This is fine when the comparison is known in advance such as a simple ascending alphabetical sort for example.

However, there are some circumstances where more logic is needed and it is not known in advance. For example, the string "4DFG5ET" might codify a date that needs to be sorted and there may be some other attribute with similar logic and so on.

If possible I don't want to keep writing the IComparer implementations and would love for it to be possible that somehow I could define the IComparer implementation in a text box or file at runtime and it would be somehow compiled in the program or persisted then available in the selction of comparisons that could be used to sort an attribute.

Does anyone have any suggestions on how to approach this?

Please ask if you would like me to clarify something as I know it is a slightly obscure question.

Many thanks in advance

Alex

lostinwpf
  • 633
  • 2
  • 9
  • 29
  • It's possible through code generation. Google `CodeDom` for example. – user703016 Jun 12 '12 at 20:24
  • `4DFG5ET` codifies a date that needs sorting? There is a lot of magic embedded in that number. [There was another question on SO about dynamic sort criteria](http://stackoverflow.com/questions/950726/dynamic-sort-criteria-for-generic-list) - would that accomplish what you're looking to do? – 48klocs Jun 12 '12 at 20:38
  • I just made that up but I have seen similar things - I was just trying to convey that the logic to perform the sort isn't general. – lostinwpf Jun 12 '12 at 20:45

4 Answers4

0

Sure, there are several different technologies that support that. Reflection.Emit comes to mind, and since the .NET framework includese a compiler you should also be able to emit C# and compile that on the fly to an assembly that you then load. There are more options in the new dynamic languages space, I believe.

0

You can make use of CSharpCodeProvider (see documentation here) to compile the provided text into a DLL and load the resulting class using reflection. Also, this SO question might be of good help for you

Community
  • 1
  • 1
GETah
  • 20,922
  • 7
  • 61
  • 103
0

I think you can use MEF for that:

quote: "The Managed Extensibility Framework (MEF) is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications. MEF can be used for third-party plugin extensibility, or it can bring the benefits of a loosely-coupled plugin-like architecture to regular applications."

eyossi
  • 4,230
  • 22
  • 20
0

As I see it, you have two options. First, you can construct your own language which can parse simple user commands and then execute the code in JIT fashion.

The second option is to actually let your users write C# code and the compile it on the fly. (As previously mentioned, Reflection.Emit should take care of most simple needs.)

EDIT A third option isn't exactly what you wanted, but you could setup a directory for comparer plugins. When your application starts, it can scan that directory and load the assemblies. Using Reflection, you can extract any classes that implement the IComparer interface, then provide them as a list to the user. (You could also use custom attributes to define friendly user names and descriptions, etc).

In this case, you would not need to recompile your entire application just to quickly add some new comparer. Also, if you have a superuser, they could use some free .NET code editor to quickly write up their own comparer and "install" them to the directory.

JDB
  • 25,172
  • 5
  • 72
  • 123
  • Sorry about the late reply got sidetracked onto something else. With regards to 2nd option is it possible to type in source directly into a text box in C# format and then compile that using Reflection.Emit or is that more for CSharpCodeProvider? Thanks – lostinwpf Jul 09 '12 at 14:09