Ejemplo n.º 1
0
        public static TField GetValue <TField>(object tdoc, string keyname)
        {
            TField ret = default(TField);

            if (tdoc == null)
            {
                return(ret);
            }
            Type       t  = tdoc.GetType();
            MemberInfo mi = t.GetProperty(keyname);

            if (mi != null)
            {
                object val = (mi as PropertyInfo).GetValue(tdoc);
                if (val == null)
                {
                    return(ret);
                }
                ret = ConvertionExtensions.ConvertTo <TField>(val as IConvertible);
                return(ret);
            }
            else
            {
                mi = t.GetField(keyname);
                object val = (mi as FieldInfo).GetValue(tdoc);
                if (val == null)
                {
                    return(ret);
                }
                ret = ConvertionExtensions.ConvertTo <TField>(val as IConvertible);
                return(ret);
            }
        }
Ejemplo n.º 2
0
 public static bool SetValue <TDocument, TSubDocument, TField>(TDocument Ptdoc, Func <TDocument, TSubDocument> func, string keyname, TField Newval)
 {
     try
     {
         if (Ptdoc == null)
         {
             return(false);
         }
         TSubDocument tdoc = func(Ptdoc);
         if (tdoc == null)
         {
             tdoc = CreateInstance <TSubDocument>();
         }
         Type       t   = tdoc.GetType();
         object     val = ConvertionExtensions.ConvertTo <TField>(Newval as IConvertible);
         MemberInfo mi  = t.GetProperty(keyname);
         if (mi != null)
         {
             (mi as PropertyInfo).SetValue(tdoc, val);
             return(true);
         }
         else
         {
             mi = t.GetField(keyname);
             (mi as FieldInfo).SetValue(tdoc, val);
             return(true);
         }
     }
     catch (Exception ce)
     {
         LogLib.LogableClass.ToLog("ConvertionExtensions填充数据错误!", ce.Message);
     }
     return(false);
 }
Ejemplo n.º 3
0
        public bool Compr(BsonElement bs)
        {
            Type        t   = this.GetType();
            BsonElement key = bs;
            BsonValue   val = bs.Value;

            return(ConvertionExtensions.Equal(this, bs.Name, getBstrVal, bs.Value));
        }
Ejemplo n.º 4
0
        public List <TField> Loc <TField>(int[] indies, string FieldName)
        {
            List <TField> ret = new List <TField>();

            foreach (int i in indies)
            {
                T doc = this[i];
                ret.Add(ConvertionExtensions.GetValue <T, TField>(doc, FieldName));
            }
            return(ret);
        }
Ejemplo n.º 5
0
        public bool Compr(BsonDocument bs)
        {
            Type t = this.GetType();

            foreach (BsonElement key in bs)
            {
                string    name = key.Name;
                BsonValue val  = key.Value;
                if (!ConvertionExtensions.Equal(this, name, getBstrVal, val))
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 6
0
 protected void SetDefaultValueAttribute()
 {
     PropertyInfo[] pps = this.GetType().GetProperties();
     for (int i = 0; i < pps.Length; i++)
     {
         PropertyInfo pi = pps[i];
         //Attribute[] atts = Attribute.GetCustomAttributes(
         DefaultValueAttribute att = Attribute.GetCustomAttribute(pi as MemberInfo, typeof(DefaultValueAttribute), true) as DefaultValueAttribute;
         if (att != null)
         {
             if (pi.CanWrite)
             {
                 object val = ConvertionExtensions.ConvertTo((IConvertible)att.Value, pi.PropertyType);
                 pi.SetValue(this, val, null);
             }
         }
     }
 }
Ejemplo n.º 7
0
        public static TField GetValue <TDocument, TField>(Func <TDocument> keynameFunc, string keyname)
        {
            TDocument Ptdoc = keynameFunc();
            TField    ret   = default(TField);

            if (Ptdoc == null)
            {
                return(ret);
            }
            TDocument tdoc = Ptdoc;// keynameFunc(Ptdoc);

            if (tdoc == null)
            {
                return(ret);
            }
            Type       t  = tdoc.GetType();
            MemberInfo mi = t.GetProperty(keyname);

            if (mi != null)
            {
                object val = (mi as PropertyInfo).GetValue(tdoc);
                if (val == null)
                {
                    return(ret);
                }
                ret = ConvertionExtensions.ConvertTo <TField>(val as IConvertible);
                return(ret);
            }
            else
            {
                mi = t.GetField(keyname);
                if (mi == null)
                {
                    return(ret);
                }
                object val = (mi as FieldInfo).GetValue(tdoc);
                if (val == null)
                {
                    return(ret);
                }
                ret = ConvertionExtensions.ConvertTo <TField>(val as IConvertible);
                return(ret);
            }
        }
Ejemplo n.º 8
0
        public object ToType(Type conversionType, IFormatProvider provider)
        {
            object ret = Activator.CreateInstance(conversionType);
            Type   t   = conversionType;
            Type   myt = this.GetType();

            MemberInfo[] mis   = t.GetMembers();
            MemberInfo[] mymis = myt.GetMembers();
            Dictionary <string, MemberInfo> mydic = new Dictionary <string, MemberInfo>();

            for (int i = 0; i < mymis.Length; i++)
            {
                if (mymis[i] is PropertyInfo || mymis[i] is FieldInfo)
                {
                    if (!mydic.ContainsKey(mymis[i].Name))
                    {
                        mydic.Add(mymis[i].Name, mymis[i]);
                    }
                    else
                    {
                        Log("错误", "强制转换时成员名重复", mymis[i].Name);
                    }
                }
            }
            //mydic =  mymis.ToDictionary(a => a.Name,a=>a);
            for (int i = 0; i < mis.Length; i++)
            {
                MemberInfo mi = mis[i];
                if (mi is PropertyInfo || mi is FieldInfo)
                {
                    if (mi is PropertyInfo)
                    {
                        if (!(mi as PropertyInfo).CanWrite)//只读,跳过
                        {
                            continue;
                        }
                    }
                    if (mydic.ContainsKey(mis[i].Name))
                    {
                        MemberInfo mymi = mydic[mis[i].Name];
                        Type       mit  = null;
                        if (mi is PropertyInfo)
                        {
                            mit = (mi as PropertyInfo).PropertyType;
                        }
                        if (mi is FieldInfo)
                        {
                            mit = (mi as FieldInfo).FieldType;
                        }
                        if (mit == null)
                        {
                            continue;
                        }
                        object val = (mymi is PropertyInfo) ? (mymi as PropertyInfo).GetValue(this, null) : (mymi as FieldInfo).GetValue(this);
                        if (val == null)
                        {
                            continue;
                        }
                        if (val is IConvertible)
                        {
                            val = ConvertionExtensions.ConvertTo((IConvertible)val, mit);
                        }
                        else
                        {
                            continue;
                        }
                        //Log("自定义类型转换",string.Format("{0}={1}",mymi.Name,val));
                        if (mi is PropertyInfo)
                        {
                            (mi as PropertyInfo).SetValue(ret, val, null);
                        }
                        else
                        {
                            (mi as FieldInfo).SetValue(ret, val);
                        }
                    }
                }
            }
            return(ret);
        }
Ejemplo n.º 9
0
 public object Clone()
 {
     return(ConvertionExtensions.Clone(this));
 }
Ejemplo n.º 10
0
 public T Clone <T>() where T : MongoData
 {
     return(ConvertionExtensions.Clone <T>(this as T));
 }
Ejemplo n.º 11
0
        public static DataTable ToTable <T>(List <T> list, bool UseDisplayNameAsColumnName, bool OnlyDisplayDefName, ref Dictionary <string, MemberInfo> DisIs)
        {
            DataTable ret   = new DataTable();
            int       lasti = 0;

            try
            {
                MemberInfo[] pis = typeof(T).GetMembers();
                //List<string> Titles;
                if (DisIs == null)
                {
                    DisIs = new Dictionary <string, MemberInfo>();
                    for (int i = 0; i < pis.Length; i++)
                    {
                        if (pis[i].MemberType != MemberTypes.Property && pis[i].MemberType != MemberTypes.Field)
                        {
                            continue;
                        }
                        Type memtype = (pis[i].MemberType == MemberTypes.Property) ? (pis[i] as PropertyInfo).PropertyType : (pis[i] as FieldInfo).FieldType;
                        ////if (pis[i].GetType().IsClass)//如果是对象,不载入
                        ////    continue;
                        //if (pis[i] is PropertyInfo)
                        //{
                        if ((memtype.IsClass && memtype != typeof(string)))//类对象跳过
                        {
                            continue;
                        }
                        //}
                        DisplayNameAttribute DNA = Attribute.GetCustomAttribute(pis[i], typeof(DisplayNameAttribute)) as DisplayNameAttribute;
                        string strTitle          = pis[i].Name;
                        if (DNA != null && UseDisplayNameAsColumnName)
                        {
                            strTitle = DNA.DisplayName;
                        }
                        if (OnlyDisplayDefName && DNA == null)
                        {
                            continue;
                        }
                        if (DisIs.ContainsKey(strTitle))
                        {
                            ToLog("错误", "转换为表", string.Format("存在相同的关键字.{0}", strTitle));
                            continue;
                        }
                        DisIs.Add(strTitle, pis[i]);
                    }
                }
                foreach (string key in DisIs.Keys)
                {
                    Type memtype = (DisIs[key].MemberType == MemberTypes.Property) ? (DisIs[key] as PropertyInfo).PropertyType : (DisIs[key] as FieldInfo).FieldType;
                    Type genericTypeDefinition = memtype;
                    if (genericTypeDefinition.IsGenericType)
                    {
                        if (genericTypeDefinition.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            genericTypeDefinition = genericTypeDefinition.GetGenericArguments()[0];
                        }
                    }
                    DataColumn dc = new DataColumn(key, genericTypeDefinition);
                    ret.Columns.Add(dc);
                }
                for (int i = 0; i < list.Count; i++)
                {
                    lasti = i;
                    DataRow dr = ret.NewRow();
                    foreach (string key in DisIs.Keys)
                    {
                        T    obj     = (T)list[i];
                        Type memtype = (DisIs[key].MemberType == MemberTypes.Property) ? (DisIs[key] as PropertyInfo).PropertyType : (DisIs[key] as FieldInfo).FieldType;
                        Type genericTypeDefinition = memtype;
                        if (genericTypeDefinition.IsGenericType)
                        {
                            if (genericTypeDefinition.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                genericTypeDefinition = genericTypeDefinition.GetGenericArguments()[0];
                            }
                        }
                        object val = DisIs[key].MemberType == MemberTypes.Property ? (DisIs[key] as PropertyInfo).GetValue(obj, null) : (DisIs[key] as FieldInfo).GetValue(obj);
                        if (val == null)
                        {
                            continue;
                        }
                        val = ConvertionExtensions.ConvertTo((IConvertible)val, genericTypeDefinition);
                        if (DisIs[key] is PropertyInfo)
                        {
                            dr[key] = val;
                        }
                        else
                        {
                            dr[key] = val;
                        }
                    }
                    ret.Rows.Add(dr);
                }
            }
            catch (Exception e)
            {
                ToLog("错误", "将对象列表转换为数据表错误!", string.Format("[{2}]{0}:{1}", e.Message, e.StackTrace, list[lasti].ToString()));
            }
            return(ret);
        }
Ejemplo n.º 12
0
        public List <T> FillByTable <T>(DataTable dt, ref List <MemberInfo> TableBuffs)
        {
            List <T> ret = new List <T>();
            Type     t   = typeof(T);

            if (TableStructure != null && TableStructure.ContainsKey(typeof(T).ToString()))
            {
                TableBuffs = TableStructure[typeof(T).ToString()];
            }
            if (dt == null || dt.Columns.Count == 0)
            {
                Log("错误", "数据源错误", "为空/或者列为空");
            }
            if (TableBuffs == null)
            {
                TableBuffs = new List <MemberInfo>();
                MemberInfo[] mis = t.GetMembers();
                for (int i = 0; i < mis.Length; i++)
                {
                    if (dt.Columns.Contains(mis[i].Name))
                    {
                        if (!TableBuffs.Contains(mis[i]))
                        {
                            TableBuffs.Add(mis[i]);
                        }
                        else
                        {
                            Log("错误", "表结构", string.Format("存在相同的列{0}.", mis[i].Name));
                        }
                    }
                }
            }
            //Log("输入表结构列表数量", TableBuffs.Count.ToString());
            if (TableStructure != null && TableBuffs != null)
            {
                if (!TableStructure.ContainsKey(typeof(T).ToString()))
                {
                    TableStructure.Add(typeof(T).ToString(), TableBuffs);
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T       cc = (T)Activator.CreateInstance(typeof(T));
                DataRow dr = dt.Rows[i];
                for (int j = 0; j < TableBuffs.Count; j++)
                {
                    //return Convert.ChangeType(value, t);

                    MemberInfo mi  = TableBuffs[j];
                    Type       ty  = (mi is PropertyInfo) ? (mi as PropertyInfo).PropertyType : (mi as FieldInfo).FieldType;
                    object     val = dr[mi.Name];
                    if (val == null)
                    {
                        continue;
                    }
                    val = ConvertionExtensions.ConvertTo((IConvertible)val, ty);
                    if (mi is PropertyInfo)
                    {
                        (mi as PropertyInfo).SetValue(cc, val, null);
                    }
                    if (mi is FieldInfo)
                    {
                        (mi as FieldInfo).SetValue(cc, val);
                    }
                }
                ret.Add(cc);
            }
            return(ret);
        }
Ejemplo n.º 13
0
 public bool FillTo(ref object obj, bool includeParent = false)
 {
     return(ConvertionExtensions.FillTo(this, ref obj, includeParent));
 }
Ejemplo n.º 14
0
 public T CopyTo <T>()
 {
     return(ConvertionExtensions.CopyTo <T>(this));
 }