我需要使用字符串propertyName设置DynamicObject属性。我找到了使用此answer获取属性值的方法,但是当涉及到setValue时,我不太确定如何重写代码以设置属性。我收到运行时错误,不太确定表达式逻辑。我想知道你是否可以建议如何实现void SetProperty(object o, string member,object value)
方法。
答案 0 :(得分:3)
在某些情况下,例如ExpandoObject
,您可以使用IDictionary<string,object>
API代替:
ExpandoObject obj = ...
var dict = (IDictionary<string, object>)obj;
object oldVal = dict[memberName];
dict[memberName] = newVal;
在IDynamicMetaObjectProvider
的更一般情况下:您可以从FastMember借用CallSiteCache
:
internal static class CallSiteCache
{
private static readonly Hashtable getters = new Hashtable(), setters = new Hashtable();
internal static object GetValue(string name, object target)
{
CallSite<Func<CallSite, object, object>> callSite = (CallSite<Func<CallSite, object, object>>)getters[name];
if (callSite == null)
{
lock (getters)
{
callSite = (CallSite<Func<CallSite, object, object>>)getters[name];
if (callSite == null)
{
CallSite<Func<CallSite, object, object>> newSite = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, name, typeof(CallSiteCache), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
getters[name] = callSite = newSite;
}
}
}
return callSite.Target(callSite, target);
}
internal static void SetValue(string name, object target, object value)
{
CallSite<Func<CallSite, object, object, object>> callSite = (CallSite<Func<CallSite, object, object, object>>)setters[name];
if (callSite == null)
{
lock (setters)
{
callSite = (CallSite<Func<CallSite, object, object, object>>)setters[name];
if (callSite == null)
{
CallSite<Func<CallSite, object, object, object>> newSite = CallSite<Func<CallSite, object, object, object>>.Create(Binder.SetMember(CSharpBinderFlags.None, name, typeof(CallSiteCache), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null) }));
setters[name] = callSite = newSite;
}
}
}
callSite.Target(callSite, target, value);
}
}
请注意,我们 可以<{1}}输入target
,但我们实际上并不需要 - IDynamicMetaObjectProvider
API不需要它。