我在C#中有一个通用的对象列表,并希望克隆列表.列表中的项目是可复制的,但似乎没有选项可做list.Clone().
有一个简单的方法吗?
是否存在通用ICloneable<T>不存在的特殊原因?
如果我不想在每次克隆东西时都需要施放它,那会更舒服.
我有一个有几个不同类的类,我将这些类中的信息发送给客户,但我不想将它们全部发送出去,所以有些是私有的,有些是[JsonObject(MemberSerialization.OptIn)]旗帜等.
但是,现在我想在需要关闭服务器时每隔12个小时(我不想使用数据库)对所有这些对象进行备份,所以我想做的(如果可能的话)是强制JSON .Net Serializer转换对象和属于该对象的所有对象.
例如:
class Foo
{
public int Number;
private string name;
private PrivateObject po = new PrivateObject();
public string ToJSON()
{ /* Serialize my public field, my property and the object PrivateObject */ }
}
Run Code Online (Sandbox Code Playgroud)
我尝试了这段代码(即使它已经过时了)但它没有序列化与我的对象相关的对象:
Newtonsoft.Json.JsonSerializerSettings jss = new Newtonsoft.Json.JsonSerializerSettings();
Newtonsoft.Json.Serialization.DefaultContractResolver dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
jss.ContractResolver = dcr;
return Newtonsoft.Json.JsonConvert.SerializeObject(this, jss);
Run Code Online (Sandbox Code Playgroud) 我有一堂课
public class Car()
{
public string Name;
public string Model;
}
Run Code Online (Sandbox Code Playgroud)
而且我有财产
List<Car> CarsA = new List<Car>();
CarsA.Add(new Car(){Name = "Verna",Model="Hyundai"});
CarsA.Add(new Car(){Name = "X1",Model="Bmw"});
Run Code Online (Sandbox Code Playgroud)
我还有另一个财产
List<Car> CarsB = new List<Car>();
Run Code Online (Sandbox Code Playgroud)
现在,我想将所有条目从CarsA添加到克隆/复制到CarsB,而不用将CarsA属性作为当前实例
(即我想为每个条目创建一个新对象并添加它)。
就像是
foreach(var car in CarsA)
{
Car newCar =new Car();
newCar.Name = car.Name;
newCar.Model = car.Model;
CarsB.Add(newCar);
}
Run Code Online (Sandbox Code Playgroud)
如果我不想实现ICloneable并且没有复制构造器怎么办?
我有一个扩展方法:
public static List<object> ToModelViewObjectList<ModelViewType>(this IEnumerable<object> source)
{
List<ModelViewType> destinationList = new List<ModelViewType>();
PropertyInfo[] ModelViewProperties = typeof(ModelViewType).GetProperties();
foreach (var sourceElement in source)
{
object destElement = Activator.CreateInstance<ModelViewType>();
foreach (PropertyInfo sourceProperty in sourceElement.GetType().GetProperties())
{
if (ModelViewProperties.Select(m => m.Name).Contains(sourceProperty.Name))
{
destElement.GetType().GetProperty(sourceProperty.Name).SetValue(destElement, sourceProperty.GetValue(sourceElement));
}
}
destinationList.Add((ModelViewType)destElement);
}
return destinationList.Cast<object>().ToList();
}
Run Code Online (Sandbox Code Playgroud)
我有一个方法,其中包含一个对象列表,我希望在此方法中使用调用扩展方法:
public void GridModel(IEnumerable<object> GridDataSource)
{
List<object> list = GridDataSource.ToModelViewObjectList<GridDataSource[0].GetType()>();
}
Run Code Online (Sandbox Code Playgroud)
我应该写什么而不是GridDataSource [0] .GetType()?
我有一个带对象参数的方法.我想创建一个对象类型的通用列表.
public void CreateListByNonGenericType(object myObject)
{
Type objType = myObject.GetType();
var lst = System.Collections.Generic.List < objType.MakeGenericType() > (); …Run Code Online (Sandbox Code Playgroud) 所以我需要一种深度克隆的方法.我想要一张卡片列表等于另一张卡片列表,但我还想修改其中一个克隆.
我做了一个复制列表的方法,如下所示:
public List<Card> Copy(List<Card> cards)
{
List<Card> clone = new List<Card>();
foreach (var card in cards)
{
clone.Add(card);
}
return clone;
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
_cards = new List<Card>();
_thrownCards = new List<Card>();
_cards = Copy(_thrownCards);
_thrownCards.Clear();
Run Code Online (Sandbox Code Playgroud)
我没有C#的经验,但不知怎的,我的直觉告诉我,我的复制方法可以变得更简单.没有其他方法可以深度复制列表吗?我尝试使用MemberWiseClone,但这只是创建了对同一个对象的引用,而不是克隆它(可能我误解了MemberWiseClone方法).
有没有人有任何提示如何简单地克隆列表对象?
考虑这种情况:Form1 构建了一个包含大量元素的 List 对象。然后,它必须通过参数将此集合传递给 Form2。
在 Form2 中制作它的硬拷贝后,我想清除与 Form1 中的集合相关的所有内容。让Col1成为 Form1 中集合的标识符。
由于 Col1 是通过参数通过引用传递的,因此我调用Col1.Clear()来清除它的元素,然后调用 Col1.TrimExcess()将其实际大小减少到 0,因此它不会跟踪大量空值。
我的目的是尽快清除所有使用过的内存。Col1.TrimExcess() 应该清除所有使用的内存,但我很好奇 Col1 = null 在我(或大多数)情况下是否会更好?
我有一个叫做temp类型的变量List<Point>.出于某种原因,当我打电话时sanitize(temp),变量temp似乎发生了变化.我知道变量会temp发生变化,因为C:\out.bmp如果我包含该sanitize(temp)行,我输出的输出会有所不同.我从消息框得到的输出是"临时没有改变".
List<Point> original = temp;
sanitize(temp);
if (temp.Equals(original)) {
MessageBox.Show(@"temp was not changed.");
} else {
MessageBox.Show(@"temp was changed.");
}
outputPointsOnBitmap(temp.ToArray(), ref windowBitmap, Color.Yellow);
windowBitmap.Save("C:\\out.bmp");
Run Code Online (Sandbox Code Playgroud)
对于好奇,这里有更多的源代码:
private void outputPointsOnBitmap(Point[] points, ref Bitmap bitmap, Color markerColor) {
foreach (Point point in points) {
bitmap.SetPixel(point.X, point.Y, markerColor);
}
}
private List<Point> sanitize(List<Point> crossPoints) {
SortedSet<int> indexesToDelete = new SortedSet<int>();
for (int i = 0; i < crossPoints.Count() - …Run Code Online (Sandbox Code Playgroud) 说我有一个ListA.我想制作一个重复的ListB,所以当我使用ListB.Remove(SomeItem);它时不会影响listA.
列表项应该引用相同的对象.
由于这是在循环中使用,因此使用资源要求最低的方法很重要.但我不知道如何以最有效的方式做到这一点.
我有收藏:
public class myList : List<myClass> { }
Run Code Online (Sandbox Code Playgroud)
并做:
foreach (myClass obj in myList) {
//...
if (some_condition) {
myList.Add(newObj); }
//...
}
Run Code Online (Sandbox Code Playgroud)
所以在2次迭代之前,我得到的错误如下:collection was changed.显然问题在于myList.Add(newObj);.
我如何组织这个循环以避免错误?请记住:我可以添加一些元素列表但永远不会删除.
我可以使用for循环与迭代器.但在这种情况下,我的代码看起来会更糟糕.也许这是一个更好的解决方案?
c# ×10
list ×3
.net ×2
clone ×2
generics ×2
collections ×1
deep-copy ×1
foreach ×1
icloneable ×1
json.net ×1