Exemplo n.º 1
0
        public static void BindableObjectField(CustomBinder target, int index)
        {
            if (target == null)
            {
                return;
            }
            int    key = target.GetHashCode();
            string propertyName;

            dicPropertyName.TryGetValue(key, out propertyName);
            EditorGUILayout.Separator();
            EditorGUILayout.Space();

            EditorGUILayout.BeginHorizontal();
            var temp = target as CustomBinder;

            GUILayout.Label(target.name, GUILayout.Width(100));
            GUILayout.Label("property:", GUILayout.MinWidth(60));
            // EditorGUILayout.ObjectField(temp.binderTarget, typeof(CustomBinder), false, GUILayout.MaxWidth(150)); //显示绑定对象
            int selectedIndex = PopupComponentsProperty(temp.target, propertyName, GUILayout.MaxWidth(150)); //绑定属性

            propertyName         = GetSelectedPropertyByIndex(selectedIndex);
            dicPropertyName[key] = propertyName;
            if (GUILayout.Button("+", GUILayout.MaxWidth(50)))
            {
                if (selectedIndex == 0)
                {
                    Debug.LogWarningFormat("please choose a property to binding");
                    return;
                }
                if (temp.bindings == null)
                {
                    temp.bindings = new List <Binding>();
                }

                Binding expression = new Binding();
                expression.propertyName = propertyName;
                temp.bindings.Add(expression);
            }
            Undo.RecordObject(target, "F");
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Separator();

            if (temp.bindings != null)
            {
                for (int i = 0; i < temp.bindings.Count; i++)
                {
                    EditorGUILayout.BeginHorizontal();
                    var binding = temp.bindings[i];
                    BindableExpressionEditor.Expression(binding, i);

                    if (GUILayout.Button("-", GUILayout.Width(30)))
                    {
                        RemoveAtbindableObjects(temp, i);
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
        }
 object DeserializeObject(ITypeResolutionService typeResolver)
 {
     try {
         if (mime_type == ResXResourceWriter.SoapSerializedObjectMimeType)
         {
             //FIXME: theres a test in the suite to check that a type converter converts from invariant string
             //do i need to take the string culture into consideration here?
             SoapFormatter soapF = new SoapFormatter();
             if (binder == null)
             {
                 binder = new CustomBinder(typeResolver);
             }
             soapF.Binder = binder;
             byte [] data = Convert.FromBase64String(dataString);
             using (MemoryStream s = new MemoryStream(data)) {
                 return(soapF.Deserialize(s));
             }
         }
         else if (mime_type == ResXResourceWriter.BinSerializedObjectMimeType)
         {
             BinaryFormatter binF = new BinaryFormatter();
             if (binder == null)
             {
                 binder = new CustomBinder(typeResolver);
             }
             binF.Binder = binder;
             byte [] data = Convert.FromBase64String(dataString);
             using (MemoryStream s = new MemoryStream(data)) {
                 return(binF.Deserialize(s));
             }
         }
         else                   // invalid mime_type
         {
             return(null);
         }
     } catch (SerializationException ex) {
         if (ex.Message.StartsWith("Couldn't find assembly"))
         {
             throw new ArgumentException(ex.Message);
         }
         else
         {
             throw ex;
         }
     }
 }
Exemplo n.º 3
0
        public static void Main()
        {
            Type t = typeof(CustomBinderDriver);
            CustomBinder binder = new CustomBinder();
            BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance |
                BindingFlags.Public | BindingFlags.Static;
            object[] args;

            // Case 1. Neither argument coercion nor member selection is needed.
            args = new object[] { };
            t.InvokeMember("PrintBob", flags, binder, null, args);

            // Only member selection is needed.
            args = new object[] { (long) 42 };
            t.InvokeMember("PrintValue", flags, binder, null, args);

            // Only member selection is needed.
            args = new object[] { "5.5" };
            t.InvokeMember("PrintValue", flags, binder, null, args);
        }
Exemplo n.º 4
0
    public static void Main()
    {
        Type         t      = typeof(CustomBinderDriver);
        CustomBinder binder = new CustomBinder();
        BindingFlags flags  = BindingFlags.InvokeMethod | BindingFlags.Instance |
                              BindingFlags.Public | BindingFlags.Static;

        object[] args;

        // Case 1. Neither argument coercion nor member selection is needed.
        args = new object[] {};
        t.InvokeMember("PrintBob", flags, binder, null, args);

        // Case 2. Only member selection is needed.
        args = new object[] { 42 };
        t.InvokeMember("PrintValue", flags, binder, null, args);

        // Case 3. Only argument coercion is needed.
        args = new object[] { "5.5" };
        t.InvokeMember("PrintNumber", flags, binder, null, args);
    }
Exemplo n.º 5
0
		object DeserializeObject (ITypeResolutionService typeResolver)
		{
			try {
				if (mime_type == ResXResourceWriter.SoapSerializedObjectMimeType) {
					//FIXME: theres a test in the suite to check that a type converter converts from invariant string
					//do i need to take the string culture into consideration here?
					SoapFormatter soapF = new SoapFormatter ();
					if (binder == null)
						binder = new CustomBinder (typeResolver);
					soapF.Binder = binder;
					byte [] data = Convert.FromBase64String (dataString);
					using (MemoryStream s = new MemoryStream (data)) {
						return soapF.Deserialize (s);
					}
				} else if (mime_type == ResXResourceWriter.BinSerializedObjectMimeType) {
					BinaryFormatter binF = new BinaryFormatter ();
					if (binder == null)
						binder = new CustomBinder (typeResolver);
					binF.Binder = binder;
					byte [] data = Convert.FromBase64String (dataString);
					using (MemoryStream s = new MemoryStream (data)) {
						return binF.Deserialize (s);
					}
				} else // invalid mime_type
					return null; 
			} catch (SerializationException ex) { 
				if (ex.Message.StartsWith ("Couldn't find assembly"))
					throw new ArgumentException (ex.Message);
				else
					throw ex;
			}
		}
Exemplo n.º 6
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // var serializedObject = property.serializedObject;

            var temp = (BindableObject)property.objectReferenceValue;

            if (temp == null)
            {
                return;
            }

            var tp   = temp.GetType();
            var prop = tp.GetProperty("target", BindingFlags.Public | BindingFlags.Instance);

            if (prop != null)
            {
                if (prop.GetValue(temp) == null)
                {
                    prop.SetValue(temp, temp.GetComponent(prop.PropertyType));
                }
            }

            var rect = EditorGUILayout.BeginHorizontal(GUILayout.Height(34));
            //selected
            bool isExpanded = property.isExpanded;

            var   rect1 = rect;
            float w     = rect.width;

            EditorGUI.HelpBox(rect, "", MessageType.None);
            rect1.height -= BindableObjectStyle.kExtraSpacing * 2;
            rect1.y      += BindableObjectStyle.kExtraSpacing;

            rect1.x            += BindableObjectStyle.kExtraSpacing;
            rect1.width         = 26f;
            isExpanded          = EditorGUI.Toggle(rect1, isExpanded);
            property.isExpanded = isExpanded;
            rect1.x             = rect1.xMax + BindableObjectStyle.kExtraSpacing;
            rect1.width         = w * .35f;


            CustomBinder customer = null;

            if (temp is CustomBinder)
            {
                customer = (CustomBinder)temp;
                EditorGUI.ObjectField(rect1, customer.target, typeof(Component), false); //显示绑定对象
            }
            else
            {
                EditorGUI.ObjectField(rect1, temp, typeof(BindableObject), false); //显示绑定对象
            }
            rect1.x     = rect1.xMax + BindableObjectStyle.kExtraSpacing;
            rect1.width = w * .35f;


            if (customer != null)
            {
                propertyName = BindalbeObjectUtilty.PopupComponentsProperty(rect1, customer.target, propertyName); //绑定属性
            }
            else
            {
                propertyName = BindalbeObjectUtilty.PopupComponentsProperty(rect1, temp, propertyName); //绑定属性
            }
            rect1.x     = rect1.xMax + BindableObjectStyle.kExtraSpacing;
            rect1.width = w * .2f - BindableObjectStyle.kExtraSpacing * 4;
            if (GUI.Button(rect1, "add"))
            {
                if (string.Equals(propertyName, BindableObjectStyle.FIRST_PROPERTY_NAME))
                {
                    Debug.LogWarningFormat("please choose a property to binding");
                    return;
                }
                Binding expression = new Binding();
                expression.propertyName = propertyName;
                temp.AddBinding(expression);
                property.isExpanded = true;
            }
            EditorGUILayout.Separator();
            EditorGUILayout.EndHorizontal();

            if (isExpanded)
            {
                //显示列表
                if (temSerialziedObject == null || temSerialziedObject.targetObject != temp)
                {
                    temSerialziedObject = new SerializedObject(temp);
                }
                {
                    var bindings = temSerialziedObject.FindProperty("bindings");
                    // serializedObject.targetObject
                    if (bindings != null && bindings.isArray)
                    {
                        selectedList.Clear();
                        temSerialziedObject.Update();

                        var len = bindings.arraySize;
                        SerializedProperty bindingProperty;
                        for (int i = 0; i < len; i++)
                        {
                            bindingProperty = bindings.GetArrayElementAtIndex(i);
                            EditorGUILayout.PropertyField(bindingProperty, true);
                            if (bindingProperty.isExpanded)
                            {
                                selectedList.Add(i);
                            }
                        }


                        rect    = EditorGUILayout.BeginHorizontal(GUILayout.Height(20));
                        rect.y += BindableObjectStyle.kExtraSpacing;

                        float width;
                        //删除数据
                        if (selectedList.Count > 0)
                        {
                            width      = 102;
                            rect.x     = rect.xMax - width;
                            rect.width = width;
                            if (GUI.Button(rect, "del property " + selectedList.Count))
                            {
                                selectedList.Sort((int a, int b) =>
                                {
                                    if (a < b)
                                    {
                                        return(1);
                                    }
                                    else if (a == b)
                                    {
                                        return(0);
                                    }
                                    else
                                    {
                                        return(-1);
                                    }
                                });

                                foreach (var i in selectedList)
                                {
                                    bindings.DeleteArrayElementAtIndex(i);
                                }
                            }
                            EditorGUILayout.Separator();
                        }
                        else
                        {
                            string DELETE_TIPS = BindableObjectStyle.PROPPERTY_CHOOSE_TIPS;
                            width      = DELETE_TIPS.Length * BindableObjectStyle.kExtraSpacing;
                            rect.x     = rect.xMax - width;
                            rect.width = width;
                            GUI.Box(rect, DELETE_TIPS);
                        }

                        temSerialziedObject.ApplyModifiedProperties();

                        EditorGUILayout.Separator();
                        EditorGUILayout.EndHorizontal();
                    }
                }
            }
            else
            {
                // GUILayout.Box("click checkbox  to see detail or delete!");
            }
        }
Exemplo n.º 7
0
        public override void OnInspectorGUI()
        {
            EditorGUILayout.Separator();
            var temp = target as BindableObject;
            var tp   = temp.GetType();
            var prop = tp.GetProperty("target", BindingFlags.Public | BindingFlags.Instance);

            if (prop != null)
            {
                if (prop.GetValue(target) == null)
                {
                    prop.SetValue(target, temp.GetComponent(prop.PropertyType));
                }
            }

            base.OnInspectorGUI();

            var   rect  = EditorGUILayout.BeginHorizontal(GUILayout.Height(34));
            var   rect1 = rect;
            float w     = rect.width;

            EditorGUI.HelpBox(rect, "", MessageType.None);

            rect1.height -= BindableObjectStyle.kExtraSpacing * 2;
            rect1.x      += BindableObjectStyle.kExtraSpacing;
            rect1.y      += BindableObjectStyle.kExtraSpacing;
            rect1.width   = w * .4f;

            //
            CustomBinder customer = null;

            if (temp is CustomBinder)
            {
                customer = (CustomBinder)temp;
                EditorGUI.ObjectField(rect1, customer.target, typeof(Component), false); //显示绑定对象
            }
            else
            {
                EditorGUI.ObjectField(rect1, temp, typeof(BindableObject), false); //显示绑定对象
            }
            rect1.x     = rect1.xMax + BindableObjectStyle.kExtraSpacing;
            rect1.width = w * .4f;

            if (customer != null)
            {
                propertyName = BindalbeObjectUtilty.PopupComponentsProperty(rect1, customer.target, propertyName); //绑定属性
            }
            else
            {
                propertyName = BindalbeObjectUtilty.PopupComponentsProperty(rect1, temp, propertyName); //绑定属性
            }
            rect1.x     = rect1.xMax + BindableObjectStyle.kExtraSpacing;
            rect1.width = w * .2f - BindableObjectStyle.kExtraSpacing * 4;
            if (GUI.Button(rect1, "add"))
            {
                if (string.Equals(propertyName, BindableObjectStyle.FIRST_PROPERTY_NAME))
                {
                    Debug.LogWarningFormat("please choose a property to binding");
                    return;
                }
                Binding expression = new Binding();
                expression.propertyName = propertyName;
                temp.AddBinding(expression);
            }
            EditorGUILayout.Separator();
            EditorGUILayout.EndHorizontal();

            //显示列表
            if (bindings.isArray)
            {
                selectedList.Clear();
                serializedObject.Update();

                var len = temp.GetBindings().Count;
                SerializedProperty bindingProperty;
                for (int i = 0; i < len; i++)
                {
                    bindingProperty = bindings.GetArrayElementAtIndex(i);
                    EditorGUILayout.PropertyField(bindingProperty, true);
                    if (bindingProperty.isExpanded)
                    {
                        selectedList.Add(i);
                    }
                }

                //删除数据
                if (selectedList.Count > 0)
                {
                    rect       = EditorGUILayout.BeginHorizontal(GUILayout.Height(20));
                    rect.x     = rect.xMax - 100;
                    rect.width = 100;
                    if (GUI.Button(rect, "del property " + selectedList.Count))
                    {
                        selectedList.Sort((int a, int b) =>
                        {
                            if (a < b)
                            {
                                return(1);
                            }
                            else if (a == b)
                            {
                                return(0);
                            }
                            else
                            {
                                return(-1);
                            }
                        });

                        foreach (var i in selectedList)
                        {
                            bindings.DeleteArrayElementAtIndex(i);
                        }
                    }
                    EditorGUILayout.Separator();
                    EditorGUILayout.EndHorizontal();
                }
                else
                {
                    GUILayout.Box(BindableObjectStyle.PROPPERTY_CHOOSE_TIPS);
                }
                serializedObject.ApplyModifiedProperties();
            }
        }
Exemplo n.º 8
0
        static void Main2()
        {
            // Get the type of MySimpleClass.
            Type myType = typeof(MySimpleClass);

            // Get an instance of MySimpleClass.
            MySimpleClass myInstance = new MySimpleClass();
            CustomBinder myCustomBinder = new CustomBinder();

            // Get the method information for the particular overload
            // being sought.
            MethodInfo myMethod = myType.GetMethod("MyMethod",
                BindingFlags.Public | BindingFlags.Instance,
                myCustomBinder, new Type[] {typeof(string),
                typeof(int)}, null);
            Console.WriteLine(myMethod.ToString());

            // Invoke the overload.
            myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod,
                myCustomBinder, myInstance,
                new Object[] { "Testing...", (int)32 });
        }
Exemplo n.º 9
0
 /// <summary>
 /// Used to avoid problems with serialization in one assembly and deserialization in another one
 /// </summary>
 /// <param name="senderAssembly">Assembly name of sender</param>
 /// <param name="targetAssembly">Assembly name of receiver</param>
 public static void SetAssemblyNames(string senderAssembly, string targetAssembly)
 {
     customBinder = new CustomBinder(senderAssembly, targetAssembly);
 }