/// <summary> /// Gets a collection of standard values collection for a System.Globalization.CultureInfo /// object using the specified context. /// </summary> public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { StandardValuesCollection values = null; if (context.PropertyDescriptor is not null) { ExtenderProvidedPropertyAttribute attr = context.PropertyDescriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; if (attr is not null) { LanguageExtenders provider = attr.Provider as LanguageExtenders; if (provider is not null) { values = provider.SupportedCultures; } } } if (values is null) { values = base.GetStandardValues(context); } return(values); }
/// <summary> /// Creates a new ExtenderProvidedPropertyAttribute. /// </summary> internal static ExtenderProvidedPropertyAttribute Create(PropertyDescriptor extenderProperty, Type receiverType, IExtenderProvider provider) { ExtenderProvidedPropertyAttribute e = new ExtenderProvidedPropertyAttribute(); e._extenderProperty = extenderProperty; e._receiverType = receiverType; e._provider = provider; return e; }
public void Ctor_Default() { var attribute = new ExtenderProvidedPropertyAttribute(); Assert.Null(attribute.ExtenderProperty); Assert.Null(attribute.Provider); Assert.Null(attribute.ReceiverType); }
public void Equals_Object_ReturnsExpected(ExtenderProvidedPropertyAttribute attribute, object other, bool expected) { Assert.Equal(expected, attribute.Equals(other)); if (other is ExtenderProvidedPropertyAttribute) { Assert.Equal(expected, attribute.GetHashCode().Equals(other.GetHashCode())); } }
public override object Serialize(IDesignerSerializationManager manager, object value) { PropertyDescriptor descriptor = (PropertyDescriptor)manager.Context[typeof(PropertyDescriptor)]; ExpressionContext context = (ExpressionContext)manager.Context[typeof(ExpressionContext)]; bool flag = (value != null) ? CodeDomSerializerBase.GetReflectionTypeHelper(manager, value).IsSerializable : true; bool flag2 = !flag; bool flag3 = (descriptor != null) && descriptor.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content); if (!flag2) { flag2 = ((context != null) && (context.PresetValue != null)) && (context.PresetValue == value); } if (((this._model == CodeDomLocalizationModel.PropertyReflection) && !flag3) && !flag2) { CodeStatementCollection statements = (CodeStatementCollection)manager.Context[typeof(CodeStatementCollection)]; bool flag4 = false; ExtenderProvidedPropertyAttribute attribute = null; if (descriptor != null) { attribute = descriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; if ((attribute != null) && (attribute.ExtenderProperty != null)) { flag4 = true; } } if ((!flag4 && (context != null)) && (statements != null)) { string name = manager.GetName(context.Owner); CodeExpression expression = base.SerializeToExpression(manager, context.Owner); if ((name != null) && (expression != null)) { RootContext context2 = manager.Context[typeof(RootContext)] as RootContext; if ((context2 != null) && (context2.Value == context.Owner)) { name = "$this"; } base.SerializeToResourceExpression(manager, value, false); if (this.EmitApplyMethod(manager, context.Owner)) { ResourceManager manager2 = manager.Context[typeof(ResourceManager)] as ResourceManager; CodeMethodReferenceExpression expression3 = new CodeMethodReferenceExpression(base.GetExpression(manager, manager2), "ApplyResources"); CodeMethodInvokeExpression expression4 = new CodeMethodInvokeExpression { Method = expression3 }; expression4.Parameters.Add(expression); expression4.Parameters.Add(new CodePrimitiveExpression(name)); statements.Add(expression4); } return(null); } } } if (flag2) { return(this._currentSerializer.Serialize(manager, value)); } return(base.SerializeToResourceExpression(manager, value)); }
/// <summary> /// Creates a new ExtenderProvidedPropertyAttribute. /// </summary> internal static ExtenderProvidedPropertyAttribute Create(PropertyDescriptor extenderProperty, Type receiverType, IExtenderProvider provider) { ExtenderProvidedPropertyAttribute e = new ExtenderProvidedPropertyAttribute(); e._extenderProperty = extenderProperty; e._receiverType = receiverType; e._provider = provider; return(e); }
public static IEnumerable <object[]> Equals_TestData() { var attribute = new ExtenderProvidedPropertyAttribute(); yield return(new object[] { attribute, attribute, true }); yield return(new object[] { attribute, new object(), false }); yield return(new object[] { attribute, null, false }); }
/// <summary> /// This method actually performs the serialization. When the member is serialized /// the necessary statements will be added to the statements collection. /// </summary> public override void Serialize(IDesignerSerializationManager manager, object value, MemberDescriptor descriptor, CodeStatementCollection statements) { if (manager is null) { throw new ArgumentNullException(nameof(manager)); } if (value is null) { throw new ArgumentNullException(nameof(value)); } if (!(descriptor is PropertyDescriptor propertyToSerialize)) { throw new ArgumentNullException(nameof(descriptor)); } if (statements is null) { throw new ArgumentNullException(nameof(statements)); } try { ExtenderProvidedPropertyAttribute exAttr = (ExtenderProvidedPropertyAttribute)propertyToSerialize.Attributes[typeof(ExtenderProvidedPropertyAttribute)]; bool isExtender = (exAttr != null && exAttr.Provider != null); bool serializeContents = propertyToSerialize.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content); CodeDomSerializer.Trace("Serializing property {0}", propertyToSerialize.Name); if (serializeContents) { SerializeContentProperty(manager, value, propertyToSerialize, isExtender, statements); } else if (isExtender) { SerializeExtenderProperty(manager, value, propertyToSerialize, statements); } else { SerializeNormalProperty(manager, value, propertyToSerialize, statements); } } catch (Exception e) { // Since we usually go through reflection, don't // show what our engine does, show what caused // the problem. if (e is TargetInvocationException) { e = e.InnerException; } manager.ReportError(new CodeDomSerializerException(string.Format(SR.SerializerPropertyGenFailed, propertyToSerialize.Name, e.Message), manager)); } }
public override bool Equals(object obj) { if (obj == this) { return(true); } ExtenderProvidedPropertyAttribute other = obj as ExtenderProvidedPropertyAttribute; return((other != null) && other._extenderProperty.Equals(_extenderProperty) && other._provider.Equals(_provider) && other._receiverType.Equals(_receiverType)); }
public void Equals_NullProperties_ThrowsNullReferenceException() { var attribute = new ExtenderProvidedPropertyAttribute(); if (!PlatformDetection.IsFullFramework) { Assert.True(attribute.Equals(new ExtenderProvidedPropertyAttribute())); } else { Assert.Throws <NullReferenceException>(() => attribute.Equals(new ExtenderProvidedPropertyAttribute())); } }
public void GetExtenderProviders_ReturnResultFromComponentSiteService_WhenComponentSiteServiceNotNull() { using TestComponent testComponent = new TestComponent(); testComponent.Site = new TestSiteWithService(); testComponent.Disposed += (object obj, EventArgs args) => { }; PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(testComponent); PropertyDescriptor testPropDescriptor = propertyDescriptorCollection["TestProp"]; Assert.NotNull(testPropDescriptor); ExtenderProvidedPropertyAttribute extenderProvidedPropertyAttribute = testPropDescriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; Assert.NotNull(extenderProvidedPropertyAttribute); Assert.IsType <TestExtenderProvider>(extenderProvidedPropertyAttribute.Provider); }
private void SerializeExtenderProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, CodeStatementCollection statements) { AttributeCollection attributes = property.Attributes; using (CodeDomSerializerBase.TraceScope("PropertyMemberCodeDomSerializer::SerializeExtenderProperty")) { ExtenderProvidedPropertyAttribute attribute = (ExtenderProvidedPropertyAttribute)attributes[typeof(ExtenderProvidedPropertyAttribute)]; CodeExpression targetObject = base.SerializeToExpression(manager, attribute.Provider); CodeExpression expression2 = base.SerializeToExpression(manager, value); if ((targetObject != null) && (expression2 != null)) { bool flag; CodeMethodReferenceExpression expression = new CodeMethodReferenceExpression(targetObject, "Set" + property.Name); object obj2 = this.GetPropertyValue(manager, property, value, out flag); CodeExpression expression4 = null; if (flag) { ExpressionContext context = null; if (obj2 != value) { context = new ExpressionContext(expression, property.PropertyType, value); manager.Context.Push(context); } try { expression4 = base.SerializeToExpression(manager, obj2); } finally { if (context != null) { manager.Context.Pop(); } } } if (expression4 != null) { CodeMethodInvokeExpression expression5 = new CodeMethodInvokeExpression { Method = expression }; expression5.Parameters.Add(expression2); expression5.Parameters.Add(expression4); statements.Add(expression5); } } } }
public override void Serialize(IDesignerSerializationManager manager, object value, MemberDescriptor descriptor, CodeStatementCollection statements) { PropertyDescriptor property = descriptor as PropertyDescriptor; if (manager == null) { throw new ArgumentNullException("manager"); } if (value == null) { throw new ArgumentNullException("value"); } if (property == null) { throw new ArgumentNullException("descriptor"); } if (statements == null) { throw new ArgumentNullException("statements"); } try { ExtenderProvidedPropertyAttribute attribute = (ExtenderProvidedPropertyAttribute)property.Attributes[typeof(ExtenderProvidedPropertyAttribute)]; bool isExtender = (attribute != null) && (attribute.Provider != null); if (property.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content)) { this.SerializeContentProperty(manager, value, property, isExtender, statements); } else if (isExtender) { this.SerializeExtenderProperty(manager, value, property, statements); } else { this.SerializeNormalProperty(manager, value, property, statements); } } catch (Exception innerException) { if (innerException is TargetInvocationException) { innerException = innerException.InnerException; } manager.ReportError(System.Design.SR.GetString("SerializerPropertyGenFailed", new object[] { property.Name, innerException.Message })); } }
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { TypeConverter.StandardValuesCollection supportedCultures = null; if (context.PropertyDescriptor != null) { ExtenderProvidedPropertyAttribute attribute = context.PropertyDescriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; if (attribute != null) { CodeDomLocalizationProvider.LanguageExtenders provider = attribute.Provider as CodeDomLocalizationProvider.LanguageExtenders; if (provider != null) { supportedCultures = provider.SupportedCultures; } } } if (supportedCultures == null) { supportedCultures = base.GetStandardValues(context); } return(supportedCultures); }
private void SerializeContentProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, bool isExtender, CodeStatementCollection statements) { bool flag; object presetValue = this.GetPropertyValue(manager, property, value, out flag); CodeDomSerializer serializer = null; if (presetValue == null) { string name = manager.GetName(value); if (name == null) { name = value.GetType().FullName; } manager.ReportError(System.Design.SR.GetString("SerializerNullNestedProperty", new object[] { name, property.Name })); } else { serializer = (CodeDomSerializer)manager.GetSerializer(presetValue.GetType(), typeof(CodeDomSerializer)); if (serializer != null) { CodeExpression targetObject = base.SerializeToExpression(manager, value); if (targetObject != null) { CodeExpression expression = null; if (isExtender) { ExtenderProvidedPropertyAttribute attribute = (ExtenderProvidedPropertyAttribute)property.Attributes[typeof(ExtenderProvidedPropertyAttribute)]; CodeExpression expression3 = base.SerializeToExpression(manager, attribute.Provider); CodeExpression expression4 = base.SerializeToExpression(manager, value); if ((expression3 != null) && (expression4 != null)) { CodeMethodReferenceExpression expression5 = new CodeMethodReferenceExpression(expression3, "Get" + property.Name); CodeMethodInvokeExpression expression6 = new CodeMethodInvokeExpression { Method = expression5 }; expression6.Parameters.Add(expression4); expression = expression6; } } else { expression = new CodePropertyReferenceExpression(targetObject, property.Name); } if (expression != null) { ExpressionContext context = new ExpressionContext(expression, property.PropertyType, value, presetValue); manager.Context.Push(context); object obj3 = null; try { SerializeAbsoluteContext context2 = (SerializeAbsoluteContext)manager.Context[typeof(SerializeAbsoluteContext)]; if (base.IsSerialized(manager, presetValue, context2 != null)) { obj3 = base.GetExpression(manager, presetValue); } else { obj3 = serializer.Serialize(manager, presetValue); } } finally { manager.Context.Pop(); } CodeStatementCollection statements2 = obj3 as CodeStatementCollection; if (statements2 == null) { CodeStatement statement2 = obj3 as CodeStatement; if (statement2 != null) { statements.Add(statement2); } } else { foreach (CodeStatement statement in statements2) { statements.Add(statement); } } } } } else { manager.ReportError(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { property.PropertyType.FullName })); } } }
/// <summary> /// Serializes the given object into a CodeDom object. This uses the stock /// resource serialization scheme and retains the expression it provides. /// </summary> public override object Serialize(IDesignerSerializationManager manager, object value) { PropertyDescriptor desc = (PropertyDescriptor)manager.Context[typeof(PropertyDescriptor)]; ExpressionContext tree = (ExpressionContext)manager.Context[typeof(ExpressionContext)]; bool isSerializable = (value != null) ? GetReflectionTypeHelper(manager, value).IsSerializable : true; // If value is not serializable, we have no option but to call the original serializer, // since we cannot push this into resources. bool callExistingSerializer = !isSerializable; // Compat: If we are serializing content, we need to skip property reflection to preserve compatibility, // since tools like WinRes expect items in collections (like TreeNodes and ListViewItems) // to be serialized as binary blobs. bool serializingContent = (desc != null && desc.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content)); // We also skip back to the original serializer if there is a preset value for this object. if (!callExistingSerializer) { callExistingSerializer = tree != null && tree.PresetValue != null && tree.PresetValue == value; } if (_model == CodeDomLocalizationModel.PropertyReflection && !serializingContent && !callExistingSerializer) { // For a property reflecting model, we need to do more work. Here we need to find // the object we are serializing against and inject an "ApplyResources" method // against the object and its name. If any of this machinery fails we will // just return the existing expression which will default to the original behavior. CodeStatementCollection statements = (CodeStatementCollection)manager.Context[typeof(CodeStatementCollection)]; // In the case of extender properties, we don't want to serialize using the property // reflecting model. In this case we'll skip it and fall through to the // property assignment model. bool skipPropertyReflect = false; ExtenderProvidedPropertyAttribute attr = null; if (desc != null) { attr = desc.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; if (attr != null && attr.ExtenderProperty != null) { skipPropertyReflect = true; } } if (!skipPropertyReflect && tree != null && statements != null) { string name = manager.GetName(tree.Owner); CodeExpression ownerExpression = SerializeToExpression(manager, tree.Owner); if (name != null && ownerExpression != null) { RootContext rootCxt = manager.Context[typeof(RootContext)] as RootContext; if (rootCxt != null && rootCxt.Value == tree.Owner) { name = "$this"; } // Ok, if we got here it means we have enough data to emit // using the reflection model. SerializeToResourceExpression(manager, value, false); if (EmitApplyMethod(manager, tree.Owner)) { ResourceManager rm = manager.Context[typeof(ResourceManager)] as ResourceManager; Debug.Assert(rm != null, "No resource manager available in context."); CodeExpression rmExpression = GetExpression(manager, rm); Debug.Assert(rmExpression != null, "No expression available for resource manager."); CodeMethodReferenceExpression methodRef = new CodeMethodReferenceExpression(rmExpression, "ApplyResources"); CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression(); methodInvoke.Method = methodRef; methodInvoke.Parameters.Add(ownerExpression); methodInvoke.Parameters.Add(new CodePrimitiveExpression(name)); statements.Add(methodInvoke); } return(null); // we have already worked our statements into the tree. } } } if (callExistingSerializer) { return(_currentSerializer.Serialize(manager, value)); } return(SerializeToResourceExpression(manager, value)); }
public void Equals_NullProperties_ThrowsNullReferenceException() { var attribute = new ExtenderProvidedPropertyAttribute(); Assert.Throws <NullReferenceException>(() => attribute.Equals(new ExtenderProvidedPropertyAttribute())); }
/// <summary> /// This serializes the given property on this object. /// </summary> private void SerializeExtenderProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, CodeStatementCollection statements) { AttributeCollection attributes = property.Attributes; using (CodeDomSerializer.TraceScope("PropertyMemberCodeDomSerializer::" + nameof(SerializeExtenderProperty))) { ExtenderProvidedPropertyAttribute exAttr = (ExtenderProvidedPropertyAttribute)attributes[typeof(ExtenderProvidedPropertyAttribute)]; // Extender properties are method invokes on a target "extender" object. // CodeExpression extender = SerializeToExpression(manager, exAttr.Provider); CodeExpression extended = SerializeToExpression(manager, value); CodeDomSerializer.TraceWarningIf(extender is null, "Extender object {0} could not be serialized.", manager.GetName(exAttr.Provider)); CodeDomSerializer.TraceWarningIf(extended is null, "Extended object {0} could not be serialized.", manager.GetName(value)); if (extender != null && extended != null) { CodeMethodReferenceExpression methodRef = new CodeMethodReferenceExpression(extender, "Set" + property.Name); object propValue = GetPropertyValue(manager, property, value, out bool validValue); CodeExpression serializedPropertyValue = null; // Serialize the value of this property into a code expression. If we can't get one, // then we won't serialize the property. if (validValue) { ExpressionContext tree = null; if (propValue != value) { // make sure the value isn't the object or we'll end up printing // this property instead of the value. tree = new ExpressionContext(methodRef, property.PropertyType, value); manager.Context.Push(tree); } try { serializedPropertyValue = SerializeToExpression(manager, propValue); } finally { if (tree != null) { Debug.Assert(manager.Context.Current == tree, "Context stack corrupted."); manager.Context.Pop(); } } } if (serializedPropertyValue != null) { CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression { Method = methodRef }; methodInvoke.Parameters.Add(extended); methodInvoke.Parameters.Add(serializedPropertyValue); statements.Add(methodInvoke); } } } }
/// <summary> /// This serializes the given property on this object as a content property. /// </summary> private void SerializeContentProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, bool isExtender, CodeStatementCollection statements) { CodeDomSerializer.Trace("Property is marked as Visibility.Content. Recursing."); object propertyValue = GetPropertyValue(manager, property, value, out bool validValue); // For persist contents objects, we don't just serialize the properties on the object; we // serialize everything. // CodeDomSerializer serializer = null; if (propertyValue is null) { CodeDomSerializer.TraceError("Property {0} is marked as Visibility.Content but it is returning null.", property.Name); string name = manager.GetName(value); if (name is null) { name = value.GetType().FullName; } manager.ReportError(new CodeDomSerializerException(string.Format(SR.SerializerNullNestedProperty, name, property.Name), manager)); } else { serializer = (CodeDomSerializer)manager.GetSerializer(propertyValue.GetType(), typeof(CodeDomSerializer)); if (serializer != null) { // Create a property reference expression and push it on the context stack. // This allows the serializer to gain some context as to what it should be // serializing. CodeExpression target = SerializeToExpression(manager, value); if (target is null) { CodeDomSerializer.TraceWarning("Unable to convert value to expression object"); } else { CodeExpression propertyRef = null; if (isExtender) { CodeDomSerializer.Trace("Content property is an extender."); ExtenderProvidedPropertyAttribute exAttr = (ExtenderProvidedPropertyAttribute)property.Attributes[typeof(ExtenderProvidedPropertyAttribute)]; // Extender properties are method invokes on a target "extender" object. // CodeExpression extender = SerializeToExpression(manager, exAttr.Provider); CodeExpression extended = SerializeToExpression(manager, value); CodeDomSerializer.TraceWarningIf(extender is null, "Extender object {0} could not be serialized.", manager.GetName(exAttr.Provider)); CodeDomSerializer.TraceWarningIf(extended is null, "Extended object {0} could not be serialized.", manager.GetName(value)); if (extender != null && extended != null) { CodeMethodReferenceExpression methodRef = new CodeMethodReferenceExpression(extender, "Get" + property.Name); CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression { Method = methodRef }; methodInvoke.Parameters.Add(extended); propertyRef = methodInvoke; } } else { propertyRef = new CodePropertyReferenceExpression(target, property.Name); } if (propertyRef != null) { ExpressionContext tree = new ExpressionContext(propertyRef, property.PropertyType, value, propertyValue); manager.Context.Push(tree); object result = null; try { SerializeAbsoluteContext absolute = (SerializeAbsoluteContext)manager.Context[typeof(SerializeAbsoluteContext)]; if (IsSerialized(manager, propertyValue, absolute != null)) { result = GetExpression(manager, propertyValue); } else { result = serializer.Serialize(manager, propertyValue); } } finally { Debug.Assert(manager.Context.Current == tree, "Serializer added a context it didn't remove."); manager.Context.Pop(); } if (result is CodeStatementCollection csc) { foreach (CodeStatement statement in csc) { statements.Add(statement); } } else { if (result is CodeStatement cs) { statements.Add(cs); } } } } } else { CodeDomSerializer.TraceError("Property {0} is marked as Visibilty.Content but there is no serializer for it.", property.Name); manager.ReportError(new CodeDomSerializerException(string.Format(SR.SerializerNoSerializerForComponent, property.PropertyType.FullName), manager)); } } }