コード例 #1
0
        public ChaosInvocationResp ProcessInvocation(ChaosInvocation chaosInvocation)
        {
            try
            {
                var realImplementObject = _serviceResolver.GetService(chaosInvocation.InterfaceTypeFullName);
                var realImplementType   = realImplementObject.GetType();
                var realImplementInfo   = ReflectionClass.Reflection(realImplementType);

                var mi = FindMethod(realImplementInfo, chaosInvocation);

                var requestParameters = chaosInvocation.Parameters;
                var args = requestParameters.DeserializeToArguments(_serializer, _typeFinder)
                           .ToArray();

                var returnValue = mi.Func(realImplementObject, args);

                var invocationReply = ToChaosInvocationResp(chaosInvocation, returnValue);
                return(invocationReply);
            }
            catch (Exception ex)
            {
                var invocationReply = ToChaosInvocationResp(chaosInvocation, (object)null);
                invocationReply.Exception = SerializeException.CreateFromException(ex);
                return(invocationReply);
            }
        }
コード例 #2
0
        public static T ConvertToObject <T>(this IDictionary <string, object> dict)
            where T : class, new()
        {
            var obj   = new T();
            var clazz = ReflectionClass.Reflection(typeof(T));

            foreach (var key in dict.Keys)
            {
                var propName = key.Substring(0, 1).ToUpper();
                if (key.Length > 1)
                {
                    propName += key.Substring(1);
                }
                var value = dict[key];
                if (!clazz.Properties.TryGetValue(propName, out var prop))
                {
                    continue;
                }
                if (value == null)
                {
                    continue;
                }
                var propType = prop.PropertyType;
                if (propType != value.GetType())
                {
                    var propValue = value.ChangeType(propType);
                    prop.Setter(obj, propValue);
                }
                else
                {
                    prop.Setter(obj, value);
                }
            }
            return(obj);
        }
コード例 #3
0
        public static T ToSummary <T>(this IEnumerable <T> list)
            where T : class, new()
        {
            var summary = new T();
            var clazzz  = ReflectionClass.Reflection(typeof(T));
            var first   = true;

            foreach (var item in list)
            {
                foreach (var prop in clazzz.Properties.Values)
                {
                    if (!prop.PropertyType.IsValueType)
                    {
                        continue;
                    }

                    var propValue = prop.Getter(item);
                    if (first)
                    {
                        prop.Setter(summary, propValue);
                        if (prop.PropertyType == typeof(DateTime))
                        {
                            prop.Setter(summary, DateTime.Now);
                        }
                    }
                    else
                    {
                        var sumValue = prop.Getter(summary);
                        if (prop.PropertyType == typeof(decimal))
                        {
                            sumValue = (decimal)sumValue + (decimal)propValue;
                        }
                        else if (prop.PropertyType == typeof(int))
                        {
                            sumValue = (int)sumValue + (int)propValue;
                        }
                        else if (prop.PropertyType == typeof(long))
                        {
                            sumValue = (long)sumValue + (long)propValue;
                        }
                        prop.Setter(summary, sumValue);
                    }
                }
                first = false;
            }
            return(summary);
        }
コード例 #4
0
ファイル: StringExtension.cs プロジェクト: flashlin/Samples
        public static string GetDisplayTitle(this object obj)
        {
            var sb        = new StringBuilder();
            var delimiter = new StringBuilder();
            var clazz     = ReflectionClass.Reflection(obj.GetType());
            var first     = true;

            foreach (var prop in clazz.Properties.Values)
            {
                var propInfo    = (PropertyInfo)prop.Info;
                var decimalAttr = propInfo.GetCustomAttribute <DecimalStringAttribute>(false);
                if (decimalAttr != null)
                {
                    var value = prop.Getter(obj);
                    if (!first)
                    {
                        sb.Append(" ");
                        delimiter.Append(" ");
                    }

                    sb.Append(prop.Name.ToFixLenString(decimalAttr.MaxLength, AlignType.Right));
                    delimiter.Append(new String('-', decimalAttr.MaxLength));
                    first = false;
                    continue;
                }

                var displayAttr = propInfo.GetCustomAttribute <DisplayStringAttribute>(false);
                if (displayAttr != null)
                {
                    if (!first)
                    {
                        sb.Append(" ");
                        delimiter.Append(" ");
                    }
                    var value = prop.Getter(obj);

                    sb.Append(displayAttr.ToDisplayString(prop.Name));

                    delimiter.Append(new String('-', displayAttr.MaxLength));
                    first = false;
                    continue;
                }
            }
            return(sb.ToString() + "\r\n" + delimiter.ToString());
        }
コード例 #5
0
        public static List <SqlDataRecord> ToSqlVariableTvp(this object obj)
        {
            var dataTable = new List <SqlDataRecord>();
            var clazz     = ReflectionClass.Reflection(obj.GetType());

            foreach (var prop in clazz.Properties)
            {
                var dr = new SqlDataRecord(
                    new SqlMetaData("Name", SqlDbType.VarChar, 255),
                    new SqlMetaData("DataType", SqlDbType.VarChar, 255),
                    new SqlMetaData("DataValue", SqlDbType.Variant)
                    );
                dr.SetString(0, "@" + prop.Key);
                dr.SetString(1, GetSqlDbType((PropertyInfo)prop.Value.Info));
                dr.SetValue(2, prop.Value.Getter(obj));
                dataTable.Add(dr);
            }
            return(dataTable);
        }
コード例 #6
0
    private static IEnumerable <ColumnInfo> GetTableColumnsInfo(Type entityType)
    {
        var entityClass = ReflectionClass.Reflection(entityType);

        foreach (var prop in entityClass.Properties)
        {
            var propName     = prop.Key;
            var propType     = prop.Value.PropertyType;
            var keyAttribute = prop.Value.Info.GetCustomAttribute <KeyAttribute>();
            var isKey        = keyAttribute != null;

            var dataType = GetDataType(propType);

            yield return(new ColumnInfo
            {
                Name = propName,
                DataType = dataType,
                IsKey = isKey
            });
        }
    }
コード例 #7
0
        public IEnumerable <StockExchangeData> GetStockList(string stockId)
        {
            if (Data == null)
            {
                yield break;
            }

            var stockTranObjInfo = ReflectionClass.Reflection(typeof(StockExchangeData));

            foreach (var dataItem in Data)
            {
                var stockTran = new StockExchangeData();
                stockTran.StockId = stockId;
                foreach (var item in Fields.Select((value, idx) => new { name = FieldNames[value], idx }))
                {
                    var valueStr = dataItem[item.idx];
                    var propInfo = stockTranObjInfo.Properties[item.name];
                    var value    = (object)valueStr;
                    if (propInfo.PropertyType != typeof(string))
                    {
                        if (propInfo.PropertyType.IsValueType)
                        {
                            valueStr = valueStr.Replace(",", "");
                            valueStr = valueStr.Replace("X", "");
                        }
                        value = valueStr.ChangeType(propInfo.PropertyType);

                        if (propInfo.Name == nameof(StockExchangeData.Date))
                        {
                            var date = (DateTime)value;
                            date  = date.AddYears(1911);
                            value = date;
                        }
                    }
                    propInfo.Setter(stockTran, value);
                }
                yield return(stockTran);
            }
        }
コード例 #8
0
ファイル: StringExtension.cs プロジェクト: flashlin/Samples
        public static void GetDisplayValue(this object obj, Action <string, string> propValueString)
        {
            var clazz = ReflectionClass.Reflection(obj.GetType());
            var first = true;

            foreach (var prop in clazz.Properties.Values)
            {
                var propInfo    = (PropertyInfo)prop.Info;
                var decimalAttr = propInfo.GetCustomAttribute <DecimalStringAttribute>(false);
                if (decimalAttr != null)
                {
                    var value = (decimal)prop.Getter(obj);
                    if (!first)
                    {
                        propValueString(string.Empty, " ");
                    }

                    var numberString = value.ToNumberString(decimalAttr.MaxLength);
                    propValueString(prop.Name, numberString);
                    first = false;
                    continue;
                }

                var displayAttr = propInfo.GetCustomAttribute <DisplayStringAttribute>(false);
                if (displayAttr != null)
                {
                    if (!first)
                    {
                        propValueString(string.Empty, " ");
                    }
                    var value = prop.Getter(obj);

                    var str = displayAttr.ToDisplayString(value);
                    propValueString(prop.Name, str);
                    first = false;
                    continue;
                }
            }
        }