Exemple #1
0
        internal protected override object OnCreateInstance(SerializationContext serCtx, DataNode data)
        {
            DataItem item = data as DataItem;

            if (item == null)
            {
                throw new InvalidOperationException("Invalid value found for type '" + Name + "'");
            }

            DataValue ctype = item ["ctype"] as DataValue;

            if (ctype != null && ctype.Value != Name)
            {
                bool     isFallbackType;
                DataType stype = FindDerivedType(ctype.Value, null, out isFallbackType);
                if (isFallbackType)
                {
                    // Remove the ctype attribute, to make sure it is not checked again
                    // by the fallback type
                    item.ItemData.Remove(ctype);
                }
                if (stype != null)
                {
                    object sobj = stype.CreateInstance(serCtx, data);
                    // Store the original data type, so it can be serialized back
                    if (isFallbackType && sobj is IExtendedDataItem)
                    {
                        ((IExtendedDataItem)sobj).ExtendedProperties ["__raw_ctype"] = ctype;
                    }
                    return(sobj);
                }
                else
                {
                    throw new InvalidOperationException("Type not found: " + ctype.Value);
                }
            }

            ConstructorInfo ctor = ValueType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);

            if (ctor == null)
            {
                throw new InvalidOperationException("Default constructor not found for type '" + ValueType + "'");
            }

            return(ctor.Invoke(null));
        }
        public object CreateConfigurationData(SerializationContext serCtx, Type type, DataNode data)
        {
            DataType dataType = GetConfigurationDataType(type);

            return(dataType.CreateInstance(serCtx, data));
        }