Commit 69dd847a authored by 潘栩锋's avatar 潘栩锋 🚴

1. 修复 FObject.Reflect 接口无法或者父接口的事件

parent 74173d07
...@@ -18,53 +18,110 @@ namespace FObjBase.Reflect ...@@ -18,53 +18,110 @@ namespace FObjBase.Reflect
public bool ignoreSet = false; public bool ignoreSet = false;
List<AnyEvent> anyEvents = new List<AnyEvent>(); List<AnyEvent> anyEvents = new List<AnyEvent>();
Dictionary<object, string> subProperties = new Dictionary<object, string>(); Dictionary<object, string> subProperties = new Dictionary<object, string>();
List<string> properties = new List<string>();
public Reflect_Proxy(int objsys_idx, UInt32 id, Type interfaceType, object obj) : base(objsys_idx) public Reflect_Proxy(int objsys_idx, UInt32 id, Type interfaceType, object obj) : base(objsys_idx)
{ {
ID = id; ID = id;
this.interfaceType = interfaceType; this.interfaceType = interfaceType;
this.obj = obj; this.obj = obj;
if(typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType))
{ InitPropertyChanged();
//继承了INotifyPropertyChanged
((INotifyPropertyChanged)(this.obj)).PropertyChanged += Obj_PropertyChanged;
}
//处理[PropertyPush] //处理[PropertyPush]
InitPropertyPush(); InitPropertyPush();
//注册全部事件 //处理[Push]
var eventInfos = this.interfaceType.GetEvents(); InitEventPush();
}
void InitPropertyChanged()
{
if (!typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType))
return;
//继承了INotifyPropertyChanged
((INotifyPropertyChanged)(this.obj)).PropertyChanged += Obj_PropertyChanged;
var interfaceTypes = new List<Type>();
interfaceTypes.Add(this.interfaceType);
interfaceTypes.AddRange(this.interfaceType.GetInterfaces());
var propertyInfos = new List<PropertyInfo>();
foreach (var ifaceType in interfaceTypes)
{
propertyInfos.AddRange(ifaceType.GetProperties());
}
foreach (var propertyInfo in propertyInfos) {
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
continue;//这个不需要推送
if (properties.Contains(propertyInfo.Name))
continue;
properties.Add(propertyInfo.Name);
}
}
void InitEventPush()
{
var interfaceTypes = new List<Type>();
interfaceTypes.Add(this.interfaceType);
interfaceTypes.AddRange(this.interfaceType.GetInterfaces());
var eventInfos = new List<EventInfo>();
foreach (var ifaceType in interfaceTypes) {
eventInfos.AddRange(ifaceType.GetEvents());
}
foreach (var eventInfo in eventInfos) foreach (var eventInfo in eventInfos)
{ {
var pushAttribute = eventInfo.GetCustomAttribute<PushAttribute>(); var pushAttribute = eventInfo.GetCustomAttribute<PushAttribute>();
if (pushAttribute != null) if (pushAttribute == null)
return;
if (anyEvents.Any(ae => ae.eventName == eventInfo.Name))
return;//已经添加了
var anyEvent = new AnyEvent()
{ {
var anyEvent = new AnyEvent() eventName = eventInfo.Name,
{ PushObjInfoEx = (msg) => {
eventName = eventInfo.Name, var buf = Misc.Converter.StringToBytes(msg);
PushObjInfoEx = (msg) => { CurrObjSys.PushObjInfoEx(
var buf = Misc.Converter.StringToBytes(msg); this, Reflect_OBJ_INTERFACE.PUSH_Event,
CurrObjSys.PushObjInfoEx( buf);
this, Reflect_OBJ_INTERFACE.PUSH_Event, }
buf); };
} //这个事件需要推送
}; eventInfo.AddEventHandler(obj, anyEvent.eventHandler);
//这个事件需要推送 anyEvents.Add(anyEvent);
eventInfo.AddEventHandler(obj, anyEvent.eventHandler);
anyEvents.Add(anyEvent);
}
} }
} }
void InitPropertyPush() { void InitPropertyPush() {
//处理[PropertyPush] //处理[PropertyPush]
var propertyInfos = interfaceType.GetProperties(); var interfaceTypes = new List<Type>();
interfaceTypes.Add(this.interfaceType);
interfaceTypes.AddRange(this.interfaceType.GetInterfaces());
var propertyInfos = new List<PropertyInfo>();
foreach (var ifaceType in interfaceTypes)
propertyInfos.AddRange(ifaceType.GetProperties());
foreach (var propertyInfo in propertyInfos) foreach (var propertyInfo in propertyInfos)
{ {
if (!IsPropertyPush(propertyInfo)) if (!IsPropertyPush(propertyInfo))
continue; continue;
if (!properties.Contains(propertyInfo.Name)) {
properties.Add(propertyInfo.Name);
}
InitSubPropertyPush(this.obj.GetType().GetProperty(propertyInfo.Name), this.obj, null); InitSubPropertyPush(this.obj.GetType().GetProperty(propertyInfo.Name), this.obj, null);
} }
} }
...@@ -122,11 +179,7 @@ namespace FObjBase.Reflect ...@@ -122,11 +179,7 @@ namespace FObjBase.Reflect
} }
} }
class SubProperty
{
public string propertyName;
public object value;
}
private void Obj_PropertyChanged(object sender, PropertyChangedEventArgs e) private void Obj_PropertyChanged(object sender, PropertyChangedEventArgs e)
{ {
...@@ -136,22 +189,15 @@ namespace FObjBase.Reflect ...@@ -136,22 +189,15 @@ namespace FObjBase.Reflect
if (interfaceType == null) if (interfaceType == null)
return; return;
var type = interfaceType; if (!properties.Contains(e.PropertyName))
var propertyInfo = type.GetProperty(e.PropertyName);
if (propertyInfo == null)
return;//获取失败!!!
if(propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>()!=null)
return;//这个不需要推送 return;//这个不需要推送
var v = this.obj.GetType().GetProperty(propertyInfo.Name).GetValue(sender); var v = this.obj.GetType().GetProperty(e.PropertyName).GetValue(sender);
var jObject = new JObject(); var jObject = new JObject();
if(v == null) if(v == null)
jObject.Add(propertyInfo.Name, null); jObject.Add(e.PropertyName, null);
else else
jObject.Add(propertyInfo.Name, JToken.FromObject(v)); jObject.Add(e.PropertyName, JToken.FromObject(v));
string json = jObject.ToString(Formatting.None); string json = jObject.ToString(Formatting.None);
var buf = Misc.Converter.StringToBytes(json); var buf = Misc.Converter.StringToBytes(json);
CurrObjSys.PushObjInfoEx( CurrObjSys.PushObjInfoEx(
...@@ -212,15 +258,9 @@ namespace FObjBase.Reflect ...@@ -212,15 +258,9 @@ namespace FObjBase.Reflect
var type = obj.GetType(); var type = obj.GetType();
var propertyInfos = type.GetProperties(); foreach (var propertyName in properties)
foreach (var propertyInfo in propertyInfos)
{ {
var attr = propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>(false); var propertyInfo = type.GetProperty(propertyName);
if (attr != null)
{
return;//这个不需要推送
}
var v = propertyInfo.GetValue(obj); var v = propertyInfo.GetValue(obj);
if(v == null) if(v == null)
jObject.Add(propertyInfo.Name, null); jObject.Add(propertyInfo.Name, null);
......
...@@ -16,7 +16,23 @@ namespace FObjBase.Reflect ...@@ -16,7 +16,23 @@ namespace FObjBase.Reflect
protected virtual Type InterfaceType { get; } protected virtual Type InterfaceType { get; }
List<AnyEvent> anyEvents = new List<AnyEvent>();
List<AnyCall> anyCalls = new List<AnyCall>();
class AnyEvent
{
public string name;
public string triggerName;
public Type retType;
}
class AnyCall
{
public string name;
public Type retType;
}
Dictionary<object,string> subProperties = new Dictionary<object, string>(); Dictionary<object,string> subProperties = new Dictionary<object, string>();
List<string> properties = new List<string>();
public Reflect_SeviceClient(UInt32 id) : base(id) { public Reflect_SeviceClient(UInt32 id) : base(id) {
init(); init();
} }
...@@ -26,19 +42,54 @@ namespace FObjBase.Reflect ...@@ -26,19 +42,54 @@ namespace FObjBase.Reflect
init(); init();
} }
void init() { void init() {
if (typeof(INotifyPropertyChanged).IsAssignableFrom(InterfaceType))
InitPropertyChanged();
InitPropertyPush();
InitEventPush();
InitCall();
}
void InitPropertyChanged()
{
if (!typeof(INotifyPropertyChanged).IsAssignableFrom(InterfaceType))
return;
//继承了INotifyPropertyChanged
this.PropertyChanged += Obj_PropertyChanged;
var interfaceTypes = new List<Type>();
interfaceTypes.Add(this.InterfaceType);
interfaceTypes.AddRange(this.InterfaceType.GetInterfaces());
var propertyInfos = new List<PropertyInfo>();
foreach (var ifaceType in interfaceTypes)
{ {
//继承了INotifyPropertyChanged propertyInfos.AddRange(ifaceType.GetProperties());
this.PropertyChanged += Obj_PropertyChanged;
} }
InitPropertyPush(); foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
continue;//这个不需要推送
if (properties.Contains(propertyInfo.Name))
continue;
properties.Add(propertyInfo.Name);
}
} }
void InitPropertyPush() void InitPropertyPush()
{ {
//处理[PropertyPush] //处理[PropertyPush]
var propertyInfos = InterfaceType.GetProperties(); var interfaceTypes = new List<Type>();
interfaceTypes.Add(this.InterfaceType);
interfaceTypes.AddRange(this.InterfaceType.GetInterfaces());
var propertyInfos = new List<PropertyInfo>();
foreach (var ifaceType in interfaceTypes)
propertyInfos.AddRange(ifaceType.GetProperties());
foreach (var propertyInfo in propertyInfos) foreach (var propertyInfo in propertyInfos)
{ {
if (!IsPropertyPush(propertyInfo)) if (!IsPropertyPush(propertyInfo))
...@@ -80,7 +131,72 @@ namespace FObjBase.Reflect ...@@ -80,7 +131,72 @@ namespace FObjBase.Reflect
} }
} }
void InitEventPush()
{
var interfaceTypes = new List<Type>();
interfaceTypes.Add(InterfaceType);
interfaceTypes.AddRange(InterfaceType.GetInterfaces());
var eventInfos = new List<EventInfo>();
foreach (var ifaceType in interfaceTypes)
{
eventInfos.AddRange(ifaceType.GetEvents());
}
foreach (var eventInfo in eventInfos)
{
var pushAttribute = eventInfo.GetCustomAttribute<PushAttribute>();
if (pushAttribute == null)
return;
if (anyEvents.Any(ae => ae.name == eventInfo.Name))
return;//已经添加了
var anyEvent = new AnyEvent()
{
name = eventInfo.Name,
triggerName = pushAttribute.TriggerName,
retType = pushAttribute.EventArgsType
};
anyEvents.Add(anyEvent);
}
}
void InitCall()
{
var interfaceTypes = new List<Type>();
interfaceTypes.Add(InterfaceType);
interfaceTypes.AddRange(InterfaceType.GetInterfaces());
var methodInfos = new List<MethodInfo>();
foreach (var ifaceType in interfaceTypes)
{
methodInfos.AddRange(ifaceType.GetMethods());
}
foreach (var methodInfo in methodInfos)
{
var callAttribute = methodInfo.GetCustomAttribute<CallAttribute>();
if (callAttribute == null)
return;
if (anyCalls.Any(ae => ae.name == methodInfo.Name))
return;//已经添加了
var anyCall = new AnyCall()
{
name = methodInfo.Name,
retType = callAttribute.ReponseType
};
anyCalls.Add(anyCall);
}
}
private void Obj_PropertyChanged(object sender, PropertyChangedEventArgs e) private void Obj_PropertyChanged(object sender, PropertyChangedEventArgs e)
{ {
if (!IsConnected) if (!IsConnected)
...@@ -92,21 +208,16 @@ namespace FObjBase.Reflect ...@@ -92,21 +208,16 @@ namespace FObjBase.Reflect
if (InterfaceType == null) if (InterfaceType == null)
return; return;
var type = InterfaceType; if (!properties.Contains(e.PropertyName))
var propertyInfo = type.GetProperty(e.PropertyName);
if (propertyInfo == null)
return;//获取失败!!!
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
return;//这个不需要推送 return;//这个不需要推送
var v = GetType().GetProperty(propertyInfo.Name).GetValue(sender);
var v = GetType().GetProperty(e.PropertyName).GetValue(sender);
var jObject = new JObject(); var jObject = new JObject();
if(v == null) if(v == null)
jObject.Add(propertyInfo.Name, null); jObject.Add(e.PropertyName, null);
else else
jObject.Add(propertyInfo.Name, JToken.FromObject(v)); jObject.Add(e.PropertyName, JToken.FromObject(v));
string json = jObject.ToString(Formatting.None); string json = jObject.ToString(Formatting.None);
...@@ -205,14 +316,14 @@ namespace FObjBase.Reflect ...@@ -205,14 +316,14 @@ namespace FObjBase.Reflect
string json = Misc.Converter.BytesToString(infodata); string json = Misc.Converter.BytesToString(infodata);
var rData = JsonConvert.DeserializeObject<Reflect_OBJ_INTERFACE.ReflectData>(json); var rData = JsonConvert.DeserializeObject<Reflect_OBJ_INTERFACE.ReflectData>(json);
var type = InterfaceType; var anyEvent = anyEvents.Find(ae => ae.name == rData.name);
if (anyEvent == null)
return;//异常!!!
var eventInfo = type.GetEvent(rData.name);
var pushAttribute = eventInfo.GetCustomAttribute<PushAttribute>();
//触发事件!!! //触发事件!!!
var methodInfo = GetType().GetMethod(pushAttribute.TriggerName); var methodInfo = GetType().GetMethod(anyEvent.triggerName);
var obj = rData.data.ToObject(pushAttribute.EventArgsType); var obj = rData.data.ToObject(anyEvent.retType);
methodInfo.Invoke(this, new object[] { obj }); methodInfo.Invoke(this, new object[] { obj });
} }
break; break;
...@@ -239,17 +350,14 @@ namespace FObjBase.Reflect ...@@ -239,17 +350,14 @@ namespace FObjBase.Reflect
string json = Misc.Converter.BytesToString(retdata); string json = Misc.Converter.BytesToString(retdata);
var reponse = JsonConvert.DeserializeObject<Reflect_OBJ_INTERFACE.ReflectData>(json); var reponse = JsonConvert.DeserializeObject<Reflect_OBJ_INTERFACE.ReflectData>(json);
var anyCall = anyCalls.Find(ac => ac.name == reponse.name);
var type = InterfaceType; if (anyCall == null)
{
var methodInfo = type.GetMethod(reponse.name);
var callAttribute = methodInfo.GetCustomAttribute<CallAttribute>();
if (callAttribute == null) {
//异常!!!应该有[Call]注明返回的类型 //异常!!!应该有[Call]注明返回的类型
throw new Exception($"{reponse.name} 异常!!!应该有[Call]注明返回的类型"); throw new Exception($"{reponse.name} 异常!!!应该有[Call]注明返回的类型");
} }
var retData = reponse.data.ToObject(callAttribute.ReponseType); var retData = reponse.data.ToObject(anyCall.retType);
((AsyncCBHandler)asyncDelegate).Invoke(asyncContext, retData); ((AsyncCBHandler)asyncDelegate).Invoke(asyncContext, retData);
} }
break; break;
......
...@@ -147,7 +147,7 @@ namespace FLY.Thick.Base.Client ...@@ -147,7 +147,7 @@ namespace FLY.Thick.Base.Client
/// <param name="filmPos">极片位置m</param> /// <param name="filmPos">极片位置m</param>
public void SetFilmPosAt01(double filmPos) public void SetFilmPosAt01(double filmPos)
{ {
Call("SetFilmPosAt01", filmPos); Call("SetFilmPosAt01", new { filmPos });
} }
/// <summary> /// <summary>
......
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