Try

Dec 23, 2009 at 11:40 AM

试图写一个含有BelongsTo的Scaffolding失败了。

BelongsTo的内容在序列化的时候,总是进入

public T Value
        {
            get
            {
                return (T)((ILazyLoading)this).Read();
            }
            set
            {
                ((ILazyLoading)this).Write(value, false);
            }
        }

实在是不想让他Read

尝试了很多种办法比如,他总是不停的.Read();

在序列化前甚至想用JsonConverter来处理一下BelongsTo,但是还是先Read

public class BelongsToConverter : JsonConverter
    {
        public override object ReadJson(JsonReader reader, Type objectType)
        {
            return null;
        }

        public override void WriteJson(JsonWriter writer, object value)
        {
            string text;
            writer.WriteValue("");
        }

        public override bool CanConvert(Type objectType)
        {
            var x = objectType;
            if (objectType.FullName=="$2")
                return true;
           // if (objectType == typeof(DateTime))
             //   return true;
            return false;
        }
    }

无奈,只能放弃RelaitionField了,改用long来做Scaffolding

Coordinator
Dec 24, 2009 at 1:23 AM

为什么Scaffolding要序列化呢?

Dec 24, 2009 at 4:50 AM

说错了,把Model序列化成Json。

你不是定义过那种“序列化属性”来序列化成XML么。

Coordinator
Dec 24, 2009 at 6:19 AM

序列化成XML是特殊处理过的,JSON如果特殊处理一下应该也可以。

 

Dec 24, 2009 at 6:31 AM

成功了。。。。

哇哈哈,DbEntry好强大

 

public string ToJson(IDbObject o)
        {
            JObject Jo = new JObject();
            T t = (T)o;
            ObjectInfo oi = ObjectInfo.GetInstance(typeof(T));
            foreach (MemberHandler memberHandler in oi.SimpleFields)
            {
                Jo.Add(new JProperty(memberHandler.Name, memberHandler.GetValue(t)));
            }
            foreach (MemberHandler memberHandler in oi.RelationFields)
            {
                if (memberHandler.IsBelongsTo)
                {
                    var os = (IBelongsTo)memberHandler.GetValue(t);
                    Jo.Add(new JProperty(memberHandler.Name, os.ForeignKey));
                }
            }
            return Jo.ToString(Newtonsoft.Json.Formatting.None, new IsoDateTimeConverter());
        }

Dec 24, 2009 at 6:33 AM

IBelongsTo 真是神秘啊。

Dec 24, 2009 at 6:35 AM

对il.CallVirtual(typeof(IBelongsTo).GetMethod("set_ForeignKey")); 这句话有着无限学习的欲望