这不是真正的问题标题,因为我真的不知道问题在哪里,所以我只是在猜测。
故事开始尝试向我的WCF服务发送和接收dynamic
类型,这导致我WCF Serialization of DLR dynamic types!
SerializableDynamicObject 类
public class SerializableDynamicObject : IDynamicMetaObjectProvider
{
private IDictionary<string,object> dynamicProperties = new Dictionary<string,object>();
#region IDynamicMetaObjectProvider implementation
public DynamicMetaObject GetMetaObject (Expression expression)
{
return new SerializableDynamicMetaObject(expression,
BindingRestrictions.GetInstanceRestriction(expression, this), this);
}
#endregion
#region Helper methods for dynamic meta object support
internal object setValue(string name, object value)
{
dynamicProperties.Add(name, value);
return value;
}
internal object getValue(string name)
{
object value;
if(!dynamicProperties.TryGetValue(name, out value)) {
value = null;
}
return value;
}
internal IEnumerable<string> getDynamicMemberNames()
{
return dynamicProperties.Keys;
}
#endregion
}
SerializableDynamicMetaObject 类
public class SerializableDynamicMetaObject : DynamicMetaObject
{
Type objType;
public SerializableDynamicMetaObject(Expression expression, BindingRestrictions restrictions, object value)
: base(expression, restrictions, value)
{
objType = value.GetType();
}
public override DynamicMetaObject BindGetMember (GetMemberBinder binder)
{
var self = this.Expression;
var dynObj = (SerializableDynamicObject)this.Value;
var keyExpr = Expression.Constant(binder.Name);
var getMethod = objType.GetMethod("getValue", BindingFlags.NonPublic | BindingFlags.Instance);
var target = Expression.Call(Expression.Convert(self, objType),
getMethod,
keyExpr);
return new DynamicMetaObject(target,
BindingRestrictions.GetTypeRestriction(self, objType));
}
public override DynamicMetaObject BindSetMember (SetMemberBinder binder, DynamicMetaObject value)
{
var self = this.Expression;
var keyExpr = Expression.Constant(binder.Name);
var valueExpr = Expression.Convert(value.Expression, typeof(object));
var setMethod = objType.GetMethod("setValue", BindingFlags.NonPublic | BindingFlags.Instance);
var target = Expression.Call(Expression.Convert(self, objType),
setMethod,
keyExpr,
valueExpr);
return new DynamicMetaObject(target,
BindingRestrictions.GetTypeRestriction(self, objType));
}
public override IEnumerable<string> GetDynamicMemberNames ()
{
var dynObj = (SerializableDynamicObject)this.Value;
return dynObj.getDynamicMemberNames();
}
}
使用这个自定义SerializableDynamicObject
并在解决了很多问题后,我终于停止了一种非常奇怪的行为(至少对我而言)。
@ 服务 伪代码:
dynamic X = new SerializableDynamicObject();
X . Ask = "Stackoverflow";
Ping ( X ); // Send to Client
@ 客户端 伪代码:
// Receiving X SerializableDynamicObject
dynamic Y = X;
Log ( Y . Ask ); // Logging @ Chrome Javascript Console (Silverlight Client)
最后运气消耗了2天才到达:(
情景I:
dynamicProperties
课程中SerializableDynamicObject
成员的访问修饰符公开。
客户登录= Stackoverflow
情景II:
dynamicProperties
课程中SerializableDynamicObject
成员的访问修饰符为私有或受保护或其他任何内容。
客户登录= null
问题:
dynamicProperties
成员)访问修饰符时会有所不同?setValue
,getValue
方法)访问成员是否有限制(相同的类,相同的程序集,相同的命名空间)?SerializableDynamicObject
类 true ?或者来自SerializableDynamicMetaObject
类(BindSetMember
,BindGetMember
方法)由于覆盖?我不是那个专业的c#书呆子,所以请原谅我,如果这个问题过于详细,那么提起太过琐碎的问题需要讨论!