Ejemplo n.º 1
0
        /// <summary>
        /// Ordnet dem Namen einer Eigenschaft den zugehörigen Namen der Tabellenspalte in der Datenbanktabelle zu
        /// </summary>
        /// <param name="propName"></param>
        /// <returns></returns>
        public static string mapPropertyToColName(Type boRecordType, string propName)
        {
            if (typeof(TEntity) == typeof(TEntityView))
            {
                return(propName);
            }

            System.Reflection.PropertyInfo pinfo = boRecordType.GetProperty(propName);
#if (DEBUG)
            if (pinfo == null)
            {
                // Liste aller Eigenschaften
                Debug.WriteLine(boRecordType.Name + "-Eigenschaften:");
                foreach (System.Reflection.PropertyInfo pi in boRecordType.GetProperties())
                {
                    Debug.WriteLine("  + hat Eigenschaft " + pi.Name);
                }
            }
#endif
            Debug.Assert(pinfo != null, "Die Eigenschaft " + propName + " existiert nicht im Business- Objekt " + boRecordType.Name);

            // Falls ein MapPropertytoColName- Attribut existiert, wird der Spaltenname aus diesem gelesen und
            // zurückgegeben, sonst der Name der Eigenschaft
            if (pinfo.GetCustomAttributes(typeof(mkoIt.Db.MapPropertyToColNameAttribute), false).Count() > 0)
            {
                mkoIt.Db.MapPropertyToColNameAttribute att = pinfo.GetCustomAttributes(typeof(mkoIt.Db.MapPropertyToColNameAttribute), false)[0] as mkoIt.Db.MapPropertyToColNameAttribute;
                return(att == null ? propName : att.ColName);
            }
            else
            {
                return(propName);
            }
        }
Ejemplo n.º 2
0
 //ALTER TABLE tool ADD ID INT UNSIGNED
 /// <summary>
 /// If mysqltable attribute is used and the columnname is defined, then this columnname will be returned. if not defined the name of the property will be returned.
 /// If neither of the properties use the mysqltable attribute, the name of the property will be returned, else exception will be thrown
 /// </summary>
 public static string GetPropertyDatabaseColumnName(System.Reflection.PropertyInfo property, System.Reflection.PropertyInfo[] properties)
 {
     if (GetUsesProperties(properties))
     {
         if (GetHasProperty(property))
         {
             if (string.IsNullOrEmpty(((MySqlColumnAttribute)property.GetCustomAttributes(false).First(n => n.GetType() == typeof(MySqlColumnAttribute))).DatabaseColumnName))
             {
                 return(property.Name);
             }
             else
             {
                 return(((MySqlColumnAttribute)property.GetCustomAttributes(false).First(n => n.GetType() == typeof(MySqlColumnAttribute))).DatabaseColumnName);
             }
         }
         else
         {
             throw new Exception("This object uses property attributes but not this property");
         }
     }
     else
     {
         return(property.Name);
     }
 }
Ejemplo n.º 3
0
        private static ModelMapping CreateModelMappingFrom(System.Reflection.PropertyInfo propertyInfo)
        {
            var displayAttribute = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() as DisplayAttribute;

            return(new ModelMapping()
            {
                IsRequired = propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), true).Any(),
                PropertyInfo = propertyInfo,
                ModelPropertyName = propertyInfo.Name,
                SourceKeyName = displayAttribute?.Name ?? propertyInfo.Name,
            });
        }
Ejemplo n.º 4
0
        private DictionaryItem getAttributes(System.Reflection.PropertyInfo info)
        {
            object[]       attributes = info.GetCustomAttributes(true);
            DictionaryItem DI         = new DictionaryItem(info.Name);

            DI.sortable     = false;
            DI.displayOrder = 0;
            DI.displayName  = DI.variableName;

            foreach (Object att in attributes)
            {
                if (att is DisplayNameAttribute)
                {
                    DI.displayName = ((DisplayNameAttribute)att).DisplayName;
                }
                if (att is SortableAttribute)
                {
                    DI.sortable = ((SortableAttribute)att).value;
                }
                if (att is DisplayOrderAttribute)
                {
                    DI.displayOrder = ((DisplayOrderAttribute)att).value;
                }
            }
            return(DI);
        }
Ejemplo n.º 5
0
        private ColumnDescriptor getAttributes(System.Reflection.PropertyInfo info)
        {
            object[]         attributes = info.GetCustomAttributes(true);
            ColumnDescriptor DI         = new ColumnDescriptor(info.Name);

            DI.sortable     = false;
            DI.displayOrder = 0;
            DI.displayName  = null;

            foreach (Object att in attributes)
            {
                if (att is DisplayNameAttribute)
                {
                    DI.displayName = ((DisplayNameAttribute)att).DisplayName;
                }
                if (att is SortableAttribute)
                {
                    DI.sortable = ((SortableAttribute)att).value;
                }
                if (att is DisplayOrderAttribute)
                {
                    DI.displayOrder = ((DisplayOrderAttribute)att).value;
                }
            }
            if (DI.displayName == null)
            {
                throw new InvalidOperationException("Column does not have display parameters");
            }
            return(DI);
        }
Ejemplo n.º 6
0
        public static string PropertyAbbreviation(this object obj, string propName)
        {
            if (obj == null)
            {
                Compute.RecordError("Cannot query the property abbreviation of a null object.");
                return("");
            }

            if (propName == null)
            {
                Compute.RecordError("Cannot query the property abbreviation where the property name is null.");
                return("");
            }

            System.Reflection.PropertyInfo prop = obj.GetType().GetProperty(propName);

            if (prop != null)
            {
                object[] attributes = prop.GetCustomAttributes(typeof(AbbreviationAttribute), false);
                if (attributes.Length == 1)
                {
                    AbbreviationAttribute attribute = (AbbreviationAttribute)attributes[0];
                    if (attribute != null)
                    {
                        return(attribute.Name);
                    }
                }
            }

            return("");
        }
Ejemplo n.º 7
0
        public IModelBinder GetBinder(ModelBinderProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Metadata.IsComplexType)
            {
                string propName = context.Metadata.PropertyName;
                System.Reflection.PropertyInfo propInfo = context.Metadata.ContainerType?.GetProperty(propName);
                if (propName == null || propInfo == null)
                {
                    return(null);
                }

                // Look for FromJson attributes
                object attribute = propInfo.GetCustomAttributes(typeof(FromJsonAttribute), false).FirstOrDefault();
                if (attribute != null)
                {
                    return(new JsonModelBinder(context.Metadata.ModelType, attribute as IJsonAttribute));
                }
            }
            return(null);
        }
Ejemplo n.º 8
0
        static string GetPersitentName(System.Reflection.PropertyInfo propertyInfo)
        {
            var a = propertyInfo.GetCustomAttributes(typeof(PersistedNameAttribute), false)
                    .Cast <PersistedNameAttribute>().FirstOrDefault();

            return(a != null ? a.Name : propertyInfo.Name);
        }
Ejemplo n.º 9
0
        private KeyTypeAttributeState GetKeyTypeAttributeState(System.Reflection.PropertyInfo pi)
        {
            KeyTypeAttributeState state = new KeyTypeAttributeState();

            CustomAttributeField[] arr = (CustomAttributeField[])pi.GetCustomAttributes(typeof(CustomAttributeField), false);
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].KeyType == KeyTypeAttribute.PrimaryKey)
                {
                    state.IsPrimaryKey = true;
                }
                else if (arr[i].KeyType == KeyTypeAttribute.ForeignKey)
                {
                    state.IsForeignKey = true;
                }
                else if (arr[i].KeyType == KeyTypeAttribute.QueryField)
                {
                    state.IsQueryField = true;
                }
                else if (arr[i].KeyType == KeyTypeAttribute.OmitIfIsNull)
                {
                    state.IsOmitIfIsNull = true;
                }
            }
            return(state);
        }
Ejemplo n.º 10
0
        private DataTable _CreateDT(Stack <ParentKeysInfo> parentKeys, System.Reflection.PropertyInfo pro, string tbName, Type subType, string parentName)
        {
            DataTable subTb;

            subTb = new DataTable(tbName);
            mDataset.Tables.Add(subTb);

            mTableNameToOwnerTabel[tbName] = parentName;
            if (pro.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true).Length > 0)
            {
                mTableNameToDescrip[tbName] = (pro.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true)[0] as System.ComponentModel.DisplayNameAttribute).DisplayName;
            }
            else
            {
                mTableNameToDescrip[tbName] = "";
            }

            mTableNameToTypeName[tbName] = subType.ToString();

            DataColumn column;
            var        ar = parentKeys.ToArray();

            for (int i = parentKeys.Count - 1; i >= 0; i--)
            {
                var pkey = ar[i];
                column            = new DataColumn();
                column.DataType   = pkey.value.GetType();
                column.ColumnName = pkey.KeyName;
                column.Caption    = pkey.Descrip;
                column.ReadOnly   = false;
                column.Unique     = false;
                subTb.Columns.Add(column);
            }

            column            = new DataColumn();
            column.DataType   = typeof(Int32);
            column.ColumnName = "@index";
            column.Caption    = "序号";
            column.ReadOnly   = false;
            column.Unique     = false;
            subTb.Columns.Add(column);

            _InitColumn(subType, subTb, null);
            return(subTb);
        }
Ejemplo n.º 11
0
 public bool MapPropertyToColumn(System.Reflection.PropertyInfo pi, ref string columnName, ref bool resultColumn)
 {
     if (pi.GetCustomAttributes(typeof(PetaPoco.ColumnAttribute), true).Length == 0)
     {
         columnName = ToDBName(columnName);
         return(true);
     }
     return(false);
 }
Ejemplo n.º 12
0
 public static T GetCustomAttribute <T>(this System.Reflection.PropertyInfo propery, bool inherit) where T : Attribute
 {
     object[] objs = propery.GetCustomAttributes(typeof(T), inherit);
     if (objs == null || objs.Length == 0)
     {
         return(null);
     }
     return(objs[0] as T);
 }
 public static JsonFieldTypes GetFieldFlag(System.Reflection.PropertyInfo pro)
 {
     object[] atts = pro.GetCustomAttributes(typeof(JsonFieldAttribute), false);
     if (atts.Length > 0)
     {
         JsonFieldAttribute attr = atts[0] as JsonFieldAttribute;
         return(attr.FieldTypeName);
     }
     return(JsonFieldTypes.Null);
 }
Ejemplo n.º 14
0
        public static T GetCustomAttribute <T>(this System.Reflection.PropertyInfo property)
        {
            var attrs = property.GetCustomAttributes(typeof(T), true);

            if (attrs?.Length != 1)
            {
                return(default(T));
            }
            return((T)attrs.FirstOrDefault());
        }
Ejemplo n.º 15
0
 public static string GetFieldFlag(System.Reflection.PropertyInfo pro)
 {
     object[] atts = pro.GetCustomAttributes(typeof(ConfigAttribute), false);
     if (atts.Length > 0)
     {
         ConfigAttribute attr = atts[0] as ConfigAttribute;
         return(attr._fieldFlag);
     }
     return(null);
 }
Ejemplo n.º 16
0
        private static string GetAttributeDisplayName(System.Reflection.PropertyInfo property)
        {
            var atts = property.GetCustomAttributes(typeof(DisplayAttribute), true);

            if (atts is null || atts.Length == 0)
            {
                return(property.Name);
            }
            return((atts[0] as DisplayAttribute).GetName());
        }
Ejemplo n.º 17
0
        private static string GetDisplayName(System.Reflection.PropertyInfo property)
        {
            string        result        = null;
            BaseAttribute baseAttribute = property.GetCustomAttributes(typeof(BaseAttribute), true).FirstOrDefault <object>() as BaseAttribute;

            if (baseAttribute != null)
            {
                result = baseAttribute.DisplayName;
            }
            return(result);
        }
Ejemplo n.º 18
0
        // Helper method to return the Category string from a
        // CategoryAttribute assigned to a property exposed by
        //the specified object
        private static string GetCategory(object source, string propertyName)
        {
            System.Reflection.PropertyInfo property = source.GetType().GetProperty(propertyName);
            CategoryAttribute attribute             = (CategoryAttribute)property.GetCustomAttributes(typeof(CategoryAttribute), false)[0];

            if (attribute == null)
            {
                return(null);
            }
            return(attribute.Category);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// return Business Type Metadata
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public string GetClassType(System.Reflection.PropertyInfo p)
        {
            var attr = p.GetCustomAttributes(typeof(BusinessTypeAttribute), false).FirstOrDefault();

            if (attr == null || !(attr is BusinessTypeAttribute))
            {
                return("");
            }

            return((attr as BusinessTypeAttribute).ClassType);
        }
Ejemplo n.º 20
0
 private string GetDisplayName(System.Reflection.PropertyInfo propertyInfo)
 {
     if (propertyInfo != null)
     {
         var propertyCustomDisplayNameAttribute = propertyInfo.GetCustomAttributes(typeof(Ifc4.Attributes.CustomDisplayNameAttribute), false).SingleOrDefault() as Ifc4.Attributes.CustomDisplayNameAttribute;
         if (propertyCustomDisplayNameAttribute != null)
         {
             return(propertyCustomDisplayNameAttribute.DisplayName);
         }
     }
     return(null);
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Return the TGIBlockListContentField value for a Content Field.
 /// </summary>
 /// <param name="t">Type on which Content Field exists.</param>
 /// <param name="index">Content Field name.</param>
 /// <returns>The value of the TGIBlockListContentFieldAttribute TGIBlockListContentField field, if found;
 /// otherwise <c>null</c>.</returns>
 public static string GetTGIBlockListContentField(Type t, string index)
 {
     System.Reflection.PropertyInfo pi = t.GetProperty(index);
     if (pi != null)
     {
         foreach (Attribute attr in pi.GetCustomAttributes(typeof(TGIBlockListContentFieldAttribute), true))
         {
             return((attr as TGIBlockListContentFieldAttribute).TGIBlockListContentField);
         }
     }
     return(null);
 }
Ejemplo n.º 22
0
        public Column(System.Reflection.PropertyInfo propertyInfo)
        {
            bool isPrimaryKey = propertyInfo.Name.ToLower() == "id";

            bool isRequired = isPrimaryKey ||
                              propertyInfo.IsDefined(typeof(RobotBlocks.Annotations.Constraint.Field.Required), false) ||
                              !Column.CanBeNull(propertyInfo.PropertyType);
            int?maximumLength = null;

            object[] lengthAttributes = propertyInfo.GetCustomAttributes(typeof(RobotBlocks.Annotations.Constraint.Field.Length), false);
            foreach (RobotBlocks.Annotations.Constraint.Field.Length lengthConstraint in lengthAttributes)
            {
                if (lengthConstraint != null)
                {
                    maximumLength = lengthConstraint.Maximum;
                    break;
                }
            }

            bool isUnique = propertyInfo.IsDefined(typeof(RobotBlocks.Annotations.Constraint.Field.Unique), false);

            string defaultValue = null;

            object[] defaultAttributes = propertyInfo.GetCustomAttributes(typeof(RobotBlocks.Annotations.Constraint.Field.Default), false);
            foreach (RobotBlocks.Annotations.Constraint.Field.Default defaultConstraint in defaultAttributes)
            {
                if (defaultConstraint != null)
                {
                    defaultValue = defaultConstraint.Value;
                    break;
                }
            }
            Name = propertyInfo.Name;
            UnderlyingSystemType = propertyInfo.PropertyType.UnderlyingSystemType;
            Length       = maximumLength;
            IsRequired   = isRequired;
            IsPrimaryKey = isPrimaryKey;
            IsUnique     = isUnique;
            DefaultValue = defaultValue;
        }
Ejemplo n.º 23
0
        public static Schema GetAttributeDetails(Schema schema, System.Reflection.PropertyInfo propInfo)
        {
            if (schema == null || propInfo == null)
            {
                return(schema);
            }
            foreach (var attribute in propInfo.GetCustomAttributes(false))
            {
                SetSchemaDetails(schema, attribute);
            }

            return(schema);
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Gets the first Attribute of given type in the given property
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="propertyInfo"></param>
 /// <returns></returns>
 public static ParametersDefinition GetProperty <ParametersDefinition>(this System.Reflection.PropertyInfo propertyInfo)
     where ParametersDefinition : Attribute
 {
     try
     {
         var def = (ParametersDefinition)propertyInfo.GetCustomAttributes(typeof(ParametersDefinition), false)[0];
         return(def);
     }
     catch (IndexOutOfRangeException)
     {
         throw new IndexOutOfRangeException($"Attribute {nameof(ParametersDefinition)} not found on property {propertyInfo.Name} on object {propertyInfo.DeclaringType.Name}");
     }
 }
Ejemplo n.º 25
0
 object GetArgumentValue(object value, System.Reflection.PropertyInfo protype)
 {
     if (protype.GetCustomAttributes(typeof(JSONAttribute), true).Length > 0
         ||
         protype.PropertyType.GetCustomAttributes(typeof(JSONAttribute), true).Length > 0)
     {
         return(JSON.Serialize(value, true));
     }
     else
     {
         return(value);
     }
 }
Ejemplo n.º 26
0
        private static bool IsWriteable(System.Reflection.PropertyInfo pi)
        {
            var attributes = pi.GetCustomAttributes(typeof(WriteAttribute), false).AsList();

            if (attributes.Count != 1)
            {
                return(true);
            }

            var writeAttribute = (WriteAttribute)attributes[0];

            return(writeAttribute.Write);
        }
Ejemplo n.º 27
0
 private static System.Reflection.PropertyInfo FindProperty(System.Reflection.PropertyInfo targetField, ref object localSource)
 {
     System.Reflection.PropertyInfo sourceProperty = null;
     foreach (var Attribute in targetField.GetCustomAttributes(true).OfType <CopyFromAttribute>())
     {
         sourceProperty = FindProperty(sourceProperty, Attribute.getPath().Split('.'), ref localSource);
         if (sourceProperty != null)
         {
             break;
         }
     }
     return(sourceProperty);
 }
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var valueProviderResult =
                bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (valueProviderResult == ValueProviderResult.None)
            {
                return(Task.CompletedTask);
            }

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

            System.Reflection.PropertyInfo propInfo = bindingContext.ModelMetadata.ContainerType.GetProperty(bindingContext.ModelMetadata.PropertyName);
            if (propInfo == null)
            {
                return(Task.CompletedTask);
            }

            var attribute = propInfo.GetCustomAttributes(typeof(OnlyEnumAttribute), false).FirstOrDefault() as OnlyEnumAttribute;

            if (attribute == null)
            {
                return(Task.CompletedTask);
            }

            if (!EnumValidator.Validate(attribute.EnumType, valueProviderResult.ToString(), out object parsedEnum))
            {
                bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, string.Format("Object \"{0}\" of type {1} is not a {2} type", valueProviderResult.ToString(), "String", attribute.EnumType.Name));
            }
            else
            {
                if (propInfo.PropertyType == typeof(string))
                {
                    bindingContext.Result = ModelBindingResult.Success(parsedEnum.ToString());
                }
                else if (propInfo.PropertyType == typeof(int) || Nullable.GetUnderlyingType(propInfo.PropertyType) == typeof(int))
                {
                    bindingContext.Result = ModelBindingResult.Success((int)parsedEnum);
                }
                else if (propInfo.PropertyType == attribute.EnumType || Nullable.GetUnderlyingType(propInfo.PropertyType) == attribute.EnumType)
                {
                    bindingContext.Result = ModelBindingResult.Success(parsedEnum);
                }
                else
                {
                    bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, string.Format("Parameter of type {0} must be declared as an Integer, String or {1} type.", propInfo.PropertyType.Name, attribute.EnumType.Name));
                }
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Return the ElementPriority value for a Content Field.
        /// </summary>
        /// <param name="t">Type on which Content Field exists.</param>
        /// <param name="index">Content Field name.</param>
        /// <returns>The value of the ElementPriorityAttribute Priority field, if found;
        /// otherwise Int32.MaxValue.</returns>
        public static Int32 GetPriority(Type t, string index)
        {
            System.Reflection.PropertyInfo pi = t.GetProperty(index);

            if (pi != null)
            {
                foreach (var attr in pi.GetCustomAttributes(typeof(ElementPriorityAttribute), true))
                {
                    return((attr as ElementPriorityAttribute).Priority);
                }
            }

            return(Int32.MaxValue);
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Return the DataGridExpandable value for a Content Field.
        /// </summary>
        /// <param name="t">Type on which Content Field exists.</param>
        /// <param name="index">Content Field name.</param>
        /// <returns>The value of the DataGridExpandableAttribute DataGridExpandable field, if found;
        /// otherwise <c>false</c>.</returns>
        public static bool GetDataGridExpandable(Type t, string index)
        {
            System.Reflection.PropertyInfo pi = t.GetProperty(index);

            if (pi != null)
            {
                foreach (var attr in pi.GetCustomAttributes(typeof(DataGridExpandableAttribute), true))
                {
                    return((attr as DataGridExpandableAttribute).DataGridExpandable);
                }
            }

            return(false);
        }