private static void createEnum(PropertyInfo pi, Dictionary <Type, XmlSchemaType> xmlTypes, XmlSchema xmlSchema) { // Create enum var res = new XmlSchemaSimpleTypeRestriction(); res.BaseTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.NmToken).QualifiedName; foreach (var o in Enum.GetNames(pi.PropertyType)) { res.Facets.Add(new XmlSchemaEnumerationFacet { Value = (o.Substring(0, 1).ToLower() + o.Substring(1)) }); } XmlSchemaSimpleType st = new XmlSchemaSimpleType { Content = res }; // For flags must create a union of the values & string if (CustomAttributeHelper.Has <System.FlagsAttribute>(pi.PropertyType)) { XmlSchemaSimpleType st2 = new XmlSchemaSimpleType(); st2.Name = getXmlTypeName(pi.PropertyType); var union = new XmlSchemaSimpleTypeUnion(); XmlSchemaSimpleType st3 = new XmlSchemaSimpleType(); var res3 = new XmlSchemaSimpleTypeRestriction(); res3.BaseTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; st3.Content = res3; union.BaseTypes.Add(st); union.BaseTypes.Add(st3); st2.Content = union; xmlSchema.Items.Add(st2); xmlTypes[pi.PropertyType] = st2; } else { st.Name = getXmlTypeName(pi.PropertyType); xmlSchema.Items.Add(st); xmlTypes[pi.PropertyType] = st; } }
/// Return names of XML attributes associated with this property, or null if no attributes are found public static string[] GetNames(PropertyInfo pi, bool includingDeprecated) { var v = CustomAttributeHelper.All <XsAttributeAttribute>(pi); if (v != null && v.Length > 0) { int cnt = 0; for (int i = 0; i < v.Length; ++i) { if (includingDeprecated || v[i].Deprecated == false) { cnt++; } } if (cnt == 0) { return(null); } var ret = new string[cnt]; var n = 0; for (int i = 0; i < v.Length; ++i) { if (includingDeprecated || v[i].Deprecated == false) { ret[n++] = v[i].Name; } } return(ret); } if (CustomAttributeHelper.Has <XmlIgnoreAttribute>(pi) || !pi.CanRead || !pi.CanWrite || pi.GetIndexParameters().Length != 0 || pi.GetSetMethod() == null) { return(null); } if (CustomAttributeHelper.Has <XsElementAttribute>(pi)) { return(null); } return(new string[] { (pi.Name.Substring(0, 1).ToLowerInvariant() + pi.Name.Substring(1)) }); }
private static object convertString(string text, Type pt) { if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable <>)) { if (String.IsNullOrEmpty(text)) { return(null); } return(convertString(text, pt.GetGenericArguments()[0])); } if (String.IsNullOrEmpty(text)) { if (pt.IsPrimitive || pt.IsEnum || pt == typeof(decimal)) { text = "0"; } } if (pt == typeof(bool)) { int t; if (Int32.TryParse(text, out t)) { return(t != 0); } return(Boolean.Parse(text)); } if (pt == typeof(byte) || pt == typeof(sbyte) || pt == typeof(short) || pt == typeof(ushort) || pt == typeof(int) || pt == typeof(uint) || pt == typeof(long) || pt == typeof(ulong) || pt == typeof(decimal) || pt == typeof(float) || pt == typeof(double)) { object o = ParsingReader.TryParseNumber(text); if (o == null) { throw new InvalidCastException("Cannot cast '" + text + "' to " + pt.FullName); } return(Convert.ChangeType(o, pt)); } if (pt == typeof(char?)) { if (string.IsNullOrEmpty(text)) { return(null); } return(text[0]); } if (pt == typeof(char)) { return(text[0]); } if (pt == typeof(DateTime)) { return(DateTime.Parse(text)); } if (pt == typeof(Guid)) { return(new Guid(text)); } if (pt == typeof(string)) { return(text); } if (pt.IsEnum) { bool hasFlags = CustomAttributeHelper.Has <FlagsAttribute>(pt); bool hasDigits = !string.IsNullOrEmpty(text) && text.IndexOfAny("0123456789".ToCharArray()) != -1; bool isempty = string.IsNullOrEmpty(text); if (isempty || hasFlags || hasDigits) { long val = 0; var names = Enum.GetNames(pt); var dictionary = new Dictionary <string, long>(StringComparer.OrdinalIgnoreCase); var values = Enum.GetValues(pt); for (int i = 0; i < names.Length; ++i) { long vv = (long)Convert.ChangeType(values.GetValue(i), typeof(long)); dictionary[names[i]] = vv; if (isempty && vv == 0) { return(Enum.ToObject(pt, vv)); } } if (isempty) { throw new InvalidCastException(String.Format("Unexpected empty enum value")); } int step = 0; foreach (string str in text.Split(s_enumDelimiters)) { if (String.IsNullOrEmpty(str)) { continue; } step++; if (!hasFlags && step > 1) { throw new InvalidCastException(String.Format("Unexpected enum value {0}", str)); } long v; if (char.IsDigit(str[0])) { val |= ParsingReader.ParseNumber <long>(str); } else if (dictionary.TryGetValue(str, out v)) { val |= v; } else { throw new InvalidCastException(String.Format("Unexpected enum value {0}", str)); } } return(Enum.ToObject(pt, val)); } return(Enum.Parse(pt, text, true)); } throw new InvalidCastException(String.Format("'{0}' cannot be converted to {1}", text, pt.ToString())); }
/// <summary> /// Initialize action /// </summary> public virtual void Initialize() { foreach (PropertyInfo c in GetType().GetProperties()) { bool valSet = false; object val = null; foreach (XsElementAttribute e in CustomAttributeHelper.All <XsElementAttribute>(c)) { if (!valSet) { val = c.GetValue(this, null); valSet = true; } ScriptActionBase bl = val as ScriptActionBase; if (bl != null && bl._elementName == null) { bl._elementName = e.Name; } continue; } if (CustomAttributeHelper.Has <XsRequiredAttribute>(c)) { if (!valSet) { if (c.GetIndexParameters().Length > 0) { continue; } val = c.GetValue(this, null); } if (val == null) { throw new ParsingException(string.Format("Required attribute '{0}' is not set", XsAttributeAttribute.GetNames(c, false)[0])); } } if ((c.PropertyType == typeof(object) || c.PropertyType == typeof(string)) && !CustomAttributeHelper.Has <XsNotTransformed>(c) && !CustomAttributeHelper.Has <XmlIgnoreAttribute>(c) && c.GetSetMethod() != null) { if (!valSet) { if (c.GetIndexParameters().Length > 0) { continue; } val = c.GetValue(this, null); } if (val != null) { if (val is string) { Context.AssertGoodTransform((string)val, Transform); } else { Context.AssertGoodTransform(val.ToString(), Transform); } } } } }