ShouldSerializeValue() public abstract method

When overridden in a derived class, indicates whether the value of this property needs to be persisted.

public abstract ShouldSerializeValue ( object component ) : bool
component object
return bool
 private object GetPropertyValue(IDesignerSerializationManager manager, PropertyDescriptor property, object value, out bool validValue)
 {
     object obj2 = null;
     validValue = true;
     try
     {
         if (!property.ShouldSerializeValue(value))
         {
             AmbientValueAttribute attribute = (AmbientValueAttribute) property.Attributes[typeof(AmbientValueAttribute)];
             if (attribute != null)
             {
                 return attribute.Value;
             }
             DefaultValueAttribute attribute2 = (DefaultValueAttribute) property.Attributes[typeof(DefaultValueAttribute)];
             if (attribute2 != null)
             {
                 return attribute2.Value;
             }
             validValue = false;
         }
         obj2 = property.GetValue(value);
     }
     catch (Exception exception)
     {
         validValue = false;
         manager.ReportError(System.Design.SR.GetString("SerializerPropertyGenFailed", new object[] { property.Name, exception.Message }));
     }
     return obj2;
 }
Example #2
0
        /// <summary>
        /// Writes an attribute to an HtmlTextWriter if it needs serializing
        /// </summary>
        /// <returns>True if it does any writing</returns>
        private static bool ProcessAttribute(PropertyDescriptor prop, object o, HtmlTextWriter writer, string prefix)
        {
            //check whether we're serialising it
            if (prop.SerializationVisibility == DesignerSerializationVisibility.Hidden
                || prop.DesignTimeOnly
                || prop.IsReadOnly
                || !prop.ShouldSerializeValue (o)
                || prop.Converter == null
                || !prop.Converter.CanConvertTo (typeof(string)))
                return false;

            bool foundAttrib = false;

            //is this an attribute? If it's content, we deal with it later.
            PersistenceModeAttribute modeAttrib = prop.Attributes[typeof (PersistenceModeAttribute)] as PersistenceModeAttribute;
            if (modeAttrib == null || modeAttrib.Mode == PersistenceMode.Attribute)
            {
                if (prop.SerializationVisibility == DesignerSerializationVisibility.Visible) {
                    if (prefix == string.Empty)
                        writer.WriteAttribute (prop.Name, prop.Converter.ConvertToString (prop.GetValue (o)));
                    else
                        writer.WriteAttribute (prefix + "-" + prop.Name, prop.Converter.ConvertToString (prop.GetValue(o)));
                    foundAttrib = true;
                }
                //recursively handle subproperties
                else if (prop.SerializationVisibility == DesignerSerializationVisibility.Content) {
                    object val = prop.GetValue (o);
                    foreach (PropertyDescriptor p in prop.GetChildProperties (val))
                        if (ProcessAttribute (p, val, writer, prop.Name))
                            foundAttrib = true;
                }
            }
            return foundAttrib;
        }
Example #3
0
        internal void AddXdoProperty(PropertyDescriptor pd, Object instance, XmlElement root, XmlDocument xd) {
            Type type = pd.PropertyType;
            bool  bisDataColumn = false;
            DataColumn col = null;  // it may cause problem to assign null here, I will need to change this.
            bool bIsSqlType = false;
            bool bImplementsInullable = false;

            if (instance is DataColumn) {
                col = (DataColumn)instance;
                bisDataColumn = true;
                bIsSqlType =  col.IsSqlType;
                bImplementsInullable = col.ImplementsINullable;
            }

            if (bImplementsInullable == false &&
                 type != typeof(string) &&     // DO NOT REMOVE THIS 
                 type != typeof(bool) &&
                 type != typeof(Type) &&
                 type != typeof(object) &&
                 type != typeof(CultureInfo) &&
                 type != typeof(Int64) &&
                 type != typeof(Int32) ) {
                return;
            }

            if ((!pd.ShouldSerializeValue(instance) || !pd.Attributes.Contains(DesignerSerializationVisibilityAttribute.Visible))&&( bIsSqlType == false)) {
                return;
            }

            Object propInst = pd.GetValue(instance) ;

            if (propInst is InternalDataCollectionBase)
                return;

            if (propInst is PropertyCollection) {
                return;
            }
            // [....]: perf: Why not have this as a table?
            // there are several xdo properties that equal to some xml attributes, we should not explicitly ouput them.
            if (
                0 == String.Compare(pd.Name, "Namespace"    , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "PrimaryKey"   , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "ColumnName"   , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "DefaultValue" , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "TableName"    , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "DataSetName"  , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "AllowDBNull"  , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "Unique"       , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "NestedInDataSet" , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "Locale"       , StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "CaseSensitive", StringComparison.Ordinal) ||
                0 == String.Compare(pd.Name, "RemotingFormat"       , StringComparison.Ordinal)
            ) {
                return;
            }

            if (bisDataColumn){    //(instance is DataColumn) {
                if (0 == String.Compare(pd.Name, "DataType", StringComparison.Ordinal)) {
                    string dt = XmlDataTypeName(col.DataType);
                    if(bIsSqlType || (col.DataType == typeof(System.Numerics.BigInteger))) {
                        root.SetAttribute(Keywords.MSD_DATATYPE, Keywords.MSDNS, col.DataType.FullName);
                    }
                    else if ((dt.Length == 0) || bImplementsInullable || ((dt == Keywords.XSD_ANYTYPE) && (col.XmlDataType != Keywords.XSD_ANYTYPE))|| (col.DataType == typeof(DateTimeOffset))) {
                        // in Whidbey, XmlDataTypeName function changed to return "anyType" for typeof(Object)
                        // should still always hit this code path for all non-built in types
                        // to handle version qualified typeof(Object) and other CDT objects correctly

                        // we must write the output exactly the same way as we read it
                        this.SetMSDataAttribute(root, col.DataType);
                    }
                    return;
                }
                if (0 == String.Compare(pd.Name, "Attribute", StringComparison.Ordinal)) {
                    return;
                }
            }

            string textValue = pd.Converter.ConvertToString(propInst) ;
            root.SetAttribute(pd.Name, Keywords.MSDNS, textValue);
            return;

        }
 internal void AddXdoProperty(PropertyDescriptor pd, object instance, XmlElement root, XmlDocument xd)
 {
     Type propertyType = pd.PropertyType;
     bool flag3 = false;
     DataColumn column = null;
     bool isSqlType = false;
     bool implementsINullable = false;
     if (instance is DataColumn)
     {
         column = (DataColumn) instance;
         flag3 = true;
         isSqlType = column.IsSqlType;
         implementsINullable = column.ImplementsINullable;
     }
     if ((((implementsINullable || (propertyType == typeof(string))) || ((propertyType == typeof(bool)) || (propertyType == typeof(Type)))) || (((propertyType == typeof(object)) || (propertyType == typeof(CultureInfo))) || ((propertyType == typeof(long)) || (propertyType == typeof(int))))) && ((pd.ShouldSerializeValue(instance) && pd.Attributes.Contains(DesignerSerializationVisibilityAttribute.Visible)) || isSqlType))
     {
         object obj2 = pd.GetValue(instance);
         if ((!(obj2 is InternalDataCollectionBase) && !(obj2 is PropertyCollection)) && (((((string.Compare(pd.Name, "Namespace", StringComparison.Ordinal) != 0) && (string.Compare(pd.Name, "PrimaryKey", StringComparison.Ordinal) != 0)) && ((string.Compare(pd.Name, "ColumnName", StringComparison.Ordinal) != 0) && (string.Compare(pd.Name, "DefaultValue", StringComparison.Ordinal) != 0))) && (((string.Compare(pd.Name, "TableName", StringComparison.Ordinal) != 0) && (string.Compare(pd.Name, "DataSetName", StringComparison.Ordinal) != 0)) && ((string.Compare(pd.Name, "AllowDBNull", StringComparison.Ordinal) != 0) && (string.Compare(pd.Name, "Unique", StringComparison.Ordinal) != 0)))) && (((string.Compare(pd.Name, "NestedInDataSet", StringComparison.Ordinal) != 0) && (string.Compare(pd.Name, "Locale", StringComparison.Ordinal) != 0)) && ((string.Compare(pd.Name, "CaseSensitive", StringComparison.Ordinal) != 0) && (string.Compare(pd.Name, "RemotingFormat", StringComparison.Ordinal) != 0)))))
         {
             if (flag3)
             {
                 if (string.Compare(pd.Name, "DataType", StringComparison.Ordinal) == 0)
                 {
                     string str = XmlDataTypeName(column.DataType);
                     if (isSqlType || (column.DataType == typeof(BigInteger)))
                     {
                         root.SetAttribute("DataType", "urn:schemas-microsoft-com:xml-msdata", column.DataType.FullName);
                         return;
                     }
                     if ((((str.Length == 0) || implementsINullable) || ((str == "anyType") && (column.XmlDataType != "anyType"))) || (column.DataType == typeof(DateTimeOffset)))
                     {
                         this.SetMSDataAttribute(root, column.DataType);
                     }
                     return;
                 }
                 if (string.Compare(pd.Name, "Attribute", StringComparison.Ordinal) == 0)
                 {
                     return;
                 }
             }
             string str2 = pd.Converter.ConvertToString(obj2);
             root.SetAttribute(pd.Name, "urn:schemas-microsoft-com:xml-msdata", str2);
         }
     }
 }
Example #5
0
		/// <summary>
		/// Writes an attribute to an HtmlTextWriter if it needs serializing
		/// </summary>
		/// <returns>True if it does any writing</returns>
		private static bool ProcessAttribute (PropertyDescriptor prop, object o, HtmlTextWriter writer, string prefix)
		{
			//FIXME: there are several NotImplementedExceptions in Mono's ASP.NET 2.0
			//this is a hack so it doesn't break when encountering them
			try {
				prop.GetValue (o);
			} catch (Exception ex) {
				if ((ex is NotImplementedException) || (ex.InnerException is NotImplementedException))
					return false;
				else
					throw;
			}
			
			//FIXME: Mono has started to return prop.ShouldSerializeValue = false for ID
			//workaround, because I'm not sure if this is expected behaviour, and it would still be 
			//broken for some people's runtime
			if (prop.DisplayName == "ID") {
				writer.WriteAttribute ("id", prop.GetValue (o) as string);
				return true;
			}
			
			//check whether we're serialising it
			if (prop.SerializationVisibility == DesignerSerializationVisibility.Hidden
				|| prop.DesignTimeOnly
				|| prop.IsReadOnly
				|| !prop.ShouldSerializeValue (o)
				|| prop.Converter == null
				|| !prop.Converter.CanConvertTo (typeof(string)))
				return false;
			
			bool foundAttrib = false;
				
			//is this an attribute? If it's content, we deal with it later.		
			PersistenceModeAttribute modeAttrib = prop.Attributes[typeof (PersistenceModeAttribute)] as PersistenceModeAttribute;
			if (modeAttrib == null || modeAttrib.Mode == PersistenceMode.Attribute)
			{
				if (prop.SerializationVisibility == DesignerSerializationVisibility.Visible) {
					if (prefix == string.Empty)
						writer.WriteAttribute (prop.Name, prop.Converter.ConvertToString (prop.GetValue (o)));
					else
						writer.WriteAttribute (prefix + "-" + prop.Name, prop.Converter.ConvertToString (prop.GetValue(o)));
					foundAttrib = true;
				}
				//recursively handle subproperties
				else if (prop.SerializationVisibility == DesignerSerializationVisibility.Content) {
					object val = prop.GetValue (o);
					foreach (PropertyDescriptor p in prop.GetChildProperties (val))
						if (ProcessAttribute (p, val, writer, prop.Name))
							foundAttrib = true;
				}
			}
			return foundAttrib;
		}
        private static bool ShouldSerialize(PropertyDescriptor pd, object instance, XamlDesignerSerializationManager manager)
        {
            MethodInfo shouldSerializeMethod;
            object invokeInstance = instance; 

            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(pd); 
            if (dpd != null && dpd.IsAttached) 
            {
                Type ownerType = dpd.DependencyProperty.OwnerType; 
                string propertyName = dpd.DependencyProperty.Name;
                string keyName = propertyName + "!";
                if (!TryGetShouldSerializeMethod(new ShouldSerializeKey(ownerType, keyName), out shouldSerializeMethod))
                { 
                    string methodName = "ShouldSerialize" + propertyName;
                    shouldSerializeMethod = ownerType.GetMethod(methodName, BindingFlags.Static | 
                        BindingFlags.NonPublic | BindingFlags.Public, null, _shouldSerializeArgsObject, null); 
                    if (shouldSerializeMethod == null)
                        shouldSerializeMethod = ownerType.GetMethod(methodName, BindingFlags.Static | 
                        BindingFlags.NonPublic | BindingFlags.Public, null, _shouldSerializeArgsManager, null);
                    if (shouldSerializeMethod == null)
                        shouldSerializeMethod = ownerType.GetMethod(methodName, BindingFlags.Static |
                        BindingFlags.NonPublic | BindingFlags.Public, null, _shouldSerializeArgsMode, null); 
                    if (shouldSerializeMethod == null)
                        shouldSerializeMethod = ownerType.GetMethod(methodName, BindingFlags.Static | 
                        BindingFlags.NonPublic | BindingFlags.Public, null, _shouldSerializeArgsObjectManager, null); 
                    if (shouldSerializeMethod != null && shouldSerializeMethod.ReturnType != typeof(bool))
                        shouldSerializeMethod = null; 
                    CacheShouldSerializeMethod(new ShouldSerializeKey(ownerType, keyName), shouldSerializeMethod);
                }
                invokeInstance = null; // static method
            } 
            else
            { 
                if (!TryGetShouldSerializeMethod(new ShouldSerializeKey(instance.GetType(), pd.Name), out shouldSerializeMethod)) 
                {
                    Type instanceType = instance.GetType(); 
                    string methodName = "ShouldSerialize" + pd.Name;
                    shouldSerializeMethod = instanceType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static |
                        BindingFlags.NonPublic | BindingFlags.Public, null, _shouldSerializeArgsObject, null);
                    if (shouldSerializeMethod == null) 
                        shouldSerializeMethod = instanceType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static |
                        BindingFlags.NonPublic | BindingFlags.Public, null, _shouldSerializeArgsManager, null); 
                    if (shouldSerializeMethod == null) 
                        shouldSerializeMethod = instanceType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static |
                        BindingFlags.NonPublic | BindingFlags.Public, null, _shouldSerializeArgsMode, null); 
                    if (shouldSerializeMethod == null)
                        shouldSerializeMethod = instanceType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static |
                        BindingFlags.NonPublic | BindingFlags.Public, null, _shouldSerializeArgsObjectManager, null);
                    if (shouldSerializeMethod != null && shouldSerializeMethod.ReturnType != typeof(bool)) 
                        shouldSerializeMethod = null;
                    CacheShouldSerializeMethod(new ShouldSerializeKey(instanceType, pd.Name), shouldSerializeMethod); 
                } 
            }
            if (shouldSerializeMethod != null) 
            {
                ParameterInfo[] parameters = shouldSerializeMethod.GetParameters();
                if (parameters != null)
                { 
                    object[] args;
                    if (parameters.Length == 1) 
                    { 
                        if (parameters[0].ParameterType == typeof(DependencyObject))
                        { 
                            args = new object[] { instance as DependencyObject };
                        }
                        else if (parameters[0].ParameterType == typeof(XamlWriterMode))
                        { 
                            args = new object[] { manager.XamlWriterMode };
                        } 
                        else 
                        {
                            args = new object[] { manager }; 
                        }
                    }
                    else
                        args = new object[] { instance as DependencyObject, manager }; 
                    return (bool)shouldSerializeMethod.Invoke(invokeInstance, args);
                } 
            } 
            return pd.ShouldSerializeValue(instance);
        } 
 private object GetPropertyValue(IDesignerSerializationManager manager, PropertyDescriptor property, object value, out bool validValue)
 {
     object instance = null;
     validValue = true;
     try
     {
         if (!property.ShouldSerializeValue(value))
         {
             AmbientValueAttribute attribute = (AmbientValueAttribute) property.Attributes[typeof(AmbientValueAttribute)];
             if (attribute != null)
             {
                 return attribute.Value;
             }
             DefaultValueAttribute attribute2 = (DefaultValueAttribute) property.Attributes[typeof(DefaultValueAttribute)];
             if (attribute2 != null)
             {
                 return attribute2.Value;
             }
             validValue = false;
         }
         instance = property.GetValue(value);
     }
     catch (Exception exception)
     {
         validValue = false;
         manager.ReportError(System.Design.SR.GetString("SerializerPropertyGenFailed", new object[] { property.Name, exception.Message }));
     }
     if (((instance != null) && !instance.GetType().IsValueType) && !TypeDescriptor.GetProvider(instance).GetReflectionType(typeof(object)).IsDefined(typeof(ProjectTargetFrameworkAttribute), false))
     {
         TypeDescriptionProvider targetFrameworkProvider = CodeDomSerializerBase.GetTargetFrameworkProvider(manager, instance);
         if (targetFrameworkProvider != null)
         {
             TypeDescriptor.AddProvider(targetFrameworkProvider, instance);
         }
     }
     return instance;
 }