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); }
public void Init() { value = CreateInstanceTool.Create(type); SetValue(value); }