可访问性级别问题

时间:2012-07-21 04:50:35

标签: c# override internal access-modifiers

这不是真正的问题标题,因为我真的不知道问题在哪里,所以我只是在猜测。

故事开始尝试向我的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成员)访问修饰符时会有所不同?
  • 从内部方法(setValuegetValue方法)访问成员是否有限制(相同的类,相同的程序集,相同的命名空间)?
  • 我认为问题来自SerializableDynamicObject true ?或者来自SerializableDynamicMetaObject类(BindSetMemberBindGetMember方法)由于覆盖

我不是那个专业的c#书呆子,所以请原谅我,如果这个问题过于详细,那么提起太过琐碎的问题需要讨论!

0 个答案:

没有答案