当前位置: 首页>>代码示例>>C#>>正文


C# PhpArray.SetArrayItem方法代码示例

本文整理汇总了C#中PHP.Core.PhpArray.SetArrayItem方法的典型用法代码示例。如果您正苦于以下问题:C# PhpArray.SetArrayItem方法的具体用法?C# PhpArray.SetArrayItem怎么用?C# PhpArray.SetArrayItem使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PHP.Core.PhpArray的用法示例。


在下文中一共展示了PhpArray.SetArrayItem方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddVariable

		/// <summary>
		/// Adds a variable to auto-global array.
		/// </summary>
		/// <param name="array">The array.</param>
		/// <param name="name">A unparsed name of variable.</param>
		/// <param name="value">A value to be added.</param>
		/// <param name="subname">A name of intermediate array inserted before the value.</param>
		private static void AddVariable(
		  PhpArray/*!*/ array,
		  string name,
		  object value,
		  string subname)
		{
			if (array == null)
				throw new ArgumentNullException("array");
			if (name == null)
				name = String.Empty;

            string key;

			// current left and right square brace positions:
			int left, right;

			// checks pattern {var_name}[{key1}][{key2}]...[{keyn}] where var_name is [^[]* and keys are [^]]*:
			left = name.IndexOf('[');
			if (left > 0 && left < name.Length - 1 && (right = name.IndexOf(']', left + 1)) >= 0)
			{
				// the variable name is a key to the "array", dots are replaced by underscores in top-level name:
                key = EncodeTopLevelName(name.Substring(0, left));

				// ensures that all [] operators in the chain except for the last one are applied on an array:
				for (;;)
				{
					// adds a level keyed by "key":
					array = Operators.EnsureItemIsArraySimple(array, key);

					// adds a level keyed by "subname" (once only):
					if (subname != null)
					{
						array = Operators.EnsureItemIsArraySimple(array, subname);
						subname = null;
					}

					// next key:
					key = name.Substring(left + 1, right - left - 1);

					// breaks if ']' is not followed by '[':
					left = right + 1;
					if (left == name.Length || name[left] != '[') break;

					// the next right brace:
					right = name.IndexOf(']', left + 1);
				}

				if (key.Length > 0)
					array.SetArrayItem(key, value);
				else
					array.Add(value);
			}
			else
			{
				// no array pattern in variable name, "name" is a top-level key:
                name = EncodeTopLevelName(name);

				// inserts a subname on the next level:
				if (subname != null)
					Operators.EnsureItemIsArraySimple(array, name)[subname] = value;
				else
					array[name] = value;
			}
		}
开发者ID:hansdude,项目名称:Phalanger,代码行数:71,代码来源:GlobalVariables.CLR.cs

示例2: SetItemEpilogue

        private static void SetItemEpilogue(object value, object key, ref object var)
        {
            // empty:
            if (IsEmptyForEnsure(var))
            {
                PhpArray var_array = new PhpArray(0, 1);
                var_array.SetArrayItem(key, value);
                var = var_array;
                return;
            }

            // PhpArray (derived types):
            PhpArray array;
            if ((array = var as PhpArray) != null)
            {
                array.SetArrayItem(key, value);
                return;
            }

            // object behaving as array:
            DObject dobj = var as DObject;
            if (dobj != null)
            {
                var realObject = dobj.RealObject;

                if (realObject is Library.SPL.ArrayAccess)
                {
                    //PhpStack stack = ScriptContext.CurrentContext.Stack;
                    //stack.AddFrame(key, value);
                    //dobj.InvokeMethod(Library.SPL.PhpArrayObject.offsetSet, null, stack.Context);
                    ((Library.SPL.ArrayAccess)realObject).offsetSet(ScriptContext.CurrentContext, key, value);
                    return;
                }

                if (realObject is IList)
                {
                    ((IList)realObject)[Convert.ObjectToInteger(key)] = value;
                    return;
                }

                if (realObject is IDictionary)
                {
                    ((IDictionary)realObject)[key] = value;
                    return;
                }
            }
            
            // errors - DObject, scalars:
            PhpException.VariableMisusedAsArray(var, false);
        }
开发者ID:kaviarasankk,项目名称:Phalanger,代码行数:50,代码来源:Operators.cs

示例3: PhpSafeType

        /// <remarks>
        /// The following code has been taken from PHP View Engine.
        /// http://phpviewengine.codeplex.com/license
        /// </remarks>
        private object PhpSafeType(object o)
        {
            // PHP can handle bool, int, double, and long
            if ((o is int) || (o is double) || (o is long) || (o is bool))
            {
                return o;
            }
            // Upcast other integer types so PHP can use them
            // TODO: What to do about System.UInt64 and byte?
            else if (o is short)
            {
                return (int)(short)o;
            }
            else if (o is ushort)
            {
                return (int)(ushort)o;
            }
            else if (o is uint)
            {
                return (long)(uint)o;
            }
            else if (o is ulong)
            {
                ulong u = (ulong)o;
                if (u <= Int64.MaxValue)
                {
                    return System.Convert.ToInt64(u);
                }
                else
                {
                    return u.ToString();
                }
            }
            // Convert System.Single to a string
            // to reduce rounding errors
            // TODO: Figure out why I need to do this
            else if (o is float)
            {
                return Double.Parse(o.ToString());
            }
            // Really not sure what the best thing is to do with 'System.Decimal'
            // TODO: Review this decision
            else if (o is decimal)
            {
                return o.ToString();
            }
            // Strings and byte arrays require special handling
            else if (o is string)
            {
                return new PhpString((string)o);
            }
            else if (o is byte[])
            {
                return new PhpBytes((byte[])o);
            }
            // Convert .NET collections into PHP arrays
            else if (o is ICollection)
            {
                var ca = new PhpArray();
                if (o is IDictionary)
                {
                    var dict = o as IDictionary;
                    foreach (var key in dict.Keys)
                    {
                        var val = PhpSafeType(dict[key]);
                        ca.SetArrayItem(PhpSafeType(key), val);
                    }
                }
                else
                {
                    foreach (var item in (ICollection)o)
                    {
                        ca.Add(PhpSafeType(item));
                    }
                }
                return ca;
            }

            // PHP types are obviously ok and can just move along
            if (o is DObject)
            {
                return o;
            }

            // Wrap all remaining CLR types so that PHP can handle tham
            return ClrObject.WrapRealObject(o);
        }
开发者ID:leekelleher,项目名称:umbraco-php-macro-engine,代码行数:91,代码来源:PhpMacroEngine.cs


注:本文中的PHP.Core.PhpArray.SetArrayItem方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。