Exemple #1
0
    public void Ctor()
    {
        var first = ReflectData
                    .GetProperties()
                    .FirstOrDefault();
        var reflect = new ReflectionProperty(first);

        assert.Equal(first.Name, reflect.Name);
        assert.Equal(first.PropertyType, reflect.ClrType);
        assert.Equal(true, reflect.CanRead);
        assert.Equal(true, reflect.CanWrite);

        var mods = reflect.GetterAccess;

        assert.Equal(true, mods.IsPublic);
        assert.Equal(false, mods.IsPrivate);
        assert.Equal(false, mods.IsInternal);
        assert.Equal(false, mods.IsVirtual);
        assert.Equal(false, mods.IsStatic);
        assert.Equal(true, mods.IsInstance);

        mods = reflect.SetterAccess;
        assert.Equal(true, mods.IsPublic);
        assert.Equal(false, mods.IsPrivate);
        assert.Equal(false, mods.IsInternal);
        assert.Equal(false, mods.IsVirtual);
        assert.Equal(false, mods.IsStatic);
        assert.Equal(true, mods.IsInstance);
    }
Exemple #2
0
 public void Ctor_ThrowsArgumentNull()
 {
     assert.Throws <ArgumentNullException>(() =>
     {
         _ = new ReflectionProperty(null);
     });
 }
Exemple #3
0
        public object GetPropertyFlashReflection([NotNull] object obj)
        {
            ReflectionType     type     = ReflectionCache.Instance.GetReflectionType(obj.GetType());
            ReflectionProperty property = type.Properties[UIntArrayPropertyName];

            return(property?.GetValue(obj));
        }
        public EnumControlView(ControlAttribute attribute, AbstractNode node, ReflectionProperty property)
        {
            var viewCont = new VisualElement();

            viewCont.AddToClassList("ControlField");

            if (!string.IsNullOrEmpty(attribute.label))
            {
                viewCont.Add(new Label(attribute.label)
                {
                    name = DefaultControlView.ControlLabelName
                });
            }

#if UNITY_EDITOR
            var enumField = new EnumField((Enum)property.GetValue(node))
            {
                name = DefaultControlView.ValueFieldName
            };
            enumField.OnValueChanged(e =>
            {
                node.owner.owner.RegisterCompleteObjectUndo("Enum Change");
                property.SetValue(node, e.newValue);
                node.Dirty(ModificationScope.Node);
            });
            viewCont.Add(enumField);
#endif
            Add(viewCont);
        }
        public void SetPropertyFlashReflection([NotNull] object obj)
        {
            ReflectionType     type     = ReflectionCache.Instance.GetReflectionType(obj.GetType());
            ReflectionProperty property = type.Properties[UIntArrayPropertyName];

            property?.SetValue(obj, ValueToSet);
        }
        public static void AreSame(ReflectionItem expected, ReflectionItem actual)
        {
            switch (expected.ItemType)
            {
            case ReflectionItemType.Property:
            {
                ReflectionProperty expectedProperty = (ReflectionProperty)expected;
                ReflectionProperty actualProperty   = (ReflectionProperty)actual;

                ReflectionAssert.AreSame(expectedProperty.UnderlyingGetMethod, actualProperty.UnderlyingGetMethod);
                ReflectionAssert.AreSame(expectedProperty.UnderlyingSetMethod, actualProperty.UnderlyingSetMethod);
                return;
            }

            case ReflectionItemType.Parameter:
            {
                ReflectionParameter expectedParameter = (ReflectionParameter)expected;
                ReflectionParameter actualParameter   = (ReflectionParameter)actual;

                ReflectionAssert.AreSame(expectedParameter.UnderlyingParameter, actualParameter.UnderlyingParameter);
                return;
            }

            default:
            {
                ReflectionMember expectedMember = (ReflectionMember)expected;
                ReflectionMember actualMember   = (ReflectionMember)actual;

                ReflectionAssert.AreSame(expectedMember.UnderlyingMember, actualMember.UnderlyingMember);
                return;
            }
            }
        }
Exemple #7
0
    public void FindAttribute()
    {
        var first = ReflectData
                    .GetProperties()
                    .FirstOrDefault();
        var reflect = new ReflectionProperty(first);

        var attr = reflect.FindAttribute <DecoratorAttribute>();

        assert.NotNull(attr);
    }
 private BaseField <T> AddControl <T>(AbstractNode node, BaseField <T> field, ReflectionProperty property)
 {
     field.value = (T)property.GetValue(node);
     field.RegisterValueChangedCallback(e =>
     {
         node.owner.owner.RegisterCompleteObjectUndo(typeof(T).Name + " Change");
         property.SetValue(node, e.newValue);
         node.Dirty(ModificationScope.Node);
     });
     return(field);
 }
Exemple #9
0
    public void FindAttributes()
    {
        var first = ReflectData
                    .GetProperties()
                    .FirstOrDefault();
        var reflect = new ReflectionProperty(first);

        var attrs = reflect.FindAttributes <DecoratorAttribute>();

        assert.NotNull(attrs);
        assert.Equal(1, attrs.Count);
    }
Exemple #10
0
    public void Private_GetValue()
    {
        var first = typeof(ReflectData).GetProperty(
            "Field4",
            BindingFlags.NonPublic | BindingFlags.Instance);

        assert.NotNull(first);

        var reflect = new ReflectionProperty(first);
        var data    = new ReflectData();

        assert.Equal(10, (int)reflect.GetValue(data));
    }
Exemple #11
0
    public void GetValue()
    {
        var first = ReflectData
                    .GetProperties()
                    .FirstOrDefault();
        var reflect = new ReflectionProperty(first);

        var data = new ReflectData()
        {
            Field2 = true
        };

        assert.Ok((bool)reflect.GetValue(data));
    }
Exemple #12
0
    public void GetStaticValue()
    {
        var first   = typeof(ReflectData).GetProperty("Field1");
        var reflect = new ReflectionProperty(first);

        try
        {
            ReflectData.Field1 = true;
            assert.Ok((bool)reflect.GetValue(null));
        }
        finally
        {
            ReflectData.Field1 = false;
        }
    }
Exemple #13
0
        static Program()
        {
            testUri  = new TestUri("SomeHost");
            @object  = testUri;
            @class   = testUri.GetType();
            property = @class.GetProperty(propertyName, bindingFlags);

            // Using FastMember - https://github.com/mgravell/fast-member
            accessor = TypeAccessor.Create(@class, allowNonPublicAccessors: allowNonPublicFieldAccess);

            flashReflectionType = ReflectionCache.Instance.GetReflectionType <TestUri>();
            flashProperty       = flashReflectionType.Properties[propertyName];

            if (flashProperty == null)
            {
                throw new NullReferenceException("Flash property is null");
            }
            expressionTreeGetter = CompiledTreePropertyInfo.GetValueGetter <TestUri>(property);
            expressionTreeSetter = CompiledTreePropertyInfo.GetValueSetter <TestUri>(property);
            var funcType = typeof(Func <TestUri, string>);

            getDelegate        = (Func <TestUri, string>)Delegate.CreateDelegate(funcType, property.GetMethod);
            getDelegateDynamic = Delegate.CreateDelegate(funcType, property.GetMethod);

            var actionType = typeof(Action <TestUri, string>);

            setDelegate        = (Action <TestUri, string>)Delegate.CreateDelegate(actionType, property.SetMethod);
            setDelegateDynamic = Delegate.CreateDelegate(actionType, property.SetMethod);

            var setterEmiter = Emit <Action <TestUri, string> >
                               .NewDynamicMethod("SetTestUriProperty")
                               .LoadArgument(0)
                               .LoadArgument(1)
                               .Call(property.SetMethod)
                               .Return();

            setter = setterEmiter.CreateDelegate();

            var getterEmiter = Emit <Func <TestUri, string> >
                               .NewDynamicMethod("GetTestUriProperty")
                               .LoadArgument(0)
                               .Call(property.GetMethod)
                               .Return();

            getter = getterEmiter.CreateDelegate();
        }
Exemple #14
0
    public void SetValue()
    {
        var first = ReflectData
                    .GetProperties()
                    .FirstOrDefault();
        var reflect = new ReflectionProperty(first);

        var data = new ReflectData()
        {
            Field2 = true
        };

        assert.True(data.Field2);

        reflect.SetValue(data, false);
        assert.False(data.Field2);
    }
        public ExposedReferenceControlView(AbstractNode node, ReflectionProperty property)
        {
            var nodeType = node.GetType();

            if (nodeType.IsGenericType)
            {
                var genericNodeType = nodeType.GetGenericTypeDefinition();
                if (typeof(ExposedReferenceNode <>) == genericNodeType)
                {
                    var  referenceType = nodeType.GenericTypeArguments[0];
                    Guid guid          = (Guid)property.GetValue(node);
#if UNITY_EDITOR
                    var  objectField      = new ObjectField();
                    var  propertyTable    = node.owner.owner as IReferenceTable;
                    var  graphUnityObject = node.owner.owner as UnityEngine.Object;
                    bool isValid;
                    objectField.objectType = referenceType;
                    objectField.value      = propertyTable != null?propertyTable.GetReferenceValue(guid, out isValid) : null;

                    objectField.OnValueChanged(e =>
                    {
                        if (propertyTable != null)
                        {
                            propertyTable.SetReferenceValue(guid, e.newValue);
                        }
                        else
                        {
                            objectField.value = null;
                        }
                        if (graphUnityObject != null)
                        {
                            EditorUtility.SetDirty(graphUnityObject);
                        }
                    });
                    Add(objectField);
#endif
                }
            }
        }
        public DefaultControlView(DefaultControlAttribute attribute, AbstractNode node, ReflectionProperty property)
        {
            var viewCont = new VisualElement();

            viewCont.AddToClassList("ControlField");

            if (!string.IsNullOrEmpty(attribute.label))
            {
                viewCont.Add(new Label(attribute.label)
                {
                    name = ControlLabelName
                });
            }

            var propertyType = property.PropertyType;

            if (propertyType == typeof(bool))
            {
                var toggle = new Toggle()
                {
                    name = ValueFieldName
                };
                toggle.value = (bool)property.GetValue(node);
                toggle.RegisterValueChangedCallback((e) =>
                {
                    node.owner.owner.RegisterCompleteObjectUndo("Boolean Change");
                    property.SetValue(node, e.newValue);
                    node.Dirty(ModificationScope.Node);
                });
                viewCont.Add(toggle);
            }
#if UNITY_EDITOR
            else if (propertyType == typeof(float))
            {
                viewCont.Add(AddControl(node, new FloatField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(double))
            {
                viewCont.Add(AddControl(node, new DoubleField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(int))
            {
                viewCont.Add(AddControl(node, new IntegerField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(Color))
            {
                viewCont.Add(AddControl(node, new ColorField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(Bounds))
            {
                viewCont.Add(AddControl(node, new BoundsField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(Rect))
            {
                viewCont.Add(AddControl(node, new RectField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(string))
            {
                viewCont.Add(AddControl(node, new TextField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(Gradient))
            {
                viewCont.Add(AddControl(node, new GradientField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(AnimationCurve))
            {
                viewCont.Add(AddControl(node, new CurveField()
                {
                    name = ValueFieldName
                }, property));
            }
            else if (propertyType == typeof(Vector2))
            {
                viewCont.Add(new MultiFloatSlotControlView(node, new[] { "x", "y" }, () => (Vector2)property.GetValue(node), v => property.SetValue(node, (Vector2)v))
                {
                    name = ValueFieldName
                });
            }
            else if (propertyType == typeof(Vector3))
            {
                viewCont.Add(new MultiFloatSlotControlView(node, new[] { "x", "y", "z" }, () => (Vector3)property.GetValue(node), v => property.SetValue(node, (Vector3)v))
                {
                    name = ValueFieldName
                });
            }
            else if (propertyType == typeof(Vector4))
            {
                viewCont.Add(new MultiFloatSlotControlView(node, new[] { "x", "y", "z", "w" }, () => (Vector4)property.GetValue(node), v => property.SetValue(node, v))
                {
                    name = ValueFieldName
                });
            }
            else if (propertyType == typeof(Quaternion))
            {
                viewCont.Add(new MultiFloatSlotControlView(node, new[] { "x", "y", "z" }, () => ((Quaternion)property.GetValue(node)).eulerAngles, v => property.SetValue(node, Quaternion.Euler(v)))
                {
                    name = ValueFieldName
                });
            }
#endif

            if (viewCont.childCount > 0)
            {
                Add(viewCont);
            }
        }
Exemple #17
0
 public abstract VisualElement InstantiateControl(AbstractNode node, ReflectionProperty property);
Exemple #18
0
 internal CommonProperty(CommonAttribute[] attributes, MonoCecilProperty monoCecilProperty, ReflectionProperty reflectionProperty)
 {
     Attributes         = attributes;
     MonoCecilProperty  = monoCecilProperty;
     ReflectionProperty = reflectionProperty;
 }