public static object GetObject(string caseString, Type genericType) { object result; if (!genericType.IsValueType && caseString == FlowSwitchNullCaseKeyIdentifier) { result = null; } else if (typeof(string).IsAssignableFrom(genericType)) { if (caseString == FlowSwitchEmptyCaseKeyIdentifier) { result = string.Empty; } else { result = caseString; } } else { //If target type is value type and the caseString is null, we should leave converter to process it. //If target type is reference type, the caseString is a non-null value here. result = XamlUtilities.GetConverter(genericType).ConvertFromString(caseString); } return(result); }
internal static string GetExpressionString(Activity expression, ParserContext context) { string expressionString = null; if (expression != null) { Type expressionType = expression.GetType(); Type expressionArgumentType = expressionType.IsGenericType ? expressionType.GetGenericArguments()[0] : typeof(object); bool isLiteral = expressionType.IsGenericType ? Type.Equals(typeof(Literal <>), expressionType.GetGenericTypeDefinition()) : false; //handle ITextExpression if (expression is ITextExpression) { ITextExpression textExpression = expression as ITextExpression; expressionString = textExpression.ExpressionText; } //handle Literal Expression else if (isLiteral) { TypeConverter converter = XamlUtilities.GetConverter(expressionArgumentType); if (converter != null && converter.CanConvertTo(context, typeof(string))) { PropertyInfo literalValueProperty = expressionType.GetProperty("Value"); Fx.Assert(literalValueProperty != null && literalValueProperty.GetGetMethod() != null, "Literal<T> must have the Value property with a public get accessor."); object literalValue = literalValueProperty.GetValue(expression, null); string convertedString = null; if (literalValue != null) { try { convertedString = converter.ConvertToString(context, literalValue); } catch (ArgumentException) { convertedString = literalValue.ToString(); } } expressionString = expressionArgumentType == typeof(string) ? ("\"" + convertedString + "\"") : convertedString; } } else if (expressionType.IsGenericType && (expressionType.GetGenericTypeDefinition() == typeof(VariableValue <>) || expressionType.GetGenericTypeDefinition() == typeof(VariableReference <>))) { PropertyInfo variableProperty = expression.GetType().GetProperty("Variable"); Variable variable = variableProperty.GetValue(expression, null) as Variable; if (variable != null) { expressionString = variable.Name; } } } return(expressionString); }
public void OnValueChanged() { if (this.Value is ModelItem) { // Since Value is a DP, this code will trigger OnValueChanged once more. this.Value = ((ModelItem)this.Value).GetCurrentValue(); return; } if (this.DataTemplateName != LabelTemplate && !this.IsBoxOnly) { this.DataTemplateName = LabelTemplate; } if (this.DisplayHintText) { this.Text = string.Empty; return; } if (this.ValueType == null) { return; } if (this.ValueType.IsValueType) { if (this.Value == null) { this.Value = Activator.CreateInstance(this.ValueType); } } if (this.Value == null) { this.Text = Null; } else if ((this.ValueType == typeof(string)) && string.Equals(this.Value, String.Empty)) { this.Text = Empty; } else { TypeConverter converter = XamlUtilities.GetConverter(this.ValueType); Fx.Assert(converter != null, "TypeConverter is not available"); try { this.Text = converter.ConvertToString(this.Value); } catch (ArgumentException) { this.Text = this.Value.ToString(); } } }
object ResolveInputText() { object result = null; if (this.ValueType == typeof(string)) { if (this.Text.Equals(Null)) { result = null; } else if (this.Text.Equals(Empty)) { result = string.Empty; } else { result = this.Text; } } else if (!this.ValueType.IsValueType && this.Text.Equals(Null)) { result = null; } else { TypeConverter converter = XamlUtilities.GetConverter(this.ValueType); Fx.Assert(converter != null, "TypeConverter is not available"); if (!converter.CanConvertFrom(typeof(string)) || !converter.CanConvertTo(typeof(string))) { throw FxTrace.Exception.AsError(new NotSupportedException(SR.NotSupportedCaseKeyStringConversion)); } result = converter.ConvertFromString(this.Text); // See if the result can be converted back to a string. // For example, we have a enum Color {Black, White}. // String "3" can be converted to integer 3, but integer 3 // cannot be converted back to a valid string for enum Color. // In this case, we disallow string "3". converter.ConvertToString(result); } string reason; if (this.CaseKeyValidationCallback != null && !this.CaseKeyValidationCallback(result, out reason)) { throw FxTrace.Exception.AsError(new ArgumentException(reason)); } return(result); }
//Raw string means the null is not represented as "<null>" and string.Empty is not represented as "<empty>". static string GetRawString(object caseObject) { string result = null; if (caseObject == null) { return(null); } if (!(caseObject is string)) { result = XamlUtilities.GetConverter(caseObject.GetType()).ConvertToString(caseObject); } else { result = (string)caseObject; } return(result); }