wiem IEnumerable.ToList() ma utworzyć nową listę, ale z elementów wskazujących na tych samych oryginalnych elementów w IEnumerable, jak to omówiono w ToList()-- Does it Create a New List?Dziwne, IEnumerable.ToList() tworzy całkowicie nowe obiekty
Jednak otrzymuję dziwne zachowanie z mojego kodu przy użyciu VS 2012; WPF; i .NET 4.0. Zaczęło się, gdy IEnumerable.SequenceEquals() wydawało się nie działać tak, jak się spodziewałem. I wykopali wokół z mojego okna QuickWatch, a niewiarygodnie, następujące oświadczenie wartość false:
this.Items.First() == this.Items.ToList()[ 0 ]
Próbowałem nawet:
this.Items.ToList().IndexOf(this.Items.First())
które oceniano na -1.
Items
jest zadeklarowana jako własność na niestandardowym kontroli WPF, tak jak poniżej:
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register (
"Items",
typeof (IEnumerable<UserLayoutType>),
typeof (UserLayoutSelectorControl),
new FrameworkPropertyMetadata (null, FrameworkPropertyMetadataOptions.AffectsRender, UserLayoutSelectorControl.PropertyChanged));
public IEnumerable<UserLayoutType> Items
{
get
{
return (IEnumerable<UserLayoutType>) this.GetValue (UserLayoutSelectorControl.ItemsProperty);
}
set
{
this.SetValue (UserLayoutSelectorControl.ItemsProperty, value);
}
}
UserLayoutType to po prostu klasa generowane przez narzędzie XSD, z następującym oświadczeniem:
//
// This source code was auto-generated by xsd, Version=4.0.30319.17929.
//
namespace MyAssays.UserLayoutCore.UserLayoutUtility {
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("UserLayout", Namespace="", IsNullable=false)]
public partial class UserLayoutType {
ten są metody w klasie fabryki, które tworzą elementy UserLayoutType w pierwszej kolejności:
public static IEnumerable<UserLayoutType> CreateFromFolder (string folderPath)
{
if (String.IsNullOrEmpty(folderPath))
throw new ArgumentNullException("folderPath", "Folder path must not be null");
var userLayoutXmlFilePaths = Directory.GetFiles (folderPath).Where (filePath => filePath.EndsWith (".UserLayout.xml", StringComparison.InvariantCultureIgnoreCase));
return userLayoutXmlFilePaths.Select(filePath => UserLayoutFactory.CreateFromFile(filePath));
}
public static UserLayoutType CreateFromFile (string filePath)
{
using (var stream = new StreamReader (filePath))
{
return (UserLayoutType) new XmlSerializer (typeof (UserLayoutType)).Deserialize (stream);
}
}
Ktoś ma pojęcie, co się dzieje? Patrz zdjęcie poniżej:
cześć, spróbuj użyć metody .Equals, czyli. this.Items.First(). Equals (this.Items.ToList() [0]) – user1778606
Czym są zadeklarowane elementy? Myślę, że najlepiej jest spróbować stworzyć krótki prosty fragment kodu, który pokazuje problemy, o których mówisz. Następnie opublikuj kod w swoim pytaniu. Poza tym musisz nauczyć się różnicy między == a .Equals() – dcaswell
Jaki jest typ środowiska wykonawczego 'this.Items'? Nie ma żadnych przesłonięć na 'Equals' lub' == '? – Blorgbeard