I'm trying to write a generic utility for use via COM from outside .NET (/skip long story). Anyway, I'm trying to add properties to an ExpandoObject and I need to get PropertyInfo structure back to pass to another routine.
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Reflection;
public class ExpandoTest
{
public string testThis(string cVariable)
{
string cOut = "";
ExpandoObject oRec = new ExpandoObject { };
IDictionary<string, object> oDict = (IDictionary<string, object>)oRec;
oDict.Add(cVariable, "Test");
Trace.WriteLine(cVariable);
Trace.WriteLine(oDict[cVariable]);
PropertyInfo thisProp = oRec.GetType().GetProperty(cVariable);
if (thisProp != null)
{
cOut= "Got a property :)";
}
return cOut;
}
}
Why do I always get a null in in thisProp? I clearly don't understand but I've been staring at it for a day and I'm not getting anywhere. All help/criticism thankfully accepted!
ExpandoObject
class itself. You won't get aPropertyInfo
for a property you just made up (it doesn't exist, even ifdynamic
makes you think it does). Nothing's magical here. You could get aPropertyDescriptor
though. – Cacophony.GetValue(oRec, null)
to the end (for example). @Anneliese I'm simply passing string parameters in through the COM interface (well, strings containing XML but still strings). It's being used to implement a user configurable text file importer (csv, tab delimited, etc.) but all the complexity is contained within .NET code. It's just not well defined data. I'm needing to build up a dynamic mapping for use with CsvHelper and I need to pass it PropertyInfo structures (using the example that I have) – Beyond