Hello,
In the sample below is an example class with a set of four properties that I called SiO2, Al2O3, Fe2O3, CaCO3.
I would like to create these four properties at run time (or more similar properties).
The names of these properties would be determined by the end-user.
I would like these dynamic properties to be compatible withreflection.
And I would like them to be compatible with Linq or System.Linq.Dynamic .
I found out this is easy by using the System.CodeDom.Compiler at run time.
However, this takes a little bit too much execution time and this creates several files in a temp folder.
For these reasons, I would be interested by other solutions.
I investigated several, but was unable to get a running program.
I considered using System.Reflection.Emit but could not digest the use of the ILGenerator which looks too complicated to me.
I dreamt that using the ILGenerator could be avoided by using expressions compiling in some way, but could not complete the idea.
Would you have some suggestion, reading or even some code?
Thanks,
Michel
// test program
public class example { public double[] analysis; public double SiO2 { get { return analysis[0]; } } public double Al2O3 { get { return analysis[1]; } } public double Fe2O3 { get { return analysis[2]; } } public double CaCO3 { get { return analysis[3]; } } public example(double[] pAnalysis) { analysis = pAnalysis; } } class Program { static void Main(string[] args) { example xmpl = new example(new double[] {15,20,25,30}); PropertyInfo[] pis = xmpl.GetType().GetProperties(); foreach (PropertyInfo pi in pis) Console.WriteLine("{0}{1}{2}", pi.ToString(), " value = ", pi.GetValue(xmpl, null)); List<example> xmpls = new List<example>(); xmpls.Add(new example(new double[] {15,20,25,30})); xmpls.Add(new example(new double[] {20,25,30,35})); xmpls.Add(new example(new double[] {25,30,35,40})); xmpls.Select(x => "Al2O3 = "+x.Al2O3).ToList().ForEach(Console.WriteLine); var sumAl2O3 = xmpls.Sum(x => x.Al2O3); Console.WriteLine("{0}{1}", "Total = ", sumAl2O3); return; } }