Commit 64a8f5dc authored by 潘栩锋's avatar 潘栩锋 🚴

从 FObject.Reflect 复制过来。完善一些小问题

parent 75ccbf90
......@@ -11,6 +11,16 @@
<xs:documentation>Used to control if the On_PropertyName_Changed feature is enabled.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="TriggerDependentProperties" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to control if the Dependent properties feature is enabled.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="EnableIsChangedProperty" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to control if the IsChanged property feature is enabled.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="EventInvokerNames" type="xs:string">
<xs:annotation>
<xs:documentation>Used to change the name of the method that fires the notify event. This is a string that accepts multiple values in a comma separated form.</xs:documentation>
......@@ -31,6 +41,16 @@
<xs:documentation>Used to control if equality checks should use the static Equals method resolved from the base class.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="SuppressWarnings" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to turn off build warnings from this weaver.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="SuppressOnPropertyNameChangedWarning" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to turn off build warnings about mismatched On_PropertyName_Changed methods.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:all>
......
......@@ -29,7 +29,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="PropertyChanged.Fody" Version="3.2.5" />
<PackageReference Include="PropertyChanged.Fody" Version="3.3.1" />
<PackageReference Include="WebSocketSharp-netstandard" Version="1.0.1" />
</ItemGroup>
......
......@@ -38,6 +38,7 @@ namespace WSCF
this.interfaceType = interfaceType;
this.obj = obj;
//注册 obj 的PropertyChanged 事件,获取 interfaceType 全部属性名称,包括它的父类的全部属性名称
InitPropertyChanged();
//处理[PropertyPush]
......@@ -76,15 +77,7 @@ namespace WSCF
}, this.obj));
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());
}
var propertyInfos = GetAllPropertyInfos();//获取全部属性, 包括父类的属性
foreach (var propertyInfo in propertyInfos)
{
......@@ -97,6 +90,23 @@ namespace WSCF
properties.Add(propertyInfo.Name);
}
}
/// <summary>
/// 获取全部属性, 包括父类的属性
/// </summary>
/// <returns></returns>
List<PropertyInfo> GetAllPropertyInfos()
{
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());
}
return propertyInfos;
}
void InitEventPush()
{
var interfaceTypes = new List<Type>();
......@@ -145,14 +155,7 @@ namespace WSCF
void InitPropertyPush()
{
//处理[PropertyPush]
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());
var propertyInfos = GetAllPropertyInfos();//获取全部属性, 包括父类的属性
foreach (var propertyInfo in propertyInfos)
......@@ -165,6 +168,9 @@ namespace WSCF
properties.Add(propertyInfo.Name);
}
//这个属性可能是 父类的, interfaceType可能没有;
//obj 肯定有
//继续枚举 propertyInfo 下面的 [PropertyPush]
InitSubPropertyPush(this.obj.GetType().GetProperty(propertyInfo.Name), this.obj, null);
}
}
......@@ -208,8 +214,8 @@ namespace WSCF
}
private void Obj_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (ignoreSet)//从服务器接收的数据,不用再推送给服务器
return;
//if (ignoreSet)//从服务器接收的数据,不用再推送给服务器
// return;
if (interfaceType == null)
return;
......@@ -245,8 +251,8 @@ namespace WSCF
}
private void Sub_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (ignoreSet)//从服务器接收的数据,不用再推送给服务器
return;
//if (ignoreSet)//从服务器接收的数据,不用再推送给服务器
// return;
if (!subProperties.ContainsKey(sender))
return;//异常, 不是子property
......@@ -346,19 +352,26 @@ namespace WSCF
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_SetProperty:
{
ignoreSet = true;
//ignoreSet = true;
//TODO
string json = pkgData.data.ToString();
JsonConvert.PopulateObject(json, obj);
ignoreSet = false;
//ignoreSet = false;
}
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke:
{
var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
var type = obj.GetType();
var methodInfo = type.GetMethod(rData.name);
var paramNames_req = rData.data.Children().OfType<JProperty>().Select(p => p.Name);
MethodInfo methodInfo = GetMethodInfo(type, rData.name, paramNames_req);
if (methodInfo == null)
{
//不能找到,
throw new Exception($"程序写错了, 不能找到 rData.name={rData.name} 或者 参数名称不对,没法完全匹配");
}
var parameterInfos = methodInfo.GetParameters();
object[] parameters = new object[parameterInfos.Count()];
for (int i = 0; i < parameters.Count(); i++)
......@@ -384,7 +397,41 @@ namespace WSCF
}
MethodInfo GetMethodInfo(Type type, string name, IEnumerable<string> parameterNames)
{
var methodInfos = from mi in type.GetMethods() where mi.Name == name select mi;
if (methodInfos.Count() == 0)
return null;
if (methodInfos.Count() == 1)
return methodInfos.First();
//必须完全匹配
foreach (var methodInfo in methodInfos)
{
var parameterInfos = methodInfo.GetParameters();
//全部参数名称
var names = parameterInfos.Select(pi => pi.Name).ToList();
//删除掉 asyncDelegate,asyncContext
names.Remove(Reflect_OBJ_INTERFACE.asyncDelegate);
names.Remove(Reflect_OBJ_INTERFACE.asyncContext);
var names_req = parameterNames;
if (names.Count() != names_req.Count())
continue;//数量不一致,肯定不同
var sames = names_req.Intersect(names);
if (sames.Count() != names_req.Count())
continue;// names 与 names_req 的交集数量与names_req不一样,肯定不同
//就是它
return methodInfo;
}
return null;
}
void asyncDelegate(object asyncContext, object retData)
{
var cc = (CC)asyncContext;
......
......@@ -348,6 +348,10 @@ namespace WSCF
//触发事件!!!
var methodInfo = GetType().GetMethod(anyEvent.triggerName);
if (methodInfo == null)
{
throw new Exception($"客户端 {GetType()} 忘记写 {anyEvent.triggerName}");
}
var obj = rData.data.ToObject(anyEvent.retType);
//出错,就提示,肯定是客户端忘记写 "Trigger_XXXX"
methodInfo.Invoke(this, new object[] { obj });
......
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