小编Red*_*yph的帖子

.net XML序列化 - 存储引用而不是对象复制

  • 在.Net/C#Application中,我有相互引用的数据结构.
  • 当我序列化它们时,.Net使用单独的对象副本序列化所有引用.
  • 在下面的示例中,我正在尝试序列化为'Person'数组
  • "人"可能会引用另一个人.

    public class Person
    {
        public string Name;
        public Person Friend;
    }
    
    Person p1 = new Person();
    p1.Name = "John";
    
    Person p2 = new Person();
    p2.Name = "Mike";
    
    p1.Friend = p2;
    
    Person[] group = new Person[] { p1, p2 };
    XmlSerializer ser = new XmlSerializer(typeof(Person[]));
    using (TextWriter tw = new StreamWriter("test.xml"))
        ser.Serialize(tw,group );
    
    //above code generates following xml
    
    <ArrayOfPerson>
      <Person>
        <Name>John</Name>
        <Friend>
          <Name>Mike</Name>
        </Friend>
      </Person>
      <Person>
        <Name>Mike</Name>
      </Person>
    </ArrayOfPerson>
    
    Run Code Online (Sandbox Code Playgroud)
  • 在上面的代码中,同一个'Mike'对象存在于两个地方,因为对同一个对象有两个引用.

  • 在反序列化时,它们变成两个不同的对象,它们在序列化时不是精确的状态.
  • 我想避免这种情况,只有序列化xml中的对象副本,所有引用都应引用此副本.反序列化后,我想回来,相同的旧数据结构.
  • 可能吗 ?

.net c# xml xml-serialization

34
推荐指数
2
解决办法
2万
查看次数

CollectionViewSource仅在第一次绑定到源时进行排序

我正在使用绑定到CollectionViewSource(播放器)的DataGrid ,它本身绑定到ListBox(级别)的当前所选项目,每个项目包含要在DataGrid中排序/显示的集合:

<ListBox Name="lstLevel"
         DisplayMemberPath="Name" 
         IsSynchronizedWithCurrentItem="True" />
Run Code Online (Sandbox Code Playgroud)

...

<!-- DataGrid source, as a CollectionViewSource to allow for sorting and/or filtering -->
<CollectionViewSource x:Key="Players" 
                      Source="{Binding ElementName=lstLevel, 
                                       Path=SelectedItem.Players}">
  <CollectionViewSource.SortDescriptions>
    <scm:SortDescription PropertyName="Name" />
  </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
Run Code Online (Sandbox Code Playgroud)

...

  <DataGrid Name="lstPlayers" AutoGenerateColumns="False" 
            CanUserSortColumns="False"
            ItemsSource="{Binding Source={StaticResource Players}}">
    <DataGrid.Columns>
      <DataGridTextColumn Header="Name"
                          Binding="{Binding Path=Name, Mode=TwoWay}"
                          Width="*" />
      <DataGridTextColumn Header="Age"
                          Binding="{Binding Path=Age, Mode=TwoWay}"
                          Width="80">
      </DataGridTextColumn>
    </DataGrid.Columns>
  </DataGrid>
Run Code Online (Sandbox Code Playgroud)

(整个C#代码在这里,XAML代码在这里,整个测试项目在这里 - 除了DataGrid我已经为玩家添加了一个简单的ListBox,以确保它不是DataGrid问题)

问题是玩家在第一次显示时进行排序,但是一旦我从ListBox中选择另一个级别,他们就不再排序了.此外,在第一次显示玩家时修改名称将根据更改对其进行排序,但是一旦更改了级别,就不再对其进行排序.

所以看起来改变CollectionViewSource的源代码会以某种方式破坏排序功能,但我不知道为什么,也不知道如何修复它.有谁知道我做错了什么?

(我用过滤器进行了测试,但是那个按预期工作了)

该框架是.NET 4.

wpf binding collectionviewsource

19
推荐指数
3
解决办法
1万
查看次数

后增量运算符重载

我在尝试在C#中重载后增量运算符时遇到问题.使用整数我们得到以下结果.

int n;

n = 10;
Console.WriteLine(n); // 10
Console.WriteLine(n++); // 10
Console.WriteLine(n); // 11

n = 10;
Console.WriteLine(n); // 10
Console.WriteLine(++n); // 11
Console.WriteLine(n); // 11
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用类时,它看起来像是交换对象.

class Account
{
    public int Balance { get; set; }
    public string Name { get; set; }

    public Account(string name, int balance)
    {
        Balance = balance;
        Name = name;
    }

    public override string ToString()
    {
        return Name + " " + Balance.ToString();
    }

    public static Account operator ++(Account a)
    {
        Account b …
Run Code Online (Sandbox Code Playgroud)

c# operator-overloading

16
推荐指数
2
解决办法
1万
查看次数

如何设置cell.imageview框架

我有表视图,我希望我的图像视图重置框架

我使用了以下代码:

[[cell imageView] setFrame:CGRectMake(0,0,32,32)];
Run Code Online (Sandbox Code Playgroud)

但没有任何工作

请让我知道如何调整默认的imageView部分

谢谢

iphone objective-c

8
推荐指数
2
解决办法
1万
查看次数

NHibernate或EF,Shards还是手动分片?

我想知道我是否做出了正确的决定.我正在开发一个应用程序,允许用户混合来自本地数据库(可能是SQL CE或SQLite)和远程共享数据库(很可能是MySQL)的数据库条目.理想情况下,它必须是灵活的并且与几种数据库服务器类型兼容(静态地按照初始配置,不动态地混合几种类型)但这不是一个显示停止.

代码是C#,.NET 4.

当然,我正在考虑NHibernate,因为它在版本3.0中具有LINQ,对代码可读性和效率的加分,并且模型优先方法比实体框架更自然.它还有混合数据库部分的Shards,这应该更容易处理.另一个选项是EF 4,我不喜欢它,因为它在.NET上下文和Visual Studio中完全支持,并且在那里有一个充满希望的未来.

碎片是否足够成熟?它已经坚持3 beta了很长一段时间,看起来像一个死的项目,让人们基于这个扩展部署专业应用程序?它有未来吗?还是有其他选择吗?

NHibernate 3在Mono上运行吗?我想是的,但实际的确认会很棒.如果必须将Web服务器开发为替代接口,那将是EF的另一个优势.

提前感谢您提供任何信息或反馈!


更新1

显然.NET 4是一个问题,为了使用NHibernate,.NET 3.5更加明智.此外,SQLite和SQL CE或WPF等框架的其他部分也存在明显的问题.

NHibernate是否允许跨不同数据库类型(例如MySQL和SQLite)进行分片?

所以我开始相信EF 4对于客户端来说更安全,而NHibernate对于可能的ASP.NET接口来说更安全.

database nhibernate entity-framework sharding

6
推荐指数
1
解决办法
1392
查看次数

拖放动态创建的快捷方式

我有一个C#应用程序,它创建了一个快捷方式来启动具有特定参数和初始目录的其他程序.我希望用户能够从Windows窗体中拖动一个快捷方式,并将其放在任何相关的位置,如桌面,开始菜单等,但我真的不知道如何处理,有人可以指向我正确的方向?

我一直在使用的PInvoke,看到几件样品的IShellLink这一个,或阅读等等之类的答案在这里,它已经帮助创建快捷方式,并将它们保存在一个.lnk文件.我假设DoDragDrop()当用户启动拖动操作时,我必须在调用中移交数据,例如通过处理MouseDown信号.就我所知,我想我需要确切知道目标期望接受丢弃的类型,以及如何序列化快捷方式,但无法找到该部分的任何信息.

也许另一种选择是获取drop的位置,并从我的应用程序中管理它,但是我再次对如何做到这一点有点无能为力.

框架版本目前是3.5,我只考虑Windows平台.

在此先感谢您的帮助!


更新/解决方案:

使用上面提到的ShellLink代码创建临时快捷方式文件,我只是用于DataObject拖放,如下例所示:

private void picShortcut_MouseDown(object sender, MouseEventArgs e)
{
    ShellLink link = new ShellLink();

    // Creates the shortcut:
    link.Target = txtTarget.Text;
    link.Arguments = txtArguments.Text;
    link.Description = txtDescription.Text;
    link.IconPath = txtIconFile.Text;
    link.IconIndex = (txtIconIndex.Text.Length > 0 ?
        System.Int32.Parse(txtIconIndex.Text) : 0);
    link.Save("tmp.lnk");

    // Starts the drag-and-drop operation:
    DataObject shortcut = new DataObject();
    StringCollection files = new StringCollection();
    files.Add(Path.GetFullPath("tmp.lnk"));
    shortcut.SetFileDropList(files);
    picShortcut.DoDragDrop(shortcut, DragDropEffects.Copy);
}
Run Code Online (Sandbox Code Playgroud)

如果你考虑PInvoke代码(这里没有显示),那么相当复杂,我仍然需要用目标名称创建这个临时文件.如果有人知道......呃,快捷方式,欢迎!也许通过移植 …

.net c# drag-and-drop windows-shell

5
推荐指数
1
解决办法
2900
查看次数

为什么析构函数被调用两次?

我有以下代码:

#include <cstdio>
#include <iostream>
using namespace std;

class A
{
    int a, b;
public:
    A() : A(5, 7) {}
    A(int i, int j)
    {
        a = i;
        b = j;
    }
    A operator+(int x)
    {
        A temp;
        temp.a = a + x;
        temp.b = b + x;
        return temp;
    }
    ~A() { cout << a << " " << b << endl; }
};

int main()
{
    A a1(10, 20), a2;
    a2 = a1 + 50;
}
Run Code Online (Sandbox Code Playgroud)

输出显示:

60 …
Run Code Online (Sandbox Code Playgroud)

c++ destructor class object

5
推荐指数
2
解决办法
271
查看次数