Beispiel #1
0
        public static object SetCount(this Array self, int newCount)
        {
            if (self == null)
            {
                return(null);
            }

            if (self.Length == newCount)
            {
                return(self);
            }

            Type t = self.GetType().GetElementType();

            Array newArray = Array.CreateInstance(t, newCount);

            Array.Copy(self, 0, newArray, 0, Math.Min(self.Length, newCount));

            //因为Array.CreateInstance API创建的数组如果元素Type为引用类型则为null
            if (newCount > self.Length)
            {
                for (int i = self.Length; i < newArray.Length; i++)
                {
                    newArray.SetValue(CreateInstanceTool.Create(t), i);
                }
            }

            return(newArray);
        }
Beispiel #2
0
        public void Init()
        {
            value = CreateInstanceTool.Create(type);

            SetValue(value);
        }