public FieldAttribute( Type protoType, string protoFieldName, //Schema:Field string targetName = ANY_TARGET, object storeFlag = null, object key = null, object kind = null, object required = null, object visible = null, string valueList = null, object dflt = null, object min = null, object max = null, object minLength = null, object maxLength = null, object charCase = null, string backendName = null, string backendType = null, string description = null, string metadata = null, object nonUI = null, string formatRegExp = null, string formatDescr = null, string displayFormat = null ) : base(targetName, null) { if (protoType == null || protoFieldName.IsNullOrWhiteSpace()) { throw new CRUDException(StringConsts.ARGUMENT_ERROR + "FieldAttr.ctor(protoType|protoFieldName=null|empty)"); } try { var schema = Schema.GetForTypedRow(protoType); var protoTargetName = targetName; var segs = protoFieldName.Split(':'); if (segs.Length > 1) { protoTargetName = segs[0].Trim(); protoFieldName = segs[1].Trim(); } if (protoTargetName.IsNullOrWhiteSpace()) { throw new Exception("Wrong target syntax"); } if (protoFieldName.IsNullOrWhiteSpace()) { throw new Exception("Wrong field syntax"); } var protoFieldDef = schema[protoFieldName]; if (protoFieldDef == null) { throw new Exception("Prototype '{0}' field '{1}' not found".Args(protoType.FullName, protoFieldName)); } var protoAttr = protoFieldDef[protoTargetName]; try { StoreFlag = storeFlag == null? protoAttr.StoreFlag : (StoreFlag)storeFlag; BackendName = backendName == null? protoAttr.BackendName : backendName; BackendType = backendType == null? protoAttr.BackendType : backendType; Key = key == null? protoAttr.Key : (bool)key; Kind = kind == null? protoAttr.Kind : (DataKind)kind; Required = required == null? protoAttr.Required : (bool)required; Visible = visible == null? protoAttr.Visible : (bool)visible; Min = min == null? protoAttr.Min : min; Max = max == null? protoAttr.Max : max; Default = dflt == null? protoAttr.Default : dflt; MinLength = minLength == null? protoAttr.MinLength : (int)minLength; MaxLength = maxLength == null? protoAttr.MaxLength : (int)maxLength; CharCase = charCase == null? protoAttr.CharCase : (CharCase)charCase; ValueList = valueList == null? protoAttr.ValueList : valueList; Description = description == null? protoAttr.Description : description; NonUI = nonUI == null? protoAttr.NonUI : (bool)nonUI; FormatRegExp = formatRegExp == null? protoAttr.FormatRegExp: formatRegExp; FormatDescription = formatDescr == null? protoAttr.FormatDescription: formatDescr; DisplayFormat = displayFormat == null? protoAttr.DisplayFormat : displayFormat; if (metadata.IsNullOrWhiteSpace()) { m_MetadataContent = protoAttr.m_MetadataContent; } else if (protoAttr.m_MetadataContent.IsNullOrWhiteSpace()) { m_MetadataContent = metadata; } else { var conf1 = ParseMetadataContent(protoAttr.m_MetadataContent); var conf2 = ParseMetadataContent(metadata); var merged = new LaconicConfiguration(); merged.CreateFromMerge(conf1, conf2); m_MetadataContent = merged.SaveToString(); } } catch (Exception err) { throw new Exception("Invalid assignment of prototype override value: " + err.ToMessageWithType()); } } catch (Exception error) { throw new CRUDException(StringConsts.CRUD_FIELD_ATTR_PROTOTYPE_CTOR_ERROR.Args(error.Message)); } }
private Schema(Type trow) { lock (s_TypeLatch) { if (s_TypeLatch.Contains(trow)) { throw new CRUDException(StringConsts.CRUD_TYPED_ROW_RECURSIVE_FIELD_DEFINITION_ERROR.Args(trow.FullName)); } s_TypeLatch.Add(trow); try { m_Name = trow.AssemblyQualifiedName; var tattrs = trow.GetCustomAttributes(typeof(TableAttribute), false).Cast <TableAttribute>(); m_TableAttrs = new List <TableAttribute>(tattrs); m_FieldDefs = new OrderedRegistry <FieldDef>(); var props = GetFieldMembers(trow); var order = 0; foreach (var prop in props) { var fattrs = prop.GetCustomAttributes(typeof(FieldAttribute), false) .Cast <FieldAttribute>() .ToArray(); //20160318 DKh. Interpret [Field(CloneFromType)] for (var i = 0; i < fattrs.Length; i++) { var attr = fattrs[i]; if (attr.CloneFromRowType == null) { continue; } if (fattrs.Length > 1) { throw new CRUDException(StringConsts.CRUD_TYPED_ROW_SINGLE_CLONED_FIELD_ERROR.Args(trow.FullName, prop.Name)); } var clonedSchema = Schema.GetForTypedRow(attr.CloneFromRowType); var clonedDef = clonedSchema[prop.Name]; if (clonedDef == null) { throw new CRUDException(StringConsts.CRUD_TYPED_ROW_CLONED_FIELD_NOTEXISTS_ERROR.Args(trow.FullName, prop.Name)); } fattrs = clonedDef.Attrs.ToArray();//replace these attrs from the cloned target break; } var fdef = new FieldDef(prop.Name, order, prop.PropertyType, fattrs, prop); m_FieldDefs.Register(fdef); order++; } s_TypedRegistry.Register(this); m_TypedRowType = trow; } finally { s_TypeLatch.Remove(trow); } } //lock }
/// <summary> /// Table factory for a typed row T /// </summary> public static Table Create <T>() where T : TypedRow { return(new Table(Schema.GetForTypedRow(typeof(T)))); }