コード例 #1
0
        private myPropInfo CreateMyProp(Type t, string name)
        {
            myPropInfo     d      = new myPropInfo();
            myPropInfoType d_type = myPropInfoType.Unknown;

            if (t == typeof(int) || t == typeof(int?))
            {
                d_type = myPropInfoType.Int;
            }
            else if (t == typeof(long) || t == typeof(long?))
            {
                d_type = myPropInfoType.Long;
            }
            else if (t == typeof(string))
            {
                d_type = myPropInfoType.String;
            }
            else if (t == typeof(bool) || t == typeof(bool?))
            {
                d_type = myPropInfoType.Bool;
            }
            else if (t == typeof(DateTime) || t == typeof(DateTime?))
            {
                d_type = myPropInfoType.DateTime;
            }
            else if (t.IsEnum)
            {
                d_type = myPropInfoType.Enum;
            }
            else if (t == typeof(Guid) || t == typeof(Guid?))
            {
                d_type = myPropInfoType.Guid;
            }
            else if (t == typeof(StringDictionary))
            {
                d_type = myPropInfoType.StringDictionary;
            }
            else if (t == typeof(NameValueCollection))
            {
                d_type = myPropInfoType.NameValue;
            }
            else if (t.IsArray)
            {
                d.bt = t.GetElementType();
                if (t == typeof(byte[]))
                {
                    d_type = myPropInfoType.ByteArray;
                }
                else
                {
                    d_type = myPropInfoType.Array;
                }
            }
            else if (t.Name.Contains("Dictionary"))
            {
                d.GenericTypes = Reflection.Instance.GetGenericArguments(t);
                if (d.GenericTypes.Length > 0 && d.GenericTypes[0] == typeof(string))
                {
                    d_type = myPropInfoType.StringKeyDictionary;
                }
                else
                {
                    d_type = myPropInfoType.Dictionary;
                }
            }
#if !SILVERLIGHT
            else if (t == typeof(Hashtable))
            {
                d_type = myPropInfoType.Hashtable;
            }
            else if (t == typeof(DataSet))
            {
                d_type = myPropInfoType.DataSet;
            }
            else if (t == typeof(DataTable))
            {
                d_type = myPropInfoType.DataTable;
            }
#endif
            else if (IsTypeRegistered(t))
            {
                d_type = myPropInfoType.Custom;
            }

            if (t.IsValueType && !t.IsPrimitive && !t.IsEnum && t != typeof(decimal))
            {
                d.IsStruct = true;
            }

            d.IsInterface = t.IsInterface;
            d.IsClass     = t.IsClass;
            d.IsValueType = t.IsValueType;
            if (t.IsGenericType)
            {
                d.IsGenericType = true;
                d.bt            = t.GetGenericArguments()[0];
            }

            d.pt         = t;
            d.Name       = name;
            d.changeType = GetChangeType(t);
            d.Type       = d_type;

            return(d);
        }
コード例 #2
0
        public Dictionary <string, myPropInfo> Getproperties(Type type, string typename)
        {
            Dictionary <string, myPropInfo> sd = null;

            if (_propertycache.TryGetValue(typename, out sd))
            {
                return(sd);
            }
            else
            {
                sd = new Dictionary <string, myPropInfo>();
                var            bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
                PropertyInfo[] pr = type.GetProperties(bf);
                foreach (PropertyInfo p in pr)
                {
                    if (p.GetIndexParameters().Length > 0)// Property is an indexer
                    {
                        continue;
                    }

                    myPropInfo d = CreateMyProp(p.PropertyType, p.Name);
                    d.setter = Reflection.CreateSetMethod(type, p);
                    if (d.setter != null)
                    {
                        d.CanWrite = true;
                    }
                    d.getter = Reflection.CreateGetMethod(type, p);
#if net4
                    var att = p.GetCustomAttributes(true);
                    foreach (var at in att)
                    {
                        if (at is DataMemberAttribute)
                        {
                            var dm = (DataMemberAttribute)at;
                            if (dm.Name != "")
                            {
                                d.memberName = dm.Name;
                            }
                        }
                    }
                    if (d.memberName != null)
                    {
                        sd.Add(d.memberName, d);
                    }
                    else
#endif
                    sd.Add(p.Name.ToLower(), d);
                }
                FieldInfo[] fi = type.GetFields(bf);
                foreach (FieldInfo f in fi)
                {
                    myPropInfo d = CreateMyProp(f.FieldType, f.Name);
                    if (f.IsLiteral == false)
                    {
                        d.setter = Reflection.CreateSetField(type, f);
                        if (d.setter != null)
                        {
                            d.CanWrite = true;
                        }
                        d.getter = Reflection.CreateGetField(type, f);
#if net4
                        var att = f.GetCustomAttributes(true);
                        foreach (var at in att)
                        {
                            if (at is DataMemberAttribute)
                            {
                                var dm = (DataMemberAttribute)at;
                                if (dm.Name != "")
                                {
                                    d.memberName = dm.Name;
                                }
                            }
                        }
                        if (d.memberName != null)
                        {
                            sd.Add(d.memberName, d);
                        }
                        else
#endif
                        sd.Add(f.Name.ToLower(), d);
                    }
                }

                _propertycache.Add(typename, sd);
                return(sd);
            }
        }