コード例 #1
0
ファイル: APIHelper.cs プロジェクト: smarenich/CBAPI_Insights
        public static EntityImpl ConvertImpl(CBAPI_Insights.API.Entity entity)
        {
            Type parentEntityType = typeof(CBAPI_Insights.API.Entity);

            EntityImpl ret = new EntityImpl();

            ret.ObjectName = entity.GetType().Name;
            ret.Delete     = entity.Delete;
            ret.ID         = entity.Id;
            //ret.NoteID = entity.SyncID;
            ret.ReturnBehavior = (ReturnBehavior)Enum.Parse(typeof(ReturnBehavior), entity.ReturnBehavior.ToString());
            ret.CustomFields   = entity.Custom?.Select(f => ConvertCustomField(f)).ToArray();

            ret.Fields = new EntityField[0];
            foreach (PropertyInfo pi in entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                if (pi == null)
                {
                    continue;
                }

                Type   t = pi.PropertyType;
                object v = pi.GetValue(entity);
                if (v != null && !t.IsArray && t.IsClass && !parentEntityType.IsAssignableFrom(t))
                {
                    string sv    = null;
                    object value = t.InvokeMember("Value", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty, null, v, new object[] { });

                    switch (t.Name)
                    {
                    case "StringValue":
                        sv = value?.ToString();
                        break;

                    case "IntValue":
                    case "DecimalValue":
                    case "ShortValue":
                    case "BooleanValue":
                    case "ByteValue":
                    case "DoubleValue":
                    case "DateTimeValue":
                    case "LongValue":
                        sv = value == null ? null : Convert.ToString(value, CultureInfo.InvariantCulture);
                        break;

                    case "GuidValue":
                        sv = value == null ? null : ((Guid?)value)?.ToString("", CultureInfo.InvariantCulture);
                        break;
                    }

                    if (v.GetType().Name.EndsWith("Return"))
                    {
                        ret.AddField(new EntityReturnField {
                            Name = pi.Name
                        });
                    }
                    if (v.GetType().Name.EndsWith("Skip"))
                    {
                        ret.AddField(new EntitySkipField {
                            Name = pi.Name
                        });
                    }
                    if (v.GetType().Name.EndsWith("Value"))
                    {
                        ret.AddField(new EntityValueField {
                            Name = pi.Name, Value = sv
                        });
                    }
                    if (v.GetType().Name.EndsWith("Search"))
                    {
                        object condition = v.GetType().InvokeMember("Condition", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty, null, v, new object[] { });

                        ret.AddField(new EntitySearchField {
                            Name = pi.Name, Value = sv, Condition = Enum.GetName(condition.GetType(), condition)
                        });
                    }
                }
                else if (t.IsArray)
                {
                    Array a = pi.GetValue(entity) as Array;
                    if (a != null && a.Length > 0)
                    {
                        EntityListField l = new EntityListField();
                        l.Name  = pi.Name;
                        l.Value = new EntityImpl[a.Length];
                        for (int i = 0; i < a.Length; i++)
                        {
                            l.Value[i] = ConvertImpl((CBAPI_Insights.API.Entity)a.GetValue(i));
                        }
                        ret.AddField(l);
                    }
                }
                else if (t.IsPrimitive || t.IsEnum)
                {
                    PropertyInfo dest = ret.GetType().GetProperties().FirstOrDefault(p => p.Name == t.Name);
                    if (dest != null)
                    {
                        dest.SetValue(ret, pi.GetValue(entity));
                    }
                }
                else if (t.IsClass)
                {
                    object e = pi.GetValue(entity);
                    if (e != null)
                    {
                        ret.AddField(new EntityObjectField {
                            Name = pi.Name, Value = ConvertImpl((CBAPI_Insights.API.Entity)e)
                        });
                    }
                }
            }
            return(ret);
        }
コード例 #2
0
ファイル: APIHelper.cs プロジェクト: smarenich/CBAPI_Insights
        public static CBAPI_Insights.API.Entity ConvertImpl(EntityImpl entity, string nameSpace)
        {
            CBAPI_Insights.API.Entity ret = (CBAPI_Insights.API.Entity)Activator.CreateInstance(System.Web.Compilation.BuildManager.GetType(nameSpace + "." + entity.ObjectName, true));
            ret.Delete         = entity.Delete;
            ret.Id             = entity.ID;
            ret.ReturnBehavior = (CBAPI_Insights.API.ReturnBehavior)Enum.Parse(typeof(CBAPI_Insights.API.ReturnBehavior), entity.ReturnBehavior.ToString());
            ret.Custom         = entity?.CustomFields.Select(f => ConvertCustomField(f)).ToList();


            foreach (EntityField ef in entity.Fields)
            {
                PropertyInfo pi = ret.GetType().GetProperty(ef.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                if (pi == null)
                {
                    continue;
                }

                if (ef is EntityObjectField)
                {
                    EntityObjectField eof = (EntityObjectField)ef;
                    if (eof.Value is EntityImpl)
                    {
                        pi.SetValue(ret, ConvertImpl((EntityImpl)eof.Value, nameSpace));
                    }
                }
                else if (ef is EntityListField)
                {
                    EntityListField elf = (EntityListField)ef;
                    if (elf.Value != null && elf.Value.Length > 0)
                    {
                        Type  arrType = System.Web.Compilation.BuildManager.GetType(nameSpace + "." + elf.Value[0].ObjectName, true);
                        Array arr     = (Array)Activator.CreateInstance(arrType.MakeArrayType(1), new object[] { elf.Value.Length });
                        for (int i = 0; i < elf.Value.Length; i++)
                        {
                            arr.SetValue(ConvertImpl(elf.Value[i], nameSpace), i);
                        }

                        MethodInfo convertMethod = typeof(APIHelper).GetMethod("ConvertArray", BindingFlags.Public | BindingFlags.Static);
                        MethodInfo generic       = convertMethod.MakeGenericMethod(new[] { arrType });


                        pi.SetValue(ret, generic.Invoke(null, new object[] { arr }));
                    }
                }
                else if (ef is EntityValueField)
                {
                    EntityValueField evf = (EntityValueField)ef;
                    if (evf.Value != null)
                    {
                        Type t = pi.PropertyType;

                        object value = null;
                        object po    = Activator.CreateInstance(t);
                        switch (t.Name)
                        {
                        case "StringValue":
                            value = evf.Value;
                            break;

                        case "IntValue":
                            value = int.Parse(evf.Value, CultureInfo.InvariantCulture);
                            break;

                        case "DecimalValue":
                            value = decimal.Parse(evf.Value, CultureInfo.InvariantCulture);
                            break;

                        case "ShortValue":
                            value = short.Parse(evf.Value, CultureInfo.InvariantCulture);
                            break;

                        case "BooleanValue":
                            value = bool.Parse(evf.Value);
                            break;

                        case "ByteValue":
                            value = byte.Parse(evf.Value, CultureInfo.InvariantCulture);
                            break;

                        case "DoubleValue":
                            value = double.Parse(evf.Value, CultureInfo.InvariantCulture);
                            break;

                        case "DateTimeValue":
                            value = DateTime.Parse(evf.Value, CultureInfo.InvariantCulture);
                            break;

                        case "GuidValue":
                            value = Guid.Parse(evf.Value);
                            break;

                        case "LongValue":
                            value = long.Parse(evf.Value, CultureInfo.InvariantCulture);
                            break;
                        }
                        t.InvokeMember("Value", BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, null, po, new object[] { value });

                        pi.SetValue(ret, po);
                    }
                }
            }
            return(ret);
        }