コード例 #1
0
//		#region public static string ReflectThrough(...);
        #region public static string ReflectThrough(...);
        public static string ReflectThrough(
            object someClass_in,
            string path_in,
            IterationFoundDelegate iteration_found_in,
            string iteration_in,
            string pathTranslated_in,
            bool returnValue_in,
            bool anyAttribute_notJustXml
            )
        {
            bool _valueHasBeenFound = false;

            return(ReflectThrough(
                       someClass_in,
                       path_in,
                       iteration_found_in,
                       iteration_in,
                       pathTranslated_in,
                       returnValue_in,
                       anyAttribute_notJustXml,
                       ref _valueHasBeenFound
                       ));
        }
コード例 #2
0
ファイル: utils.cs プロジェクト: katshann/ogen
		public static string ReflectThrough(
			object someClass_in, 
			string path_in, 
			IterationFoundDelegate iteration_found_in, 
			string iteration_in, 
			string pathTranslated_in, 
			bool returnValue_in, 
			bool anyAttribute_notJustXml, 
			ref bool valueHasBeenFound_out
		) {
			string _output = null;

#if DEBUG
const bool _usePerformance1 = true;
const bool _usePerformance2 = false;
const bool _usePerformance3 = true;
const bool _usePerformance5 = false;
//Console.WriteLine(
//    "class: {0}\npath: {1}\niteration: {2}\npathTranslated: {3}\nreturnValue: {4}\nanyAttribute_notJustXml: {5}\n",
//    someClass_in.GetType().ToString(),
//    path_in,
//    iteration_in,
//    pathTranslated_in,
//    returnValue_in,
//    anyAttribute_notJustXml
//);
//Console.ReadKey();
#else
const bool _usePerformance1 = true;
const bool _usePerformance2 = false;
const bool _usePerformance3 = true;
const bool _usePerformance5 = false;
#endif

			if (
//#if DEBUG
_usePerformance1 && (
//#endif
				(
					returnValue_in
					&&
					(
						(path_in.Length > iteration_in.Length)
						||
						(path_in != iteration_in.Substring(0, path_in.Length))
					)
				)
				||
				(
					!returnValue_in
					&&
					(
						(pathTranslated_in.Length > iteration_in.Length)
						||
						(pathTranslated_in != iteration_in.Substring(0, pathTranslated_in.Length))
					)
				)
//#if DEBUG
)
//#endif
			) {
				// performance tweak, compares path to avoid looking in the wrong direction 
				return null;
			}

			if (iteration_in == pathTranslated_in) {
				valueHasBeenFound_out = true;
				if (iteration_found_in != null) iteration_found_in(path_in);
			}

//#if DEBUG
//Console.WriteLine(
//    "\n\t---\n\t{0}.{1}.ReflectThrough:{7}(\n\t\tsomeClass_in:\"{2}.{3}\",\n\t\tpath_in:\"{4}\",\n\t\titeration_in:\"{5}\",\n\t\tpathTranslated_in:\"{6}\"\n\t)\n\t---",
//    typeof(utils).Namespace,
//    typeof(utils).Name,
//    someClass_in.GetType().Namespace,
//    someClass_in.GetType().Name,
//    path_in,
//    iteration_in,
//    pathTranslated_in,
//    returnValue_in ? "READ" : "ITERATE"
//);
//Console.Write("{{{0}}}", path_in.ToUpper());
//#endif

			PropertyInfo[] _properties;
			System.Xml.Serialization.XmlElementAttribute _elementAttribute;
			System.Xml.Serialization.XmlAttributeAttribute _attribute;
			object _value;
			Array _array;
			bool _isAttribute = false;
			bool _isElement = false;
			string _attributename = string.Empty;
			int _indexOfSquareBrackets_begin = -1;
			int _indexOfSquareBrackets_end = -1;
			string _aux1;
			string _aux2;

			_properties = someClass_in.GetType().GetProperties(
				BindingFlags.Public | 
				BindingFlags.Instance
			);
			for (int _prop = 0; _prop < _properties.Length; _prop++) {
				_isAttribute = Attribute.IsDefined(
					_properties[_prop], 
					typeof(System.Xml.Serialization.XmlAttributeAttribute)
				);
				_isElement = Attribute.IsDefined(
					_properties[_prop], 
					typeof(System.Xml.Serialization.XmlElementAttribute)
				);

				if (
					_isElement
&&
(
	(_properties[_prop].PropertyType != typeof(string))
	&&
	(_properties[_prop].PropertyType != typeof(decimal))
	&&
	(_properties[_prop].PropertyType != typeof(int))
	&&
	(_properties[_prop].PropertyType != typeof(bool))
	&&
	(_properties[_prop].PropertyType != typeof(DateTime))
)
				) {
					#region XmlElement...
					_value = _properties[_prop].GetValue(someClass_in, null);
					if (_value == null) continue;

					_elementAttribute 
						= (System.Xml.Serialization.XmlElementAttribute)Attribute.GetCustomAttributes(
							_properties[_prop], 
							typeof(System.Xml.Serialization.XmlElementAttribute), 
							true
						)[0];

					_aux1 = string.Format(
						"{0}.{1}",
						path_in,
						_elementAttribute.ElementName
					);
					_indexOfSquareBrackets_begin = _aux1.Length;
					if (
//#if DEBUG
_usePerformance2 && (
//#endif

(_indexOfSquareBrackets_begin > iteration_in.Length)

						||
						(
							_aux1 != iteration_in.Substring(
								0,
								_indexOfSquareBrackets_begin
							)
						)
						||
						(
							(
								(_aux2 = iteration_in.Substring(
									_indexOfSquareBrackets_begin, 
									1
								)) != "["
							)
							&&
							(_aux2 != ".")
						)
//#if DEBUG
)
//#endif
					) {
						continue;
					}

					if (_value.GetType().IsArray) {
						if (
//#if DEBUG
_usePerformance3 && 
//#endif
							returnValue_in
&&
(_indexOfSquareBrackets_begin + 1 < iteration_in.Length)
						) {
							_indexOfSquareBrackets_end
								= iteration_in.IndexOf(
									']',
									_indexOfSquareBrackets_begin + 1
								);
						}

						_array = (Array)_value;
						for (
							int i = 
								(
//#if DEBUG
_usePerformance5 &&
//#endif
									returnValue_in
								)
								?
									// performance tweak, goes straight to the iteration 
									// when (returnValue_in == true)
									int.Parse(
										iteration_in.Substring(
											_indexOfSquareBrackets_begin + 1,
											_indexOfSquareBrackets_end - (_indexOfSquareBrackets_begin + 1)
										)
									)
								: 
									0
							;

							i < _array.Length;
							i++
						) {
							_output = ReflectThrough(
								_array.GetValue(i), 
								string.Format(
									"{0}.{1}[{2}]", 
									path_in, 
									_elementAttribute.ElementName, 
									i
								), 
								iteration_found_in, 
								iteration_in, 
								string.Format(
									"{0}.{1}[n]", 
									pathTranslated_in, 
									_elementAttribute.ElementName
								), 
								returnValue_in, 
								anyAttribute_notJustXml,
								ref valueHasBeenFound_out
							);
							if (returnValue_in && valueHasBeenFound_out)
								return _output;
						}
					} else {
						_output = ReflectThrough(
							_value, 
							string.Format(
								"{0}.{1}", 
								path_in, 
								_elementAttribute.ElementName
							), 
							iteration_found_in, 
							iteration_in, 
							string.Format(
								"{0}.{1}", 
								pathTranslated_in, 
								_elementAttribute.ElementName
							), 
							returnValue_in, 
							anyAttribute_notJustXml,
							ref valueHasBeenFound_out
						);
						if (returnValue_in && valueHasBeenFound_out)
							return _output;
					}
					#endregion
				} else if (
					_isAttribute
					||
					anyAttribute_notJustXml
					||
(
	_isElement
	&&
	(
		(_properties[_prop].PropertyType == typeof(string))
	)
)
				) {
					#region XmlAttribute...
					if (
						anyAttribute_notJustXml
						&&
						!_properties[_prop].CanRead
					) continue;

					try {
						_value = _properties[_prop].GetValue(someClass_in, null);
					} catch (Exception _ex) {
						string _ex_message = string.Format(
							"\n---\n{0}.{1}.ReflectThrough(\n\tsomeClass_in:\"{2}.{3}\",\n\tpath_in:\"{4}\",\n\titeration_in:\"{5}\",\n\tpathTranslated_in:\"{6}\"\n)\n---\n{7}",
							typeof(utils).Namespace,
							typeof(utils).Name,
							someClass_in.GetType().Namespace,
							someClass_in.GetType().Name,
							path_in,
							iteration_in,
							pathTranslated_in,
							_ex.Message
						);
#if DEBUG
_value = null;
Console.WriteLine(_ex_message);
#else
						throw new Exception(_ex_message);
#endif
					}

					if (_value == null) continue;

					if (_isAttribute) {
						_attribute
							= (System.Xml.Serialization.XmlAttributeAttribute)Attribute.GetCustomAttributes(
								_properties[_prop],
								typeof(System.Xml.Serialization.XmlAttributeAttribute),
								true
							)[0];
						_attributename = _attribute.AttributeName;
					} else if (_isElement) {
						_elementAttribute
							= (System.Xml.Serialization.XmlElementAttribute)Attribute.GetCustomAttributes(
								_properties[_prop],
								typeof(System.Xml.Serialization.XmlElementAttribute),
								true
							)[0];
						_attributename = _elementAttribute.ElementName;
					} else {
						_attributename = _properties[_prop].Name;
					}

					if (string.Format("{0}.{1}", path_in, _attributename) == iteration_in) {
						valueHasBeenFound_out = true;
						return _value.ToString();
					}
					#endregion
				} 
				//else {
				//    throw new Exception(string.Format(
				//        "class: {0}\npath: {1}\niteration: {2}\npathTranslated: {3}\nreturnValue: {4}\nanyAttribute_notJustXml: {5}\n", 
				//        someClass_in.GetType().ToString(), 
				//        path_in, 
				//        iteration_in, 
				//        pathTranslated_in, 
				//        returnValue_in, 
				//        anyAttribute_notJustXml
				//    ));
				//}
			}

			return _output;
		}
コード例 #3
0
ファイル: utils.cs プロジェクト: katshann/ogen
//		#region public static string ReflectThrough(...);
		#region public static string ReflectThrough(...);
		public static string ReflectThrough(
			object someClass_in,
			string path_in,
			IterationFoundDelegate iteration_found_in,
			string iteration_in,
			string pathTranslated_in,
			bool returnValue_in,
			bool anyAttribute_notJustXml
		) {
			bool _valueHasBeenFound = false;

			return ReflectThrough(
				someClass_in,
				path_in,
				iteration_found_in,
				iteration_in,
				pathTranslated_in,
				returnValue_in,
				anyAttribute_notJustXml,
				ref _valueHasBeenFound
			);
		}
コード例 #4
0
        public static string ReflectThrough(
            object someClass_in,
            string path_in,
            IterationFoundDelegate iteration_found_in,
            string iteration_in,
            string pathTranslated_in,
            bool returnValue_in,
            bool anyAttribute_notJustXml,
            ref bool valueHasBeenFound_out
            )
        {
            string _output = null;

#if DEBUG
            const bool _usePerformance1 = true;
            const bool _usePerformance2 = false;
            const bool _usePerformance3 = true;
            const bool _usePerformance5 = false;
//Console.WriteLine(
//    "class: {0}\npath: {1}\niteration: {2}\npathTranslated: {3}\nreturnValue: {4}\nanyAttribute_notJustXml: {5}\n",
//    someClass_in.GetType().ToString(),
//    path_in,
//    iteration_in,
//    pathTranslated_in,
//    returnValue_in,
//    anyAttribute_notJustXml
//);
//Console.ReadKey();
#else
            const bool _usePerformance1 = true;
            const bool _usePerformance2 = false;
            const bool _usePerformance3 = true;
            const bool _usePerformance5 = false;
#endif

            if (
//#if DEBUG
                _usePerformance1 && (
//#endif
                    (
                        returnValue_in
                        &&
                        (
                            (path_in.Length > iteration_in.Length)
                            ||
                            (path_in != iteration_in.Substring(0, path_in.Length))
                        )
                    )
                    ||
                    (
                        !returnValue_in
                        &&
                        (
                            (pathTranslated_in.Length > iteration_in.Length)
                            ||
                            (pathTranslated_in != iteration_in.Substring(0, pathTranslated_in.Length))
                        )
                    )
//#if DEBUG
                    )
//#endif
                )
            {
                // performance tweak, compares path to avoid looking in the wrong direction
                return(null);
            }

            if (iteration_in == pathTranslated_in)
            {
                valueHasBeenFound_out = true;
                if (iteration_found_in != null)
                {
                    iteration_found_in(path_in);
                }
            }

//#if DEBUG
//Console.WriteLine(
//    "\n\t---\n\t{0}.{1}.ReflectThrough:{7}(\n\t\tsomeClass_in:\"{2}.{3}\",\n\t\tpath_in:\"{4}\",\n\t\titeration_in:\"{5}\",\n\t\tpathTranslated_in:\"{6}\"\n\t)\n\t---",
//    typeof(utils).Namespace,
//    typeof(utils).Name,
//    someClass_in.GetType().Namespace,
//    someClass_in.GetType().Name,
//    path_in,
//    iteration_in,
//    pathTranslated_in,
//    returnValue_in ? "READ" : "ITERATE"
//);
//Console.Write("{{{0}}}", path_in.ToUpper());
//#endif

            PropertyInfo[] _properties;
            System.Xml.Serialization.XmlElementAttribute   _elementAttribute;
            System.Xml.Serialization.XmlAttributeAttribute _attribute;
            object _value;
            Array  _array;
            bool   _isAttribute   = false;
            bool   _isElement     = false;
            string _attributename = string.Empty;
            int    _indexOfSquareBrackets_begin = -1;
            int    _indexOfSquareBrackets_end   = -1;
            string _aux1;
            string _aux2;

            _properties = someClass_in.GetType().GetProperties(
                BindingFlags.Public |
                BindingFlags.Instance
                );
            for (int _prop = 0; _prop < _properties.Length; _prop++)
            {
                _isAttribute = Attribute.IsDefined(
                    _properties[_prop],
                    typeof(System.Xml.Serialization.XmlAttributeAttribute)
                    );
                _isElement = Attribute.IsDefined(
                    _properties[_prop],
                    typeof(System.Xml.Serialization.XmlElementAttribute)
                    );

                if (
                    _isElement
                    &&
                    (
                        (_properties[_prop].PropertyType != typeof(string))
                        &&
                        (_properties[_prop].PropertyType != typeof(decimal))
                        &&
                        (_properties[_prop].PropertyType != typeof(int))
                        &&
                        (_properties[_prop].PropertyType != typeof(bool))
                        &&
                        (_properties[_prop].PropertyType != typeof(DateTime))
                    )
                    )
                {
                    #region XmlElement...
                    _value = _properties[_prop].GetValue(someClass_in, null);
                    if (_value == null)
                    {
                        continue;
                    }

                    _elementAttribute
                        = (System.Xml.Serialization.XmlElementAttribute)Attribute.GetCustomAttributes(
                              _properties[_prop],
                              typeof(System.Xml.Serialization.XmlElementAttribute),
                              true
                              )[0];

                    _aux1 = string.Format(
                        "{0}.{1}",
                        path_in,
                        _elementAttribute.ElementName
                        );
                    _indexOfSquareBrackets_begin = _aux1.Length;
                    if (
//#if DEBUG
                        _usePerformance2 && (
//#endif

                            (_indexOfSquareBrackets_begin > iteration_in.Length)

                            ||
                            (
                                _aux1 != iteration_in.Substring(
                                    0,
                                    _indexOfSquareBrackets_begin
                                    )
                            )
                            ||
                            (
                                (
                                    (_aux2 = iteration_in.Substring(
                                         _indexOfSquareBrackets_begin,
                                         1
                                         )) != "["
                                )
                                &&
                                (_aux2 != ".")
                            )
//#if DEBUG
                            )
//#endif
                        )
                    {
                        continue;
                    }

                    if (_value.GetType().IsArray)
                    {
                        if (
//#if DEBUG
                            _usePerformance3 &&
//#endif
                            returnValue_in
                            &&
                            (_indexOfSquareBrackets_begin + 1 < iteration_in.Length)
                            )
                        {
                            _indexOfSquareBrackets_end
                                = iteration_in.IndexOf(
                                      ']',
                                      _indexOfSquareBrackets_begin + 1
                                      );
                        }

                        _array = (Array)_value;
                        for (
                            int i =
                                (
//#if DEBUG
                                    _usePerformance5 &&
//#endif
                                    returnValue_in
                                )
                                                                ?
                                // performance tweak, goes straight to the iteration
                                // when (returnValue_in == true)
                                int.Parse(
                                    iteration_in.Substring(
                                        _indexOfSquareBrackets_begin + 1,
                                        _indexOfSquareBrackets_end - (_indexOfSquareBrackets_begin + 1)
                                        )
                                    )
                                                                :
                                0
                            ;

                            i < _array.Length;
                            i++
                            )
                        {
                            _output = ReflectThrough(
                                _array.GetValue(i),
                                string.Format(
                                    "{0}.{1}[{2}]",
                                    path_in,
                                    _elementAttribute.ElementName,
                                    i
                                    ),
                                iteration_found_in,
                                iteration_in,
                                string.Format(
                                    "{0}.{1}[n]",
                                    pathTranslated_in,
                                    _elementAttribute.ElementName
                                    ),
                                returnValue_in,
                                anyAttribute_notJustXml,
                                ref valueHasBeenFound_out
                                );
                            if (returnValue_in && valueHasBeenFound_out)
                            {
                                return(_output);
                            }
                        }
                    }
                    else
                    {
                        _output = ReflectThrough(
                            _value,
                            string.Format(
                                "{0}.{1}",
                                path_in,
                                _elementAttribute.ElementName
                                ),
                            iteration_found_in,
                            iteration_in,
                            string.Format(
                                "{0}.{1}",
                                pathTranslated_in,
                                _elementAttribute.ElementName
                                ),
                            returnValue_in,
                            anyAttribute_notJustXml,
                            ref valueHasBeenFound_out
                            );
                        if (returnValue_in && valueHasBeenFound_out)
                        {
                            return(_output);
                        }
                    }
                    #endregion
                }
                else if (
                    _isAttribute
                    ||
                    anyAttribute_notJustXml
                    ||
                    (
                        _isElement
                        &&
                        (
                            (_properties[_prop].PropertyType == typeof(string))
                        )
                    )
                    )
                {
                    #region XmlAttribute...
                    if (
                        anyAttribute_notJustXml
                        &&
                        !_properties[_prop].CanRead
                        )
                    {
                        continue;
                    }

                    try {
                        _value = _properties[_prop].GetValue(someClass_in, null);
                    } catch (Exception _ex) {
                        string _ex_message = string.Format(
                            "\n---\n{0}.{1}.ReflectThrough(\n\tsomeClass_in:\"{2}.{3}\",\n\tpath_in:\"{4}\",\n\titeration_in:\"{5}\",\n\tpathTranslated_in:\"{6}\"\n)\n---\n{7}",
                            typeof(utils).Namespace,
                            typeof(utils).Name,
                            someClass_in.GetType().Namespace,
                            someClass_in.GetType().Name,
                            path_in,
                            iteration_in,
                            pathTranslated_in,
                            _ex.Message
                            );
#if DEBUG
                        _value = null;
                        Console.WriteLine(_ex_message);
#else
                        throw new Exception(_ex_message);
#endif
                    }

                    if (_value == null)
                    {
                        continue;
                    }

                    if (_isAttribute)
                    {
                        _attribute
                            = (System.Xml.Serialization.XmlAttributeAttribute)Attribute.GetCustomAttributes(
                                  _properties[_prop],
                                  typeof(System.Xml.Serialization.XmlAttributeAttribute),
                                  true
                                  )[0];
                        _attributename = _attribute.AttributeName;
                    }
                    else if (_isElement)
                    {
                        _elementAttribute
                            = (System.Xml.Serialization.XmlElementAttribute)Attribute.GetCustomAttributes(
                                  _properties[_prop],
                                  typeof(System.Xml.Serialization.XmlElementAttribute),
                                  true
                                  )[0];
                        _attributename = _elementAttribute.ElementName;
                    }
                    else
                    {
                        _attributename = _properties[_prop].Name;
                    }

                    if (string.Format("{0}.{1}", path_in, _attributename) == iteration_in)
                    {
                        valueHasBeenFound_out = true;
                        return(_value.ToString());
                    }
                    #endregion
                }
                //else {
                //    throw new Exception(string.Format(
                //        "class: {0}\npath: {1}\niteration: {2}\npathTranslated: {3}\nreturnValue: {4}\nanyAttribute_notJustXml: {5}\n",
                //        someClass_in.GetType().ToString(),
                //        path_in,
                //        iteration_in,
                //        pathTranslated_in,
                //        returnValue_in,
                //        anyAttribute_notJustXml
                //    ));
                //}
            }

            return(_output);
        }