Exemplo n.º 1
0
        /// <summary>
        /// Converts the <see cref="WitnessCondition"/> from a JSON object.
        /// </summary>
        /// <param name="json">The <see cref="WitnessCondition"/> represented by a JSON object.</param>
        /// <returns>The converted <see cref="WitnessCondition"/>.</returns>
        public static WitnessCondition FromJson(JObject json)
        {
            WitnessConditionType type = Enum.Parse <WitnessConditionType>(json["type"].GetString());

            if (ReflectionCache <WitnessConditionType> .CreateInstance(type) is not WitnessCondition condition)
            {
                throw new FormatException("Invalid WitnessConditionType.");
            }
            condition.ParseJson(json);
            return(condition);
        }
Exemplo n.º 2
0
        private void InjectToBehaviour(MonoBehaviour behaviour)
        {
            Type componentType = behaviour.GetType();

            IMemberInjection[] injections = ReflectionCache.GetInjections(componentType);

            foreach (IMemberInjection injection in injections)
            {
                injection.Inject(behaviour, _context);
            }
        }
Exemplo n.º 3
0
 private void DecompressPayload()
 {
     if (_payload_compressed.Length == 0)
     {
         return;
     }
     byte[] decompressed = Flags.HasFlag(MessageFlags.Compressed)
         ? _payload_compressed.DecompressLz4(PayloadMaxSize)
         : _payload_compressed;
     Payload = ReflectionCache <MessageCommand> .CreateSerializable(Command, decompressed);
 }
        public void FindAttributeByType_AttributeExist_List()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject)).Attributes;

            // Act
            var property = reflection[typeof(CustomAttribute)];

            // Assert
            Assert.AreEqual(3, property.Count);
        }
Exemplo n.º 5
0
        public void FindMethodsByName_MethodNotExist_EmptyList()
        {
            // Arrange
            ReflectionClass rc = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            var methods = rc.Methods["TestMethodNotExist"];

            // Assert
            Assert.AreEqual(0, methods.Count);
        }
Exemplo n.º 6
0
        public void FindPropertyByName_PropertyNotExist_Null()
        {
            // Arrange
            ReflectionClass rc = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            var property = rc.Properties["Property12"];

            // Assert
            Assert.IsNull(property);
        }
        public void FullName_PropertyExist_ShouldContainParentNameAndNameSpace()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject)).Properties["Property1"];

            // Act
            var name = reflection.FullName;

            // Assert
            Assert.AreEqual("ReflectionUtilites.Test.ExampleObjects.ExampleObject.Property1", name);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 根据名称获取 <see cref="IRecordWrapper"/> 相对应的方法。
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static MethodInfo GetMethodByName(DbType type)
        {
            var methodName = GetDbTypeName(type);

            if (string.IsNullOrEmpty(methodName))
            {
                throw new ArgumentNullException(SR.GetString(SRKind.NoMatchRecordWrapperMethod, type.ToString()));
            }

            return(ReflectionCache.GetMember(methodName, typeof(string), methodName, (_, name) => typeof(IRecordWrapper).GetMethod(name, new[] { typeof(IDataReader), typeof(string) })));
        }
Exemplo n.º 9
0
        public IObservable <T> ObservableUnsortedScan <T>(T start = default(T), T end = default(T))
            where T : class
        {
            var cache      = ReflectionCache.For <T>();
            var table      = LocateTable <T>(cache);
            var startKey   = ExtractKey(cache, start);
            var endKey     = ExtractKey(cache, end);
            var observable = DataClient.Value.ObserveUnsortedRows(table, startKey, endKey);

            return(observable.Select(Inflate <T>));
        }
Exemplo n.º 10
0
        public void GetAttributes_AttributesExists_CountZero()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject)).Properties;

            // Act
            int count = reflection["Property2"].Attributes.Count;

            // Assert
            Assert.AreEqual(4, count);
        }
Exemplo n.º 11
0
        public void TestCreateInstance2()
        {
            TestItem defaultItem = new TestItem1();
            object   item2       = ReflectionCache <MyTestEnum> .CreateInstance(MyTestEnum.Item2, defaultItem);

            (item2 is TestItem2).Should().BeTrue();

            object item1 = ReflectionCache <MyTestEnum> .CreateInstance((MyTestEnum)0x02, new TestItem1());

            (item1 is TestItem1).Should().BeTrue();
        }
Exemplo n.º 12
0
        public void Static_members_are_not_returned_from_ReflectionCache()
        {
            //// Act
            var propertiesAndFields = ReflectionCache.GetPropertiesAndFields(typeof(TestClass));

            //// Assert
            Assert.Contains(propertiesAndFields, p => p.MemberInfo.Name == "field");
            Assert.DoesNotContain(propertiesAndFields, p => p.MemberInfo.Name == "staticField");
            Assert.Contains(propertiesAndFields, p => p.MemberInfo.Name == "property");
            Assert.DoesNotContain(propertiesAndFields, p => p.MemberInfo.Name == "staticProperty");
        }
Exemplo n.º 13
0
 /// <inheritdoc/>
 public Task Initialize()
 {
     if (IsInitialized)
     {
         return(Task.CompletedTask);
     }
     AvailableBehaviourTypes = ReflectionCache.FindTypes(type => type.IsSubclassOf(typeof(NPCBehaviourBase)) && !type.IsAbstract);
     IsInitialized           = true;
     Debug.Log($"{GetType().Name} scenario editor extension has been initialized.");
     return(Task.CompletedTask);
 }
Exemplo n.º 14
0
        public void IsAssignableFrom_ForAssignableInterface_True()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            var result = reflection.IsAssignableFrom(typeof(IExampleInterface));

            // Assert
            Assert.AreEqual(true, result);
        }
Exemplo n.º 15
0
        public void Name_PropertyExist_ShouldNotContainParentname()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject)).Properties["Property1"];

            // Act
            var name = reflection.Name;

            // Assert
            Assert.AreEqual("Property1", name);
        }
Exemplo n.º 16
0
        public PropertyInfo GetProperty(string name, BindingFlags flags)
        {
            var props = ReflectionCache.GetProperties(this, name, flags);

            if (props.Length > 1)
            {
                throw new AmbiguousMatchException("not unique: " + name);
            }

            return(props.Length == 0 ? null : props[0]);
        }
Exemplo n.º 17
0
        public void PropertyType_PropertyExist_PropertyType()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject)).Properties["Property4"];

            // Act
            Type propertyType = reflection.PropertyType;

            // Assert
            Assert.AreEqual(typeof(ExampleObject), propertyType);
        }
Exemplo n.º 18
0
        public void GetMethodList_MethodCount_WithoutPropertiesWithStandart()
        {
            // Arrange
            ReflectionClass rc = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            int count = rc.Methods.Count;

            // Assert
            Assert.AreEqual(7, count);
        }
Exemplo n.º 19
0
        public void Properties_PropertiesExist_Count()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            int count = reflection.Properties.Count;

            // Assert
            Assert.AreEqual(4, count);
        }
Exemplo n.º 20
0
        public void GetPropertyList_PropertyCount_ExactNumber()
        {
            // Arrange
            ReflectionClass rc = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            int count = rc.Properties.Count;

            // Assert
            Assert.AreEqual(4, count);
        }
Exemplo n.º 21
0
        public void Methods_MethodsExist_CountWithStandart()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            int count = reflection.Methods.Count;

            // Assert
            Assert.AreEqual(7, count);
        }
Exemplo n.º 22
0
        private static string RegexOutput(object schema, object profile, string output)
        {
            var mappers = cache.TryGetValue(output, () =>
            {
                var list        = new List <AccessorMap>();
                var schemaType  = schema != null ? schema.GetType() : null;
                var profileType = profile != null ? profile.GetType() : null;
                var regex       = new Regex(@"\{(\w+)\}");
                var matches     = regex.Matches(output);
                foreach (Match match in matches)
                {
                    var group = match.Groups[0];
                    var name  = group.Value.Substring(1, group.Value.Length - 2);

                    if (schemaType != null)
                    {
                        var property = schemaType.GetProperty(name);
                        if (property != null)
                        {
                            list.Add(new AccessorMap
                            {
                                GroupName  = group.Value,
                                Accessor   = ReflectionCache.GetAccessor(property),
                                ObjectType = ObjectType.Schema
                            });
                        }
                    }

                    if (profileType != null)
                    {
                        var property = profileType.GetProperty(name);
                        if (property != null)
                        {
                            list.Add(new AccessorMap
                            {
                                GroupName  = group.Value,
                                Accessor   = ReflectionCache.GetAccessor(property),
                                ObjectType = ObjectType.Profile
                            });
                        }
                    }
                }

                return(list);
            });

            foreach (var p in mappers)
            {
                var value = GetPropertyValue(schema, profile, p);
                output = output.Replace(p.GroupName, value);
            }

            return(output);
        }
Exemplo n.º 23
0
        public void BaseType_AnyObject_RealObjectType()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            var type = reflection.BaseType;

            // Assert
            Assert.AreEqual(typeof(ExampleObject), type);
        }
        public void GetAttributeList_AttributeCount_ExactNumber()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject)).Attributes;

            // Act
            int count = reflection.Count;

            // Assert
            Assert.AreEqual(4, count);
        }
Exemplo n.º 25
0
        public void FullName_ForClass_ShouldContainNameSpace()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            var name = reflection.FullName;

            // Assert
            Assert.AreEqual("ReflectionUtilites.Test.ExampleObjects.ExampleObject", name);
        }
        public void FindAttributeByType_AttributeNotExist_EmptyList()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject)).Attributes;

            // Act
            var property = reflection[typeof(TestMethodAttribute)];

            // Assert
            Assert.AreEqual(0, property.Count);
        }
Exemplo n.º 27
0
        public void Name_ForClass_ShouldNotContainNamespace()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject));

            // Act
            var name = reflection.Name;

            // Assert
            Assert.AreEqual("ExampleObject", name);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Deserializes an <see cref="WitnessCondition"/> object from a <see cref="MemoryReader"/>.
        /// </summary>
        /// <param name="reader">The <see cref="MemoryReader"/> for reading data.</param>
        /// <param name="maxNestDepth">The maximum nesting depth allowed during deserialization.</param>
        /// <returns>The deserialized <see cref="WitnessCondition"/>.</returns>
        public static WitnessCondition DeserializeFrom(ref MemoryReader reader, int maxNestDepth)
        {
            WitnessConditionType type = (WitnessConditionType)reader.ReadByte();

            if (ReflectionCache <WitnessConditionType> .CreateInstance(type) is not WitnessCondition condition)
            {
                throw new FormatException();
            }
            condition.DeserializeWithoutType(ref reader, maxNestDepth);
            return(condition);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Deserializes an <see cref="TransactionAttribute"/> object from a <see cref="BinaryReader"/>.
        /// </summary>
        /// <param name="reader">The <see cref="BinaryReader"/> for reading data.</param>
        /// <returns>The deserialized attribute.</returns>
        public static TransactionAttribute DeserializeFrom(BinaryReader reader)
        {
            TransactionAttributeType type = (TransactionAttributeType)reader.ReadByte();

            if (ReflectionCache <TransactionAttributeType> .CreateInstance(type) is not TransactionAttribute attribute)
            {
                throw new FormatException();
            }
            attribute.DeserializeWithoutType(reader);
            return(attribute);
        }
Exemplo n.º 30
0
        public void Parameters_ParametersExist_ListOfParameters()
        {
            // Arrange
            var reflection = ReflectionCache.GetReflection(typeof(ExampleObject)).Methods["TestMethod"][1];

            // Act
            var parameters = reflection.Parameters;

            // Assert
            Assert.AreEqual(1, parameters.Length);
        }
Exemplo n.º 31
0
        /// <summary>
        /// Animates the current control with some values.
        /// </summary>
        /// <param name="c">The control to animate.</param>
        /// <param name="properties">The anonymous object with name / value pairs.</param>
        /// <param name="duration">The duration of the animation.</param>
        /// <param name="easing">The Easing object to use.</param>
        /// <param name="complete">The callback method to invoke when the animation is finished.</param>
        public static void Animate(this Control c, object properties, int duration, Easing easing, Action complete)
        {
            var t = new Timer();
            t.Interval = 30;
            var frame = 0;
            var maxframes = (int)Math.Ceiling(duration / 30.0);
            var reflection = properties.GetType();
            var target = c.GetType();
            var props = reflection.GetProperties();
            var values = new ReflectionCache[props.Length];

            for (int i = 0; i < props.Length; i++)
            {
                values[i] = new ReflectionCache(target.GetProperty(props[i].Name));
                values[i].SetStart(values[i].Info.GetValue(c, null));
                values[i].SetEnd(props[i].GetValue(properties, null));
            }

            t.Tick += (s, e) =>
            {
                frame++;

                for (int i = 0; i < values.Length; i++)
                {
                    values[i].Execute(c, easing, frame, maxframes);
                }

                if (frame == maxframes)
                {
                    t.Stop();

                    if (complete != null)
                        complete();
                }
            };

            t.Start();
        }
Exemplo n.º 32
0
 public BuiltinFunctionKey(Type declaringType, ReflectionCache.MethodBaseCache cache, FunctionType funcType) {
     Cache = cache;
     FunctionType = funcType;
     DeclaringType = declaringType;
 }
Exemplo n.º 33
0
            public ReflectionCache(PropertyInfo info)
            {
                Info = info;

                if(info == null)
                    throw new ArgumentException("Invalid property to animate. The given properties have to match a property of the control.");

                var subprops = info.PropertyType.GetProperties().Where(m => m.CanRead && m.CanWrite).ToArray();

                if (subprops.Length > 0)
                {
                    SubList = new ReflectionCache[subprops.Length];

                    for (int i = 0; i < subprops.Length; i++)
                        SubList[i] = new ReflectionCache(subprops[i]);
                }
            }