本文整理汇总了C#中System.ComponentModel.PropertyDescriptor.ShouldSerializeValue方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyDescriptor.ShouldSerializeValue方法的具体用法?C# PropertyDescriptor.ShouldSerializeValue怎么用?C# PropertyDescriptor.ShouldSerializeValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ComponentModel.PropertyDescriptor
的用法示例。
在下文中一共展示了PropertyDescriptor.ShouldSerializeValue方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPropertyValue
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;
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:29,代码来源:PropertyMemberCodeDomSerializer.cs
示例2: ProcessAttribute
/// <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;
}
示例3: AddXdoProperty
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;
}
示例4: AddXdoProperty
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);
}
}
}
示例5: ProcessAttribute
/// <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;
}
示例6: ShouldSerialize
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);
}
示例7: GetPropertyValue
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;
}