Example #1
0
        public void SetHolder(Dictionary <string, object> msgDict)
        {
            Type t = this.GetType();
            //ValueMap<string, RepresentedModelValue> model = new RepresentedModel(t).schema;
            ValueMap <string, RepresentedModelValue> model = RepresentedModel.FindScheme(t);

            foreach (KeyValuePair <string, object> v in msgDict)
            {
                int descIndex = model.IndexOf(v.Key);
                if (descIndex >= 0)
                {
                    //PropertyInfo pi = t.GetProperty(v.Key);
                    //if (pi == null)
                    //    continue;
                    PropertyInfo pi = model.val2[descIndex].propertyDescriptor;
                    Type         pt = pi.PropertyType;

                    bool nullable = false;
                    Type ptn      = Nullable.GetUnderlyingType(pt);
                    if (nullable = ptn != null)
                    {
                        pt = ptn;
                    }

                    if (v.Value == null && !nullable)
                    {
                        if (pt.BaseType != typeof(object))
                        {
                            throw new Exception("value does not accept nullable types, key: " + v.Key);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (!nullable && pt != v.Value.GetType())
                    {
                        throw new Exception("unknown type for key: " + v.Key);
                    }

                    pi.SetValue(this, v.Value, null);
                }
            }
            Holder = new Dictionary <string, object>(msgDict);
        }
Example #2
0
        void ReverseSetProps()
        {
            if (Holder == null)
            {
                return;
            }
            Type t = this.GetType();
            ValueMap <string, RepresentedModelValue> model = RepresentedModel.FindScheme(t);

            foreach (KeyValuePair <string, object> v in Holder)
            {
                //PropertyInfo pi = t.GetProperty(v.Key);
                //if (pi == null)
                //    continue;
                int propDescIndex = model.IndexOf(v.Key);
                if (propDescIndex >= 0)
                {
                    PropertyInfo pi = model.val2[propDescIndex].propertyDescriptor;
                    pi.SetValue(this, v.Value, null);
                }
            }
        }