コード例 #1
0
        void ClassListBoxOnSelectionChanged(object sender,
                                            SelectionChangedEventArgs args)
        {
            ListBox          lstbox = sender as ListBox;
            ClassAndProperty item   = lstbox.SelectedItem as ClassAndProperty;

            itemsProperties.Items.Clear();

            if (item != null)
            {
                // Get constructor and properties.
                ConstructorInfo construct = item.Type.GetConstructor(Type.EmptyTypes);
                object          obj       = construct.Invoke(null);
                PropertyInfo[]  props     = item.Type.GetProperties(BindingFlags.Public |
                                                                    BindingFlags.Instance);
                foreach (PropertyInfo prop in props)
                {
                    if (prop.PropertyType == typeof(int) ||
                        prop.PropertyType == typeof(Point3D) ||
                        prop.PropertyType == typeof(Vector3D) ||
                        prop.PropertyType == typeof(double))
                    {
                        PropertyAndValue propvalue = new PropertyAndValue(prop, prop.GetValue(obj, null).ToString());
                        itemsProperties.Items.Add(propvalue);
                    }
                }
                DoDump();
            }
        }
コード例 #2
0
        // Gets the MeshGeometry3D and converts to XAML.
        void DoDump()
        {
            txtboxDump.Text = String.Empty;
            ClassAndProperty item = lstboxClass.SelectedItem as ClassAndProperty;

            if (item != null)
            {
                // Create the object with the MeshGeometry3D property.
                ConstructorInfo construct =
                    item.Type.GetConstructor(Type.EmptyTypes);
                object obj     = construct.Invoke(null);
                bool   isError = false;

                // Get all the proeprties and set them.
                foreach (object objPropValue in itemsProperties.Items)
                {
                    PropertyAndValue propvalue = objPropValue as PropertyAndValue;
                    string           str       = propvalue.Value;

                    Type       typeProperty = propvalue.Property.PropertyType;
                    MethodInfo method       = typeProperty.GetMethod("Parse",
                                                                     new Type[] { typeof(string) });
                    object objValue;

                    try
                    {
                        objValue = method.Invoke(obj, new object[] { str });
                    }
                    catch
                    {
                        Console.WriteLine("catch1");
                        txtboxDump.Text += "Value for property " +
                                           propvalue.Property.Name + " cannot be parsed\n";
                        isError = true;
                        continue;
                    }

                    try
                    {
                        propvalue.Property.SetValue(obj, objValue, null);
                    }
                    catch
                    {
                        Console.WriteLine("catch2");
                        txtboxDump.Text += "Value for property " +
                                           propvalue.Property.Name + " cannot be set\n";
                        isError = true;
                        continue;
                    }
                }
                if (!isError)
                {
                    // Dump the MeshGeometry3D.
                    MeshGeometry3D geo = item.Property.GetValue(obj, null) as MeshGeometry3D;

                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent              = true;
                    settings.IndentChars         = new string(' ', 4);
                    settings.NewLineOnAttributes = true;
                    settings.OmitXmlDeclaration  = true;

                    StringBuilder strbuild = new StringBuilder();
                    XmlWriter     xmlwrite = XmlWriter.Create(strbuild, settings);

                    XamlWriter.Save(geo, xmlwrite);
                    txtboxDump.Text = strbuild.ToString();
                }
            }
        }