Exemple #1
0
        private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            Attribute[] customAttributes = ReflectionUtils.GetAttributes(type, null, true);

            foreach (Attribute attribute in customAttributes)
            {
                Type attributeType = attribute.GetType();

                // only test on attribute type name
                // attribute assembly could change because of type forwarding, etc
                if (string.Equals(attributeType.FullName, "System.ComponentModel.DataAnnotations.MetadataTypeAttribute", StringComparison.Ordinal))
                {
                    const string metadataClassTypeName = "MetadataClassType";

                    if (_metadataTypeAttributeReflectionObject == null)
                    {
                        _metadataTypeAttributeReflectionObject = ReflectionObject.Create(attributeType, metadataClassTypeName);
                    }

                    return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
                }
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            ReflectionObject reflectionObject = ReflectionObjectPerType.Get(value.GetType());

            DefaultContractResolver?resolver =
                serializer.ContractResolver as DefaultContractResolver;

            writer.WriteStartObject();
            writer.WritePropertyName(
                (resolver != null) ? resolver.GetResolvedPropertyName(KeyName) : KeyName
                );
            serializer.Serialize(
                writer,
                reflectionObject.GetValue(value, KeyName),
                reflectionObject.GetType(KeyName)
                );
            writer.WritePropertyName(
                (resolver != null) ? resolver.GetResolvedPropertyName(ValueName) : ValueName
                );
            serializer.Serialize(
                writer,
                reflectionObject.GetValue(value, ValueName),
                reflectionObject.GetType(ValueName)
                );
            writer.WriteEndObject();
        }
Exemple #3
0
 private void EnsureReflectionObject(Type t)
 {
     if (_reflectionObject == null)
     {
         _reflectionObject = ReflectionObject.Create(t, t.GetConstructor(new[] { typeof(byte[]) }), BinaryToArrayName);
     }
 }
        public override void Do(EventModel model, ref List <MessageModel> replyMessages)
        {
            var message     = model.message.text.Split(' ');
            var actionReply = ReflectionObject.GenericReflectionWithParm <Reply>($"{message[0].FirstCharToUpper()}Reply", new object[] { _context });

            actionReply.Do(message, ref replyMessages);
        }
Exemple #5
0
        private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            Type metadataTypeAttributeType = GetMetadataTypeAttributeType();

            if (metadataTypeAttributeType == null)
            {
                return(null);
            }

            Attribute attribute = ReflectionUtils.GetAttributes(type, metadataTypeAttributeType, true).SingleOrDefault();

            if (attribute == null)
            {
                return(null);
            }

            const string metadataClassTypeName = "MetadataClassType";

            if (_metadataTypeAttributeReflectionObject == null)
            {
                _metadataTypeAttributeReflectionObject = ReflectionObject.Create(metadataTypeAttributeType, metadataClassTypeName);
            }

            return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
        }
 private static void EnsureReflectionObject(Type objectType)
 {
     if (_reflectionObject == null)
     {
         _reflectionObject = ReflectionObject.Create(objectType, KeyPropertyName, ValuePropertyName);
     }
 }
Exemple #7
0
 private void EnsureReflectionObject(Type t)
 {
     if (this._reflectionObject == null)
     {
         this._reflectionObject = ReflectionObject.Create(t, t.GetConstructor(new Type[] { typeof(byte[]) }), new string[] { "ToArray" });
     }
 }
 /// <summary>
 /// The get transitions.
 /// </summary>
 /// <param name="record">
 /// The record. 
 /// </param>
 /// <param name="rootActivity">
 /// The root activity. 
 /// </param>
 /// <returns>
 /// A collection of possible transitions 
 /// </returns>
 public static IList<Transition> GetTransitions(this StateMachineStateRecord record, Activity rootActivity)
 {
     var activity = WorkflowInspectionServices.Resolve(rootActivity, record.Activity.Id);
     dynamic internalState = new ReflectionObject(activity);
     dynamic transitions = new ReflectionObject(internalState.Transitions);
     return ((ReflectionObject)transitions).Inner as IList<Transition>;
 }
        public override object ReadJson(JsonReader reader, Type objectType, [Nullable(2)] object existingValue, JsonSerializer serializer)
        {
            EntityKeyMemberConverter.EnsureReflectionObject(objectType);

            object obj = EntityKeyMemberConverter._reflectionObject.Creator(new object[0]);

            EntityKeyMemberConverter.ReadAndAssertProperty(reader, "Key");
            reader.ReadAndAssert();
            ReflectionObject reflectionObject = EntityKeyMemberConverter._reflectionObject;
            object           target           = obj;
            string           member           = "Key";
            object           value            = reader.Value;

            reflectionObject.SetValue(target, member, (value != null) ? value.ToString() : null);
            EntityKeyMemberConverter.ReadAndAssertProperty(reader, "Type");
            reader.ReadAndAssert();
            object value2 = reader.Value;
            Type   type   = Type.GetType((value2 != null) ? value2.ToString() : null);

            EntityKeyMemberConverter.ReadAndAssertProperty(reader, "Value");
            reader.ReadAndAssert();
            EntityKeyMemberConverter._reflectionObject.SetValue(obj, "Value", serializer.Deserialize(reader, type));
            reader.ReadAndAssert();
            return(obj);
        }
Exemple #10
0
 private static void EnsureReflectionObject(Type t)
 {
     if (BinaryConverter._reflectionObject == null)
     {
         BinaryConverter._reflectionObject = ReflectionObject.Create(t, t.GetConstructor(new Type[] { typeof(byte[]) }), new string[] { "ToArray" });
     }
 }
        private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            object[] customAttributes;
#if !PORTABLE
            customAttributes = type.GetCustomAttributes(false);
#else
            customAttributes = type.GetTypeInfo().GetCustomAttributes(false).Cast <object>().ToArray();
#endif

            foreach (var attribute in customAttributes)
            {
                Type attributeType = attribute.GetType();

                if (string.Equals(attributeType.Name, "MetadataTypeAttribute", StringComparison.Ordinal) &&
                    attributeType.Assembly().FullName.StartsWith("System.ComponentModel.DataAnnotations", StringComparison.Ordinal))
                {
                    const string metadataClassTypeName = "MetadataClassType";

                    if (_metadataTypeAttributeReflectionObject == null)
                    {
                        _metadataTypeAttributeReflectionObject = ReflectionObject.Create(attributeType, metadataClassTypeName);
                    }

                    return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
                }
            }

            return(null);
        }
Exemple #12
0
 private static void EnsureReflectionObject(Type objectType)
 {
     if (EntityKeyMemberConverter._reflectionObject == null)
     {
         EntityKeyMemberConverter._reflectionObject = ReflectionObject.Create(objectType, new string[] { "Key", "Value" });
     }
 }
Exemple #13
0
        private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            object[] customAttributes;
#if !PORTABLE
            customAttributes = type.GetCustomAttributes(false);
#else
            customAttributes = type.GetTypeInfo().GetCustomAttributes(false).Cast <object>().ToArray();
#endif

            foreach (var attribute in customAttributes)
            {
                Type attributeType = attribute.GetType();

                // only test on attribute type name
                // attribute assembly could change because of type forwarding, etc
                if (string.Equals(attributeType.FullName, "System.ComponentModel.DataAnnotations.MetadataTypeAttribute", StringComparison.Ordinal))
                {
                    const string metadataClassTypeName = "MetadataClassType";

                    if (_metadataTypeAttributeReflectionObject == null)
                    {
                        _metadataTypeAttributeReflectionObject = ReflectionObject.Create(attributeType, metadataClassTypeName);
                    }

                    return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
                }
            }

            return(null);
        }
 private static void EnsureReflectionObject(Type objectType)
 {
     if (_reflectionObject == null)
     {
         string[] memberNames = new string[] { "Key", "Value" };
         _reflectionObject = ReflectionObject.Create(objectType, memberNames);
     }
 }
        private static ReflectionObject InitializeReflectionObject(Type t)
        {
            Type[] genericArguments = t.GetGenericArguments();
            Type   item             = ((IList <Type>)genericArguments)[0];
            Type   type             = ((IList <Type>)genericArguments)[1];

            return(ReflectionObject.Create(t, t.GetConstructor(new Type[] { item, type }), new string[] { "Key", "Value" }));
        }
        private static ReflectionObject InitializeReflectionObject(Type t)
        {
            IList <Type> genericArguments = t.GetGenericArguments();
            Type         keyType          = genericArguments[0];
            Type         valueType        = genericArguments[1];

            return(ReflectionObject.Create(t, t.GetConstructor(new[] { keyType, valueType }), KeyName, ValueName));
        }
Exemple #17
0
 private void EnsureReflectionObject(Type t)
 {
     if (this._reflectionObject == null)
     {
         Type[]   types       = new Type[] { typeof(byte[]) };
         string[] memberNames = new string[] { "ToArray" };
         this._reflectionObject = ReflectionObject.Create(t, t.GetConstructor(types), memberNames);
     }
 }
Exemple #18
0
        public static void Quit()
        {
            Engine.TickWorkQueue.Enqueue(() =>
            {
                dynamic game = new ReflectionObject(Reflection.GetTypePropertyValue <dynamic>("ClassicUO.Client", "Game", null));

                game.Exit();
            });
        }
        private static ReflectionObject InitializeReflectionObject(Type t)
        {
            Type[] genericArguments = t.GetGenericArguments();
            Type   type             = genericArguments[0];
            Type   type2            = genericArguments[1];

            Type[]   types       = new Type[] { type, type2 };
            string[] memberNames = new string[] { "Key", "Value" };
            return(ReflectionObject.Create(t, t.GetConstructor(types), memberNames));
        }
Exemple #20
0
 /// <summary>
 /// Serves as a hash function for a particular type.
 /// </summary>
 /// <returns>
 /// A hash code for the current <see cref="T:System.Object"/>.
 /// </returns>
 /// <filterpriority>2</filterpriority>
 public override int GetHashCode()
 {
     unchecked
     {
         int result = Level;
         result = (result * 397) ^ (ReflectionObject != null ? ReflectionObject.GetHashCode() : 0);
         result = (result * 397) ^ (Node != null ? Node.GetHashCode() : 0);
         return(result);
     }
 }
 private void EnsureReflectionObject(Type t)
 {
     if (this._reflectionObject != null)
     {
         return;
     }
     this._reflectionObject = ReflectionObject.Create(t, (MethodBase)t.GetConstructor(new Type[1]
     {
         typeof(byte[])
     }), "ToArray");
 }
Exemple #22
0
        private static ReflectionObject InitializeReflectionObject(Type t)
        {
            Type[] genericArguments = t.GetGenericArguments();
            Type   type1            = ((IList <Type>)genericArguments)[0];
            Type   type2            = ((IList <Type>)genericArguments)[1];

            return(ReflectionObject.Create(t, (MethodBase)t.GetConstructor(new Type[2] {
                type1,
                type2
            }), "Key", "Value"));
        }
        private void EnsureReflectionObject(Type t)
        {
            if (_reflectionObject == null)
            {
#if NETSTANDARD1_3 || NETSTANDARD1_4 || NETSTANDARD1_5
                _reflectionObject = ReflectionObject.Create(t, System.Reflection.TypeExtensions.GetConstructor(t, new[] { typeof(byte[]) }), BinaryToArrayName);
#else
                _reflectionObject = ReflectionObject.Create(t, t.GetConstructor(new[] { typeof(byte[]) }), BinaryToArrayName);
#endif
            }
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            ReflectionObject        reflectionObject = KeyValuePairConverter.ReflectionObjectPerType.Get(value.GetType());
            DefaultContractResolver contractResolver = serializer.ContractResolver as DefaultContractResolver;

            writer.WriteStartObject();
            writer.WritePropertyName((contractResolver != null ? contractResolver.GetResolvedPropertyName("Key") : "Key"));
            serializer.Serialize(writer, reflectionObject.GetValue(value, "Key"), reflectionObject.GetType("Key"));
            writer.WritePropertyName((contractResolver != null ? contractResolver.GetResolvedPropertyName("Value") : "Value"));
            serializer.Serialize(writer, reflectionObject.GetValue(value, "Value"), reflectionObject.GetType("Value"));
            writer.WriteEndObject();
        }
Exemple #25
0
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializerWriter internalWriter)
        {
            ReflectionObject reflectionObject = ReflectionObjectPerType.Get(value.GetType());

            DefaultContractResolver resolver = internalWriter.Serializer.ContractResolver as DefaultContractResolver;

            writer.WriteStartObject();
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyName) : KeyName);
            internalWriter.Serialize(writer, reflectionObject.GetValue(value, KeyName), reflectionObject.GetType(KeyName));
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValueName) : ValueName);
            internalWriter.Serialize(writer, reflectionObject.GetValue(value, ValueName), reflectionObject.GetType(ValueName));
            writer.WriteEndObject();
        }
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        public override object?ReadJson(JsonReader reader, Type objectType, object?existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                if (!ReflectionUtils.IsNullableType(objectType))
                {
                    throw JsonSerializationException.Create(reader, "Cannot convert null value to KeyValuePair.");
                }

                return(null);
            }

            object?key   = null;
            object?value = null;

            reader.ReadAndAssert();

            Type t = ReflectionUtils.IsNullableType(objectType)
                ? Nullable.GetUnderlyingType(objectType)
                : objectType;

            ReflectionObject reflectionObject = ReflectionObjectPerType.Get(t);

            Serialization.JsonContract keyContract   = serializer.ContractResolver.ResolveContract(reflectionObject.GetType(KeyName));
            Serialization.JsonContract valueContract = serializer.ContractResolver.ResolveContract(reflectionObject.GetType(ValueName));

            while (reader.TokenType == JsonToken.PropertyName)
            {
                string propertyName = reader.Value !.ToString();
                if (string.Equals(propertyName, KeyName, StringComparison.OrdinalIgnoreCase))
                {
                    reader.ReadForTypeAndAssert(keyContract, false);

                    key = serializer.Deserialize(reader, keyContract.UnderlyingType);
                }
                else if (string.Equals(propertyName, ValueName, StringComparison.OrdinalIgnoreCase))
                {
                    reader.ReadForTypeAndAssert(valueContract, false);

                    value = serializer.Deserialize(reader, valueContract.UnderlyingType);
                }
                else
                {
                    reader.Skip();
                }

                reader.ReadAndAssert();
            }

            return(reflectionObject.Creator !(key, value));
        }
        public void Invoke_ShouldCallInvokeOnBothMocks_AndReturnTrue()
        {
            // Arrange
            MockMockMethodWithParam <string> mockMockMethodWithParam = new MockMockMethodWithParam <string> .Builder().Invoke().Build();

            MockMockMethodWithResponse <bool> mockMockMethodWithResponse = new MockMockMethodWithResponse <bool> .Builder().Invoke(true).Build();

            MockMethodWithParamAndResponse <string, bool> subject = new ReflectionObject <MockMethodWithParamAndResponse <string, bool> >("methodName", mockMockMethodWithParam, mockMockMethodWithResponse).Object();

            // Act
            bool actual = subject.Invoke("expected");

            // Assert
            actual.Should().BeTrue();
        }
        public void Invoke_ShouldFlagInvoked_AndNotExcept()
        {
            // Arrange
            MockMockMethodWithParam <string> mockMockMethodWithParam = new MockMockMethodWithParam <string> .Builder().Invoke().Build();

            MockMockMethodWithResponse <bool> mockMockMethodWithResponse = new MockMockMethodWithResponse <bool> .Builder().Invoke(true).Build();

            MockMethodWithParamAndResponse <string, bool> subject = new ReflectionObject <MockMethodWithParamAndResponse <string, bool> >("methodName", mockMockMethodWithParam, mockMockMethodWithResponse).Object();

            // Act
            bool actual = subject.Invoke("expected");

            // Assert
            subject.AssertInvoked();
        }
        public async Task InvokeTask_ShouldCallInvokeOnBothMocks_AndReturnFalse()
        {
            // Arrange
            MockMockMethodWithParam <string> mockMockMethodWithParam = new MockMockMethodWithParam <string> .Builder().Invoke().Build();

            MockMockMethodWithResponse <bool> mockMockMethodWithResponse = new MockMockMethodWithResponse <bool> .Builder().Invoke(false).Build();

            MockMethodWithParamAndResponse <string, bool> subject = new ReflectionObject <MockMethodWithParamAndResponse <string, bool> >("methodName", mockMockMethodWithParam, mockMockMethodWithResponse).Object();

            // Act
            bool actual = await subject.InvokeTask("expected");

            // Assert
            actual.Should().BeFalse();
        }
        public async Task Post([FromBody] ReceiveMessageApiModel content)
        {
            List <MessageModel> replyMessages = new List <MessageModel>();
            EventModel          receiveEvent  = content.events?.FirstOrDefault();
            string replyToken = receiveEvent.replyToken;

            var lineEvent = ReflectionObject.GenericReflectionWithParm <LineEvent>($"{receiveEvent.type.FirstCharToUpper()}LineEvent", new object[] { _context });

            if (lineEvent != null)
            {
                lineEvent.Do(receiveEvent, ref replyMessages);
            }

            await ResponseLine(replyToken, replyMessages);
        }
Exemple #31
0
        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                if (!ReflectionUtils.IsNullableType(objectType))
                {
                    throw JsonSerializationException.Create(reader, "Cannot convert null value to KeyValuePair.");
                }
                return((object)null);
            }

            object obj1 = (object)null;
            object obj2 = (object)null;

            reader.ReadAndAssert();
            Type             key = ReflectionUtils.IsNullableType(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType;
            ReflectionObject reflectionObject = KeyValuePairConverter.ReflectionObjectPerType.Get(key);

            while (reader.TokenType == JsonToken.PropertyName)
            {
                string a = reader.Value.ToString();
                if (string.Equals(a, "Key", StringComparison.OrdinalIgnoreCase))
                {
                    reader.ReadAndAssert();
                    obj1 = serializer.Deserialize(reader, reflectionObject.GetType("Key"));
                }
                else if (string.Equals(a, "Value", StringComparison.OrdinalIgnoreCase))
                {
                    reader.ReadAndAssert();
                    obj2 = serializer.Deserialize(reader, reflectionObject.GetType("Value"));
                }
                else
                {
                    reader.Skip();
                }

                reader.ReadAndAssert();
            }

            return(reflectionObject.Creator(new object[2] {
                obj1,
                obj2
            }));
        }
 private static void EnsureReflectionObject(Type objectType)
 {
     if (_reflectionObject == null)
         _reflectionObject = ReflectionObject.Create(objectType, KeyPropertyName, ValuePropertyName);
 }
 /// <summary>
 /// The get activity.
 /// </summary>
 /// <param name="activityInfo">
 /// The activity info. 
 /// </param>
 /// <returns>
 /// The System.Activities.Activity. 
 /// </returns>
 public static Activity GetActivity(this ActivityInfo activityInfo)
 {
     dynamic activityObj = new ReflectionObject(activityInfo);
     return activityObj.Activity;
 }
 /// <summary>
 /// The get transitions.
 /// </summary>
 /// <param name="record">
 /// The record. 
 /// </param>
 /// <returns>
 /// A collection of possible transitions 
 /// </returns>
 public static IList<Transition> GetTransitions(this StateMachineStateRecord record)
 {
     dynamic internalState = new ReflectionObject(record.Activity.GetActivity());
     dynamic transitions = new ReflectionObject(internalState.Transitions);
     return ((ReflectionObject)transitions).Inner as IList<Transition>;
 }
Exemple #35
0
 public static ReflectionObject ParseString(String s)
 {
     if (s == null || s == "") return null;
     ReflectionObject o = new ReflectionObject();
     if (s.EndsWith(")")) o.type = Types.Methods;
     else o.type = Types.Fields;
     int i = s.LastIndexOf('.');
     if (i == -1)
     {
         o.nmSpace = "";
         if (o.type == Types.Fields)
             o.name = s;
         else
             o.name = s.Substring(0, s.IndexOf('('));
     }
     else
     {
         o.nmSpace = s.Substring(0, i);
         i++;
         if (o.type == Types.Fields)
         {
             o.name = s.Substring(i, s.Length - i);
         }
         else
         {
             o.name = s.Substring(i, s.IndexOf('(') - i);
         }
     }
     return o;
 }
Exemple #36
0
 private void EnsureReflectionObject(Type t)
 {
     if (_reflectionObject == null)
         _reflectionObject = ReflectionObject.Create(t, t.GetConstructor(new[] { typeof(byte[]) }), BinaryToArrayName);
 }