コード例 #1
0
ファイル: DataAttributes.cs プロジェクト: evgeniynet/APIBeta
        public static void CopyProperties <TSource>(TSource Source, object LinqDestination)
        {
            Type DestinationType = LinqDestination.GetType();
            Type SourceType      = typeof(TSource);

            System.Reflection.FieldInfo[] SourceFields = SourceType.GetFields();
            foreach (System.Reflection.FieldInfo SourceField in SourceFields)
            {
                //bool DataIgnore = false;
                object[] attr = SourceField.GetCustomAttributes(typeof(DataIgnoreAttrib), true);
                if (attr.Length > 0)
                {
                    continue;                 //DataIgnore = true;
                }
                string DestPropName = SourceField.Name;
                attr = SourceField.GetCustomAttributes(typeof(DataNameAttrib), true);
                if (attr.Length > 0)
                {
                    DestPropName = ((DataNameAttrib)attr[0]).DataName;
                }

                System.Reflection.PropertyInfo DestProp = DestinationType.GetProperty(DestPropName);
                if (DestProp == null)
                {
                    continue;
                }
                //if ((!DestProp.PropertyType.FullName.StartsWith("System.")) && DataIgnore) continue;
                object v = SourceField.GetValue(Source);
                if (v != null)
                {
                    v = Convert.ChangeType(v, DestProp.PropertyType);
                }
                DestProp.SetValue(LinqDestination, v, null);
            }
        }