我正在尝试编写一个通用实用程序,以从.NET外部通过COM使用(/跳过长篇故事)。无论如何,我试图将属性添加到ExpandoObject,并且我需要获取PropertyInfo结构以传递给另一个例程。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我总是在thisProp中获得null?我显然不明白,但我一直盯着它看了一天,却一无所获。感谢所有的帮助/批评!