private Expression DeserializeNew(XElement el)
        {
            var elementArray = el.Elements().ToArray();
            var type         = DeserializeTypeInfo(elementArray[0]);

            UsedTypes.Add(type.FullName);

            var parms = elementArray.Skip(1)
                        .Select(DeserializeNode)
                        .ToArray();
            var parmTypes = parms.Select(c => c.Type)
                            .ToArray();

            // Value types don't have parameterless constructors
            if (type.GetTypeInfo().IsValueType&& parms.Length == 0)
            {
                return(Expression.New(type));
            }

            return(Expression.New(KnownTypes.GetConstructor(type, parmTypes), parms));
        }
        private Type DeserializeTypeInfo(XElement el)
        {
            var typename = el.Attribute("typename").Value;
            var isArray  = el.Attribute("isarray").Value == bool.TrueString;

            var genericArgs = el.Elements().Select(DeserializeTypeInfo).ToArray();

            if (genericArgs.Length > 0)
            {
                // Remove <T, ...> at the end
                typename = Regex.Replace(typename, @"<(\w|\s|,)+>$", "");
                var type = KnownTypes.FindType(typename + "`" + genericArgs.Length);
                return(type.MakeGenericType(genericArgs));
            }

            var resultType = KnownTypes.FindType(typename);

            if (!isArray)
            {
                return(resultType);
            }
            return
                (resultType.MakeArrayType());
        }
Exemple #3
0
        private static void ResetProperties(object rootElement, string targetId, string propertyList)
        {
            var frameworkElement = rootElement as FrameworkElement;

            if (frameworkElement == null)
            {
                return;
            }

            InitialPropertyList.TryAdd(targetId, propertyList);

            foreach (var property in propertyList.Split(','))
            {
                var split = property.Split('|');

                if (split.Length != 2)
                {
                    continue;
                }

                var typePart     = split[0];
                var propertyPart = split[1];
                var lastDot      = propertyPart.LastIndexOf('.');

                if (lastDot != -1)
                {
                    var propertyOwner = propertyPart.Substring(0, lastDot);
                    var propertyName  = propertyPart.Substring(lastDot + 1);

                    var propertyOwnerType = KnownTypes.FindType(propertyOwner, rootElement);

                    if (typePart == "DependencyProperty")
                    {
                        var dpField = propertyOwnerType.GetField(propertyName, BindingFlags.Static | BindingFlags.Public);
                        if (dpField == null)
                        {
                            Debug.WriteLine("Dependency property " + propertyName + " not found on type " + propertyOwnerType.FullName);
                            continue;
                        }

                        var dp = (DependencyProperty)dpField.GetValue(null);
                        frameworkElement.ClearValue(dp);
                    }
                    else if (typePart == "RoutedEvent")
                    {
                        var reField = propertyOwnerType.GetField(propertyName, BindingFlags.Static | BindingFlags.Public);
                        if (reField == null)
                        {
                            Debug.WriteLine("Routed event " + propertyName + " not found on type " + propertyOwnerType.FullName);
                            continue;
                        }

                        var re = (DependencyProperty)reField.GetValue(null);
                        frameworkElement.ClearValue(re);
                    }
                    else if (typePart == "Property")
                    {
                        Debug.WriteLine("Resetting CLR properties not implented yet (" + propertyName + " at " + propertyOwnerType.FullName + ")");
                    }
                    else if (typePart == "Event")
                    {
                        Debug.WriteLine("Resetting CLR events not implented yet (" + propertyName + " at " + propertyOwnerType.FullName + ")");
                    }
                }
                else
                {
                }
            }
        }