예제 #1
0
        //public static XObject CreateObject( string xmlString, Type objType )
        //{
        //    try
        //    {
        //        if( xmlString == null ||
        //            XObjectHelper.IsXObjectType( objType ) == false ) return null;

        //        //object item = objType.Assembly.CreateInstance( objType.ToString() );
        //        object item = objType.Assembly.CreateInstance(objType.FullName);

        //        XmlDocument xmlDoc = new XmlDocument();
        //        xmlDoc.LoadXml( xmlString );

        //        string tname = XObjectHelper.GetCleanName(objType.Name);
        //        XmlNodeList nodelist = xmlDoc.GetElementsByTagName(tname);
        //        if( nodelist != null && nodelist.Count > 0 )
        //        {
        //            XObjectHelper.XBaseType.InvokeMember( "Load",
        //                BindingFlags.Public | BindingFlags.NonPublic |
        //                BindingFlags.Instance | BindingFlags.InvokeMethod,
        //                null, item, new object[]{nodelist[0]} );
        //        }

        //        _lastError = null;
        //        return item as XObject;
        //    }
        //    catch( Exception err )
        //    {
        //        XObjectManager.NotifyException( typeof(XObjectManager), err );
        //        return null;
        //    }
        //}
        public static XBase CreateObject(string xmlString, Type objType)
        {
            try
            {
                if (xmlString == null ||
                    XObjectHelper.IsXBaseType(objType) == false)
                {
                    return(null);
                }

                //object item = objType.Assembly.CreateInstance( objType.ToString() );
                object item = objType.Assembly.CreateInstance(objType.FullName);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xmlString);

                string      tname    = XObjectHelper.GetCleanName(objType.Name);
                XmlNodeList nodelist = xmlDoc.GetElementsByTagName(tname);
                if (nodelist != null && nodelist.Count > 0)
                {
                    XObjectHelper.XBaseType.InvokeMember("Load",
                                                         BindingFlags.Public | BindingFlags.NonPublic |
                                                         BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                         null, item, new object[] { nodelist[0] });
                }

                _lastError = null;
                return(item as XBase);
            }
            catch (Exception err)
            {
                XObjectManager.NotifyException(typeof(XObjectManager), err);
                return(null);
            }
        }
예제 #2
0
        protected virtual object GetValueEx(string name)
        {
            if (name == null || name.Length < 1)
            {
                return("");
            }

            try
            {
                object result = this.GetType().InvokeMember(name,
                                                            /*BindingFlags.DeclaredOnly |*/ BindingFlags.Public |
                                                            BindingFlags.Instance | BindingFlags.GetProperty |
                                                            BindingFlags.GetField,
                                                            null, this, new object[] { });

                if (result == null)
                {
                    return("");
                }

                MemberInfo[] pilist = this.GetType().GetMember(name);
                if (pilist.Length < 1)
                {
                    return("");
                }

                Type       type  = null;
                MemberInfo minfo = pilist[0];
                if (minfo is PropertyInfo)
                {
                    type = ((PropertyInfo)minfo).PropertyType;
                }
                if (minfo is FieldInfo)
                {
                    type = ((FieldInfo)minfo).FieldType;
                }
                if (type == null)
                {
                    return("");
                }

                return(ProcessGetValue(type, minfo, result));
            }
            catch (Exception e)
            {
                XObjectManager.NotifyException(this, e);
                return(e);
            }
        }
예제 #3
0
        protected static string[] GetPropertyNames(Type t)
        {
            ArrayList strlist = new ArrayList();

            try
            {
                //PropertyInfo[] list = this.GetType().GetProperties( BindingFlags.Instance | BindingFlags.Public );
                //PropertyInfo[] list = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                IOrderedEnumerable <PropertyInfo> list = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).OrderBy(item => item.MetadataToken);
                if (list != null)
                {
                    foreach (PropertyInfo prop in list)
                    {
                        if (prop.DeclaringType == XObjectHelper.XObjectType ||
                            prop.DeclaringType == XObjectHelper.XObjectCollectionType)
                        {
                            continue;
                        }

                        object[] olist = prop.GetCustomAttributes(XObjectHelper.XNodeAttributeType, false);
                        if (olist != null && olist.Length > 0)
                        {
                            XNodeAttribute attr = olist[0] as XNodeAttribute;
                            if (attr != null || attr.VisibleInSerialization == false)
                            {
                                continue;
                            }
                        }

                        strlist.Add(prop.Name);
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
                XObjectManager.NotifyException(t, e);
            }

            return(strlist.ToArray(typeof(string)) as string[]);
        }
예제 #4
0
        protected virtual bool SetValueEx(string name, string newvalue)
        {
            if (name == null || name.Length < 1)
            {
                return(false);
            }

            try
            {
                MemberInfo[] pilist = this.GetType().GetMember(name);
                if (pilist.Length < 1)
                {
                    return(false);
                }

                Type       type  = null;
                MemberInfo minfo = pilist[0];
                if (minfo is PropertyInfo)
                {
                    type = ((PropertyInfo)minfo).PropertyType;
                }
                if (minfo is FieldInfo)
                {
                    type = ((FieldInfo)minfo).FieldType;
                }
                if (type == null)
                {
                    return(false);
                }

                return(ProcessSetValue(type, name, newvalue));
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
                XObjectManager.NotifyException(this, e);
                return(false);
            }
        }
예제 #5
0
        protected bool SetValue(string name, XmlNodeReader reader, ref bool hasAlreadyMoveToNext)
        {
            hasAlreadyMoveToNext = false;
            if (name == null || name.Length < 1)
            {
                return(false);
            }

            try
            {
                PropertyInfo pi   = this.GetType().GetProperty(name);
                Type         type = pi.PropertyType;

                if (XObjectHelper.IsXObjectType(type))
                {
                    object theproperty = this.GetType().InvokeMember(name,
                                                                     /*BindingFlags.DeclaredOnly |*/ BindingFlags.Public |
                                                                     BindingFlags.Instance | BindingFlags.GetProperty,
                                                                     null, this, new object[] { });

                    if (theproperty == null && reader.IsEmptyElement == false)
                    {
                        theproperty = type.Assembly.CreateInstance(type.FullName);
                        this.GetType().InvokeMember(name,
                                                    /*BindingFlags.DeclaredOnly |*/ BindingFlags.Public |
                                                    BindingFlags.Instance | BindingFlags.SetProperty,
                                                    null, this, new object[] { theproperty });
                    }

                    if (theproperty != null)
                    {
                        XObjectHelper.XBaseType.InvokeMember("InnerLoad",
                                                             BindingFlags.DeclaredOnly |
                                                             BindingFlags.Public | BindingFlags.NonPublic |
                                                             BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                             null, theproperty, new object[] { reader, name });

                        return(true);
                    }
                }

                if (XObjectHelper.IsXObjectCollectionType(type))
                {
                    object thecollection = this.GetType().InvokeMember(name,
                                                                       /*BindingFlags.DeclaredOnly |*/ BindingFlags.Public |
                                                                       BindingFlags.Instance | BindingFlags.GetProperty,
                                                                       null, this, new object[] { });

                    if (thecollection == null && reader.IsEmptyElement == false)
                    {
                        thecollection = type.Assembly.CreateInstance(type.FullName);
                        this.GetType().InvokeMember(name,
                                                    /*BindingFlags.DeclaredOnly |*/ BindingFlags.Public |
                                                    BindingFlags.Instance | BindingFlags.SetProperty,
                                                    null, this, new object[] { thecollection });
                    }

                    if (thecollection != null)
                    {
                        XObjectHelper.XBaseType.InvokeMember("InnerLoad",
                                                             BindingFlags.DeclaredOnly |
                                                             BindingFlags.Public | BindingFlags.NonPublic |
                                                             BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                             null, thecollection, new object[] { reader, name });

                        return(true);
                    }
                }

                if (type == typeof(string))
                {
                    object[] olist = pi.GetCustomAttributes(XObjectHelper.XRawXmlStringAttributeType, false);
                    if (olist != null && olist.Length > 0)
                    {
                        XRawXmlStringAttribute a = olist[0] as XRawXmlStringAttribute;
                        if (a != null && a.EnableRawXmlString)
                        {
                            string rawXmlString = reader.ReadInnerXml();
                            hasAlreadyMoveToNext = true;

                            return(SetValueEx(name, rawXmlString));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("(" + name + ") " + e.ToString());
                XObjectManager.NotifyException(this, e);
                return(false);
            }

            return(SetValueEx(name, reader.ReadString()));
        }