【问题标题】:How to update server property on updating client property如何在更新客户端属性时更新服务器属性
【发布时间】:2019-03-18 15:31:57
【问题描述】:

我在 WCF 通信的两端都有两个相等的类:

public class UserInfo
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    ....
}

一个客户端的类更新我需要更新服务的类。 我可以使用方法实现 WCF 服务:

public interface IUserInfoUpdateContract
{
   void UpdateFirstName(string value);
   void UpdateLastName(string value);
   void UpdateAge(string value);
   ...
}

但是,是否有另一种方法可以动态更新属性? 例如:

public interface IUserInfoUpdate
{
   void UpdateProperty(string propertyName, object propertyValue);
}

在客户端的使用:

public class UserInfo
{
    private string _firstName;
    public string FirstName 
    { 
        get { return _firstName; }
        set 
        { 
            _firstName = value;
            wcfClient.UpdateProperty(nameof(FirstName), FirstName);
        }
    }
}

我有什么选项可以在没有反射的情况下动态更新服务端的属性吗?

【问题讨论】:

    标签: c# wcf reflection inotifypropertychanged


    【解决方案1】:

    由于您不想使用反射,您可以执行以下操作。将内部Dictionary 添加到UserInfo 保存所有属性值,然后可以通过属性名称引用这些值string

    public class UserInfo
    {
        private IDictionary<string, object> _dictionary = new Dictionary<string, object>();
    
        public string FirstName
        {
            get
            {
                object value;
                return _dictionary.TryGetValue(nameof(FirstName), out value) ? (string)value : null;
            }
            set
            {
                _dictionary[nameof(FirstName)] = value;
            }
        }
    
    
        public string LastName
        {
            get
            {
                object value;
                return _dictionary.TryGetValue(nameof(LastName), out value) ? (string)value : null;
            }
            set
            {
                _dictionary[nameof(LastName)] = value;
            }
        }
    
        public int Age
        {
            get
            {
                object value;
                return _dictionary.TryGetValue(nameof(Age), out value) ? (int)value : 0;
            }
            set
            {
                _dictionary[nameof(Age)] = value;
            }
        }
    
        public object this[string property]
        {
            set
            {
                //todo: validation if needed
                _dictionary[property] = value;
            }
        }
    

    用法

    var info = new UserInfo();
    info.FirstName = "hello";
    info["LastName"] = "Last";
    

    【讨论】:

      【解决方案2】:

      如果你只想申请 UserInfo 而不是任何类型,你可以使用 if else,这样你的服务器端只需要一个方法。

       public void UpdateProperty(string propertyName,object propertyValue)
          {
              UserInfo userInfo = new UserInfo(); // use your userInfo
              if(propertyName == "FirstName")
              {
                  userInfo.FirstName = propertyValue as string;
              }
              if(propertyName == "Age")
              {
                  userInfo.Age = (int)propertyValue;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多