Server.Commands.Properties.GetPropertyInfoChain C# (CSharp) Méthode

GetPropertyInfoChain() public static méthode

public static GetPropertyInfoChain ( Mobile from, Type type, string propertyString, PropertyAccess endAccess, string &failReason ) : System.Reflection.PropertyInfo[]
from Mobile
type System.Type
propertyString string
endAccess PropertyAccess
failReason string
Résultat System.Reflection.PropertyInfo[]
		public static PropertyInfo[] GetPropertyInfoChain( Mobile from, Type type, string propertyString, PropertyAccess endAccess, ref string failReason )
		{
			string[] split = propertyString.Split( '.' );

			if ( split.Length == 0 )
				return null;

			PropertyInfo[] info = new PropertyInfo[split.Length];

			for ( int i = 0; i < info.Length; ++i )
			{
				string propertyName = split[i];

				if ( CIEqual( propertyName, "current" ) )
					continue;

				PropertyInfo[] props = type.GetProperties( BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public );

				bool isFinal = ( i == (info.Length - 1) );

				PropertyAccess access = endAccess;

				if ( !isFinal )
					access |= PropertyAccess.Read;

				for ( int j = 0; j < props.Length; ++j )
				{
					PropertyInfo p = props[j];

					if ( CIEqual( p.Name, propertyName ) )
					{
						CPA attr = GetCPA( p );

						if ( attr == null )
						{
							failReason = String.Format( "Property '{0}' not found.", propertyName );
							return null;
						}
						else if ( (access & PropertyAccess.Read) != 0 && from.AccessLevel < attr.ReadLevel )
						{
							failReason = String.Format( "You must be at least {0} to get the property '{1}'.",
								Mobile.GetAccessLevelName( attr.ReadLevel ), propertyName );

							return null;
						}
						else if ( (access & PropertyAccess.Write) != 0 && from.AccessLevel < attr.WriteLevel )
						{
							failReason = String.Format( "You must be at least {0} to set the property '{1}'.",
								Mobile.GetAccessLevelName( attr.WriteLevel ), propertyName );

							return null;
						}
						else if ( (access & PropertyAccess.Read) != 0 && !p.CanRead )
						{
							failReason = String.Format( "Property '{0}' is write only.", propertyName );
							return null;
						}
						else if ( (access & PropertyAccess.Write) != 0 && (!p.CanWrite || attr.ReadOnly) && isFinal )
						{
							failReason = String.Format( "Property '{0}' is read only.", propertyName );
							return null;
						}

						info[i] = p;
						type = p.PropertyType;
						break;
					}
				}

				if ( info[i] == null )
				{
					failReason = String.Format( "Property '{0}' not found.", propertyName );
					return null;
				}
			}

			return info;
		}

Usage Example

        public PropertyCondition(Mobile from, Type type, PropertyInfo[] props, string prop, string oper, string arg, bool logicalNot)
        {
            m_From       = from;
            m_LogicalNot = logicalNot;

            string failReason = "";

            m_PropertyInfoChain = Properties.GetPropertyInfoChain(from, type, prop, true, ref failReason);

            if (m_PropertyInfoChain == null)
            {
                throw new Exception(failReason);
            }

            /*for ( int i = 0; i < props.Length; ++i )
             * {
             *      PropertyInfo check = props[i];
             *
             *      if ( !Insensitive.Equals( check.Name, prop ) )
             *              continue;
             *
             *      m_Property = check;
             *      break;
             * }
             *
             * if ( m_Property == null )
             *      throw new Exception( String.Format( "No property with the name ({0}) was found on type ({1}).", prop, type.Name ) );
             *
             * CPA attr = Properties.GetCPA( m_Property );
             *
             * if ( attr == null )
             *      throw new Exception( String.Format( "No property with the name ({0}) was found on type ({1}).", prop, type.Name ) );
             *
             * if ( from.AccessLevel < attr.ReadLevel )
             *      throw new Exception( String.Format( "Getting this property ({0}) requires at least {1} access level.", prop, Mobile.GetAccessLevelName( attr.ReadLevel ) ) );*/

            string error = Properties.ConstructFromString(m_PropertyInfoChain[m_PropertyInfoChain.Length - 1].PropertyType, null, arg, ref m_Argument);

            if (error != null)
            {
                throw new Exception(error);
            }

            switch (oper)
            {
            case "=":
            case "==":
            case "is": m_Operator = ConditionOperator.Equality; break;

            case "!=": m_Operator = ConditionOperator.Inequality; break;

            case ">": m_Operator = ConditionOperator.Greater; break;

            case "<": m_Operator = ConditionOperator.Lesser; break;

            case ">=": m_Operator = ConditionOperator.GreaterEqual; break;

            case "<=": m_Operator = ConditionOperator.LesserEqual; break;

            case "==~":
            case "~==":
            case "=~":
            case "~=":
            case "is~":
            case "~is": m_Operator = ConditionOperator.EqualityInsensitive; break;

            case "!=~":
            case "~!=": m_Operator = ConditionOperator.InequalityInsensitive; break;

            case "starts": m_Operator = ConditionOperator.StartsWith; break;

            case "starts~":
            case "~starts": m_Operator = ConditionOperator.StartsWithInsensitive; break;

            case "ends": m_Operator = ConditionOperator.EndsWith; break;

            case "ends~":
            case "~ends": m_Operator = ConditionOperator.EndsWithInsensitive; break;

            case "contains": m_Operator = ConditionOperator.Contains; break;

            case "contains~":
            case "~contains": m_Operator = ConditionOperator.ContainsInsensitive; break;
            }

            if (m_Operator != ConditionOperator.Equality && m_Operator != ConditionOperator.Inequality)
            {
                if (m_Argument != null && !(m_Argument is IComparable))
                {
                    throw new Exception(String.Format("This property ({0}) is not comparable.", prop));
                }
            }
        }
All Usage Examples Of Server.Commands.Properties::GetPropertyInfoChain