Exemple #1
0
        private static void BindProperty(CElement view, Dictionary <string, object> objectsMap, PropertyPrototype property)
        {
            // todo exceptions
            var propName = property.Name;
            var srcPath  = property.Path;

            object srcValue  = objectsMap[srcPath.Object];
            object srcObject = null;
            Type   srcType   = null;

            if (srcValue is Type)
            {
                srcType   = (Type)srcValue;
                srcObject = null;
            }
            else
            {
                srcType   = srcValue.GetType();
                srcObject = srcValue;
            }

            var srcProp = srcType.GetMember(srcPath.Member, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).FirstOrDefault(); // todo: propper overload resolving

            if (srcProp == null)
            {
                Log.Error($"Unable to assign property \"{propName}\" of object \"{view}\": source object \"{srcPath}\" does not exists");
                return;
            }

            var dstProp = view.GetType().GetMember(propName, BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(); // todo: propper overload resolving

            if (dstProp != null && !IsCompatibleBindable(dstProp.MemberType(), srcProp.MemberType()))
            {
                dstProp = view.GetType().GetMember(propName + "Prop", BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(); // todo: propper overload resolving
            }

            if (dstProp != null && IsCompatibleBindable(dstProp.MemberType(), srcProp.MemberType()))
            {
                var dstObj = dstProp.GetValue(view);
                dstProp.MemberType().GetMethod(nameof(Bindable <object> .Bind), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(object), typeof(MemberInfo) }, null)
                .Invoke(dstObj, new object[] { srcObject, srcProp });
                $"Binded {srcProp} of {srcValue} to {dstProp}".Log();
            }
            else
            {
                if (dstProp == null)
                {
                    Log.Error($"Unable to assign property \"{propName}\" of object \"{view}\": property \"{propName}\" does not exists");
                }
                else
                {
                    Log.Error($"Unable to assign property \"{propName}\" of object \"{view}\": \"{srcValue}\" is not of compatible type");
                }
            }
        }
Exemple #2
0
        private static void AssignPropertyText(CElement view, Dictionary <string, object> objectsMap, PropertyPrototype property)
        {
            var propName = property.Name;
            var propInfo = view.GetType().GetMember(propName, BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(); // todo:

            if (propInfo == null)
            {
                Log.Error($"Unable to resolve node {propName} for object {view}");
                return;
            }

            if (!propInfo.CanWrite())
            {
                Log.Error($"Unable to assign property {propName} of object {view}");
                return;
            }

            var valueType = property.TypeHint ?? propInfo.MemberType();

            valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;

            var value = ParseFromText(property.Value, property.Translate, valueType);

            propInfo.SetValue(view, value);

            $"Assigned {value} to {propName}".Log();
        }