Commit fa4a5134 authored by 潘栩锋's avatar 潘栩锋 🚴

修复 数组property没有注册PropertyChanged 事件

parent 8a2ac8c7
using System; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using static WSCF.Reflect_OBJ_INTERFACE;
namespace WSCF namespace WSCF
{ {
...@@ -47,11 +50,14 @@ namespace WSCF ...@@ -47,11 +50,14 @@ namespace WSCF
//处理[PropertyPush] //处理[PropertyPush]
var propertyInfos = GetAllPropertyInfos(rootNode.InterfaceType); var propertyInfos = GetAllPropertyInfos(rootNode.InterfaceType);
//找到全部[PropertyPush]
propertyInfos = propertyInfos.FindAll(p => p.GetCustomAttribute<PropertyPushAttribute>() != null);
if (propertyInfos.Count() == 0)
return;
foreach (var propertyInfo in propertyInfos) foreach (var propertyInfo in propertyInfos)
{ {
if (propertyInfo.GetCustomAttribute<PropertyPushAttribute>() == null)
continue;//必须是[PropertyPush]
SubPropertyNode node = new SubPropertyNode(); SubPropertyNode node = new SubPropertyNode();
if (ReInitPropertyPush(node, rootNode.Obj, propertyInfo, rootNode.SubPropertyChanged)) if (ReInitPropertyPush(node, rootNode.Obj, propertyInfo, rootNode.SubPropertyChanged))
{ {
...@@ -256,6 +262,63 @@ namespace WSCF ...@@ -256,6 +262,63 @@ namespace WSCF
//重新注册 //重新注册
COMMON.ReInitPropertyPush(sub_node, node.Obj, propertyInfo, node.SubPropertyChanged); COMMON.ReInitPropertyPush(sub_node, node.Obj, propertyInfo, node.SubPropertyChanged);
} }
public static ReflectData Sub_PropertyChanged(
SubPropertyNode node, PropertyChangedEventArgs e,
SubPropertyNode rootNode, List<string> rootProperties)
{
if (rootNode == node)
{
//根对象
if (!rootProperties.Contains(e.PropertyName))//InterfaceType不包含它,不需要推送
return null;
}
var type = node.Obj.GetType();
var propertyInfo = type.GetProperty(e.PropertyName);
if (propertyInfo == null)
return null;//获取失败!!!
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
return null;//这个不需要推送
var v = propertyInfo.GetValue(node.Obj);
var jObject = new JObject();
if (v == null)
jObject.Add(propertyInfo.Name, null);
else
jObject.Add(propertyInfo.Name, JToken.FromObject(v));
var rData = new ReflectData
{
name = COMMON.GetNodePath(node),
data = jObject
};
return rData;
}
public static void InitPropertyChanged(Type interfaceType, object obj, SubPropertyNode rootNode, List<string> properties)
{
if (!typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType))
return;
//继承了INotifyPropertyChanged
((INotifyPropertyChanged)obj).PropertyChanged += rootNode.PropertyChanged;
var propertyInfos = COMMON.GetAllPropertyInfos(interfaceType);//获取全部属性, 包括父类的属性
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
continue;//这个不需要推送
if (properties.Contains(propertyInfo.Name))
continue;
properties.Add(propertyInfo.Name);
}
}
} }
/// <summary> /// <summary>
......
...@@ -125,8 +125,12 @@ namespace WSCF ...@@ -125,8 +125,12 @@ namespace WSCF
SubPropertyChanged = Sub_PropertyChanged SubPropertyChanged = Sub_PropertyChanged
}; };
InitPropertyChanged(); //注册 obj 的PropertyChanged 事件,获取 interfaceType 全部属性名称,包括它的父类的全部属性名称
COMMON.InitPropertyChanged(interfaceType, obj, rootNode, properties);
//处理[PropertyPush]
COMMON.InitPropertyPush(rootNode); COMMON.InitPropertyPush(rootNode);
InitEventPush(); InitEventPush();
InitCall(); InitCall();
...@@ -142,27 +146,6 @@ namespace WSCF ...@@ -142,27 +146,6 @@ namespace WSCF
} }
}; };
} }
void InitPropertyChanged()
{
if (!typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType))
return;
//继承了INotifyPropertyChanged
this.PropertyChanged += rootNode.PropertyChanged;
var propertyInfos = COMMON.GetAllPropertyInfos(this.interfaceType);//获取全部属性, 包括父类的属性
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
continue;//这个不需要推送
if (properties.Contains(propertyInfo.Name))
continue;
properties.Add(propertyInfo.Name);
}
}
void InitEventPush() void InitEventPush()
{ {
...@@ -247,34 +230,10 @@ namespace WSCF ...@@ -247,34 +230,10 @@ namespace WSCF
if (ignoreSet)//从服务器接收的数据,不用再推送给服务器 if (ignoreSet)//从服务器接收的数据,不用再推送给服务器
return; return;
if (rootNode == node) var rData = COMMON.Sub_PropertyChanged(node, e, rootNode, properties);
{ if (rData == null)
//根对象
if (!properties.Contains(e.PropertyName))//InterfaceType不包含它,不需要推送
return; return;
}
var type = node.Obj.GetType();
var propertyInfo = type.GetProperty(e.PropertyName);
if (propertyInfo == null)
return;//获取失败!!!
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
return;//这个不需要推送
var v = propertyInfo.GetValue(node.Obj);
var jObject = new JObject();
if (v == null)
jObject.Add(propertyInfo.Name, null);
else
jObject.Add(propertyInfo.Name, JToken.FromObject(v));
var rData = new ReflectData
{
name = COMMON.GetNodePath(node),
data = jObject
};
send_CALL_SetProperty(rData); send_CALL_SetProperty(rData);
} }
......
...@@ -60,11 +60,21 @@ namespace WSCF ...@@ -60,11 +60,21 @@ namespace WSCF
Proxy.OnMessage(this, pkgData); Proxy.OnMessage(this, pkgData);
} }
protected override void OnOpen()
{
base.OnOpen();
Proxy.OnOpen();
}
protected override void OnClose(CloseEventArgs e)
{
base.OnClose(e);
Proxy.OnClose();
}
public virtual void SendTo(string msg) public virtual void SendTo(string msg)
{ {
if (this.State != WebSocketState.Open)
return;
base.Send(msg); base.Send(msg);
} }
......
...@@ -17,7 +17,7 @@ namespace WSCF ...@@ -17,7 +17,7 @@ namespace WSCF
/// <summary> /// <summary>
/// 代理对象 接口类型 /// 代理对象 接口类型
/// </summary> /// </summary>
protected virtual Type InterfaceType { get; } protected abstract Type InterfaceType { get; }
/// <summary> /// <summary>
/// 连接成功 /// 连接成功
......
...@@ -90,7 +90,7 @@ namespace WSCF ...@@ -90,7 +90,7 @@ namespace WSCF
}; };
//注册 obj 的PropertyChanged 事件,获取 interfaceType 全部属性名称,包括它的父类的全部属性名称 //注册 obj 的PropertyChanged 事件,获取 interfaceType 全部属性名称,包括它的父类的全部属性名称
InitPropertyChanged(); COMMON.InitPropertyChanged(interfaceType, obj, rootNode, properties);
//处理[PropertyPush] //处理[PropertyPush]
COMMON.InitPropertyPush(rootNode); COMMON.InitPropertyPush(rootNode);
...@@ -98,27 +98,6 @@ namespace WSCF ...@@ -98,27 +98,6 @@ namespace WSCF
//处理[Push] //处理[Push]
InitEventPush(); InitEventPush();
} }
void InitPropertyChanged()
{
if (!typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType))
return;
//继承了INotifyPropertyChanged
((INotifyPropertyChanged)(this.obj)).PropertyChanged += rootNode.PropertyChanged;
var propertyInfos = COMMON.GetAllPropertyInfos(this.interfaceType);//获取全部属性, 包括父类的属性
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
continue;//这个不需要推送
if (properties.Contains(propertyInfo.Name))
continue;
properties.Add(propertyInfo.Name);
}
}
void InitEventPush() void InitEventPush()
{ {
...@@ -156,29 +135,10 @@ namespace WSCF ...@@ -156,29 +135,10 @@ namespace WSCF
} }
void Sub_PropertyChanged(SubPropertyNode node, PropertyChangedEventArgs e) void Sub_PropertyChanged(SubPropertyNode node, PropertyChangedEventArgs e)
{ {
//if (ignoreSet)//从服务器接收的数据,不用再推送给服务器 var rData = COMMON.Sub_PropertyChanged(node, e, rootNode, properties);
// return; if (rData == null)
var type = node.Obj.GetType(); return;
var propertyInfo = type.GetProperty(e.PropertyName);
if (propertyInfo == null)
return;//获取失败!!!
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
return;//这个不需要推送
var v = propertyInfo.GetValue(node.Obj);
var jObject = new JObject();
if (v == null)
jObject.Add(propertyInfo.Name, null);
else
jObject.Add(propertyInfo.Name, JToken.FromObject(v));
var rData = new ReflectData
{
name = COMMON.GetNodePath(node),
data = jObject
};
reponse_PushObjInfoEx(rData, false); reponse_PushObjInfoEx(rData, false);
} }
...@@ -334,7 +294,7 @@ namespace WSCF ...@@ -334,7 +294,7 @@ namespace WSCF
JToken.FromObject(rData)); JToken.FromObject(rData));
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
SendEx(json); SendEx?.Invoke(json);
} }
void reponse_CALL_MethodInvoke(ReflectData rData, string guid) void reponse_CALL_MethodInvoke(ReflectData rData, string guid)
...@@ -347,7 +307,7 @@ namespace WSCF ...@@ -347,7 +307,7 @@ namespace WSCF
JToken.FromObject(rData)); JToken.FromObject(rData));
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
SendEx(json); SendEx?.Invoke(json);
} }
void reponse_CALL_GetAllProperties(ReflectData rData, string guid) void reponse_CALL_GetAllProperties(ReflectData rData, string guid)
...@@ -360,7 +320,7 @@ namespace WSCF ...@@ -360,7 +320,7 @@ namespace WSCF
JToken.FromObject(rData)); JToken.FromObject(rData));
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
SendEx(json); SendEx?.Invoke(json);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment